0% found this document useful (0 votes)
8 views

A 4

notes

Uploaded by

bharati
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

A 4

notes

Uploaded by

bharati
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

5. Write a ‘C’ function to check if a number is prime.

Use this function to

display all prime numbers between 100 and 500.

Ans:- #include <stdio.h>

int displayPrimeNumber (int x); //function prototype

int main ()

int num1, num2, i, flag;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

printf("Prime numbers between %d and %d are:\n", num1, num2);

// Displaying prime number between num1 and num2

for (i = num1 + 1; i < num2; ++i)

flag = displayPrimeNumber(i);

if (flag == 0) //if flag == 0, then display i

printf("%d\t", i);

return 0;

int displayPrimeNumber(int x)

int j, flag = 0;
for (j = 2; j <= x/2; ++j)

if (x % j == 0)

flag = 1;

break;

return flag;

}
6.Write a ‘C’ program to accept an array of n float values and display them in the reverse Order.

Ans:- #include <stdio.h>

void main()

int i,n,a[100];

printf("\n\nRead n number of values in an array and display it in reverse order:\n");

printf("------------------------------------------------------------------------\n");

printf("Input the number of elements to store in the array :");

scanf("%d",&n);

printf("Input %d number of elements in the array :\n",n);

for(i=0;i<n;i++)
{

printf("element - %d : ",i);

scanf("%d",&a[i]);

printf("\nThe values store into the array are : \n");

for(i=0;i<n;i++)

printf("% d",a[i]);

printf("\n\nThe values store into the array in reverse are :\n");

for(i=n-1;i>=0;i--)

printf("% 5d",a[i]);

printf("\n\n");

7. Write a ‘C’ program to accept n integers in an array and count the frequency of each

element of an array.

Ans:- #include <stdio.h>

void main()

{
int arr1[100], fr1[100];

int n, i, j, ctr;

printf("\n\nCount frequency of each element of an array:\n");

printf("------------------------------------------------\n");

printf("Input the number of elements to be stored in the array :");

scanf("%d",&n);

printf("Input %d elements in the array :\n",n);

for(i=0;i<n;i++)

printf("element - %d : ",i);

scanf("%d",&arr1[i]);

fr1[i] = -1;

for(i=0; i<n; i++)

ctr = 1;

for(j=i+1; j<n; j++)

if(arr1[i]==arr1[j])

ctr++;

fr1[j] = 0;

}
if(fr1[i]!=0)

fr1[i] = ctr;

printf("\nThe frequency of all elements of array : \n");

for(i=0; i<n; i++)

if(fr1[i]!=0)

printf("%d occurs %d times\n", arr1[i], fr1[i]);

8. Write a ‘C’ program to calculate occurrences of a number in an array of n integers.

Ans:- #include <stdio.h>

int main()

int Size, i, num, occr = 0;

printf("Please Enter the Array size = ");

scanf("%d", &Size);
int arr[Size];

printf("Enter the Array %d elements : ", Size);

for (i = 0; i < Size; i++)

scanf("%d", &arr[i]);

printf("Please Enter the Array Item to Know = ");

scanf("%d", &num);

for (i = 0; i < Size; i++)

if (arr[i] == num)

occr++;

printf("%d Occurred %d Times.\n", num, occr);

You might also like