23. Find the Largest Two Numbers
#include
#include
#define max 4
void main()
{
int array[max], i, largest1, largest2, temp;
printf("Enter %d integer numbers \n", max);
for (i = 0; i < max; i++)
{
scanf("%d", &array[i]);
}
printf("Input interger are \n");
for (i = 0; i < max; i++)
{
printf("%5d", array[i]);
}
printf("\n");
largest1 = array[0];
largest2 = array[1];
if (largest1 < largest2)
{
temp = largest1;
largest1 = largest2;
largest2 = temp;
}
for (i = 2; i < 4; i++)
{
if (array[i] >= largest1)
{
largest2 = largest1;
largest1 = array[i];
}
else if (array[i] > largest2)
{
largest2 = array[i];
}
}
printf("n%d is the first largest \n", largest1);
printf("%d is the second largest \n", largest2);
printf("nAverage of %d and %d = %d \n", largest1, largest2,
(largest1 + largest2) / 2);
getch();
}
Output
Enter 4 integer numbers
80
23
79
58
Input interger are
80 23 79 58
80 is the first largest
79 is the second largest
Average of 80 and 79 = 79
24. Sort
the Array in an Ascending Order
#include
#include
void main()
{
int i, j, a, n, number[30];
clrscr();
printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < n; ++i)
printf("%d\n", number[i]);
getch();
}
Output
Enter the value of N
6
Enter the numbers
3
78
90
456
780
200
The numbers arranged in ascending order are given below
3
78
90
200
456
780
No comments:
Post a Comment