c program to find largest number among n numbers


1. c program to find largest number among N numbers  
To understand this example you should have knowledge of array in c.

# include <stdio.  h>
void main ()
{
 int x [10], j, y, n; 
printf ("Input the size of the array:"); 
scanf ("%d", &n); 
printf ("Input the elements of the array: \ n"); 
for (j=0;j<n; j + +)
{
scanf ("%d", x[j]);
}
y = X [0];
for (j=1;j<n;j + +)
{
if (x[j]> y)
Y = x[j];
}
printf ("\ The largest element among inputted array elements is: %d", y); 
}
Output
 Input the size of the array:  4
Input the elements of the array
2 7 1 14
The largest element among input array elements is: 14
EXPLANATION:
Here the objective of the program is to find the largest element among the array.  Here, the user enters the size of the array, suppose 4 then he will input the elements of the array through scanf() function and for loop.  Then suppose x [0] value is assigned to the larger variable.  To find the largest among the array elements, the next step is to initialize the variable j= 1 in for loop and check the condition 1<4.  Since it satisfies execute the loop and inside the for loop we have used the if condition to measure between x [1] and larger.  if( x [1]> larger) e.g.,  7> 1 then larger 7. .  Next loop increments to 2 and inside the loop again test the condition as if(x[3]>larger) e.g.,  14>7 yes condition satisfied.  therefore larger =  14 and next loop increments to 4. As the maximum size of loop is 4.  therefore loop terminates.
Some other program of one dimensional array
2.  c program to find smallest elements in one dimensional
# include <stdio.h>
# include<conio.h>
void main ()
{
int x [100], y, n, i, loc=1;
clrscr();
printf ("Input the size of the array:");
scanf ("%d",n); 
printf ("Input %d elements of the array:" n);
for (1 =0; i<n;i++)
{
scanf ("%d", &x [i]); 
}
y = x [0;
for (i=1: i<n;i++)
{
if (x [i] <y)
{
y=x[i];
loc = i + 1;
}}
printf ("Smallest element is present at location [%d] and the value  is: %d ", loc, y);
getch ();
} 
Output:
Input the size of the array: 3
Input 3 elements of the array:
3 8 1 
Smallest elements is present at location [3] and the value is: 1
EXPLANATION:
Adopt the largest number among three using array explanation.
3. C program of reverse the array  
# include <stdio.h>
# include <conio.h>
{
void main ()
{
 int x [100], y [100], n, i, j:
clrscr (); 
printf ("Input the size of the array:"); 
scanf ("%d", &n);
 printf ("Input the elements of the array:");
for (1 = 0; i <n; i++)
scanf ("%d",&x[i]); 
for (i = n -1, j=0;i> = 0;i - -,j++)
{
y[ j] = x [i];
}
for (i =0; i<n; i + +)
{
x [i] =y [i];
}
printf ("The Reverse of inputted array elements are:"); 
for (i =0;i <n;i++)
{
printf ("%d", x [i]); 
}
getch ();
Output:
Input the size of the array: 4
Input the elements of the array:
2 3 4 5
The reverse of input array elements are: 5 4 3 2
4. c program of find the sum and average of n numbers using one dimensional array
# include <stdio.h>
void main ()
{
int x [50], n, sum = 0, j; 
float  avg ;
printf ("Input the size of the array:"); 
scanf ("%d",&n);
printf ("Input the elements of the array:");
for (“j=0; j<n; j++)
{
scanf ("%d", &x [j]);
}
for (j=0; j<n; j++)
{
sum = sum+x [j];
}
printf ("The sum of elements of the array is: d \ n", sum); 
avg = sum/n; 
printf ("The Average of inputted array elements is: %f", avg);
}
Output
Input the size of the array: 3
Input the elements of the array: 3 4 5
The sum of elements of array is: 12
The Average of inputted array elements is: 4.000000

5. c program to insert the elements into the array and display it.
 # include <stdio.h>
# define M 3
void main ()
{
int  x[M],y;
for(y =0; y <M; y++)
{
printf ("Input the elements of the array x[%d]:", y);
scanf("%d" ,&x[y]); 
}
for (y=0; y<M; y++)
{
printf ("The inputted elements of the array x [%d] is: %d\n" ,y, x[y]);
}
}
 Output
Input the elements of the array x [0]: 5
Input the elements  of the array x [1]: 6
Input the elements of the array x [2]: 7
The input elements of the array x [0] is: 5
The input elements of the array x [1] is: 6
The input elements of the array x [2] is: 7
EXPLANATION:
 Here in the above program, instead of fixing  the size of the array we have used symbolic constant, M, i.e. , size of the array is 3.  We cannot store more than 3elements in the above array. Then we have input the elements through scanf () function by using for loop.  Then using for loop and printf () function we have displayed the elements of the array.
 Instead of using the symbolic constant we can alternatively write the same program. 
Let 's see the same program. 
#include <stdio.h>
void main ()
{
int x [100], y,n;
printf ("Input the size of the array:"); 
scanf ("%d", &n);
for (y = 0; y<n; y++)
{
Printf ("Input the elements of the array x [%d):” y):
scanf ("%d ", &x[y]); 
}
for (y = 0;y<n; y++)
{
printf ("The inputted elements of the array x [%d]is: %d\n”,y,x[y]);
}}

No comments

Powered by Blogger.