Question-Twenty-five numbers are entered from the keyboard into an
array. The number to be searched is entered through the
keyboard by the user. Write a program to find if the number to
be searched is present in the array and if it is present, display
the number of times it appears in the array.
Solution-
with this question i am starting chapter 8 problems. it's a pretty important chapter.so, please try to understand theory portion nicely, then jump on the problems. So, let's first see the solution of this problem. it's quite a basic one.
#include<stdio.h>
#define m 25 /* using this we can easily generalize this program for any number of elements */
int main()
{
int array[ m];
int i,count=0,num;
printf("Enter any 25 numbers\n");
for(i=0;i<=m-1;i++)
scanf("%d",&array[i]);
printf("Enter the number that you want to search\n");
scanf("%d",&num);
for(i=0;i<=m-1;i++)
{
if(num==array[i])
count++;
}
if(count!=0)
printf("Number is present in the array. it appeared %d times\n",count);
else
printf("Number is not present in the array\n");
return 0;
}
array. The number to be searched is entered through the
keyboard by the user. Write a program to find if the number to
be searched is present in the array and if it is present, display
the number of times it appears in the array.
Solution-
with this question i am starting chapter 8 problems. it's a pretty important chapter.so, please try to understand theory portion nicely, then jump on the problems. So, let's first see the solution of this problem. it's quite a basic one.
#include<stdio.h>
#define m 25 /* using this we can easily generalize this program for any number of elements */
int main()
{
int array[ m];
int i,count=0,num;
printf("Enter any 25 numbers\n");
for(i=0;i<=m-1;i++)
scanf("%d",&array[i]);
printf("Enter the number that you want to search\n");
scanf("%d",&num);
for(i=0;i<=m-1;i++)
{
if(num==array[i])
count++;
}
if(count!=0)
printf("Number is present in the array. it appeared %d times\n",count);
else
printf("Number is not present in the array\n");
return 0;
}
No comments:
Post a Comment