Enter your email address:


Monday, September 2, 2013

Home » , » answer to 6 and 7 questions find the greatest

answer to 6 and 7 questions find the greatest


Questions: Top-30-c-programs-asked-in-interview_6304.html

6. Write a program to find the greatest of three numbers.
Program:

#include
int main(){
int a, b, c;
printf("En ter a,b,c: \n");
scanf("%d %d %d",&a,&b,&c);
if (a>b&&a>c){
printf("a is Greater than b and c");
}
else if (b>a&&b>c){
printf("b is Greater than a and c");
}
else if (c>a&&c>b){
printf("c is Greater than a and b");
}
else{
printf("al l are equal or any two values are equal");
}
return 0;
}
Output:
Enter a,b,c: 3 5 8
c is Greater than a and b
Explanatio n with examples:
Consider three numbers a=5,b=4,c= 8
if(a>b&&a>c) then a is greater than b and c
now check this condition for the three numbers 5,4,8 i.e.
if(5>4&&5>8) /* 5>4 is true but 5>8 fails */
so the control shifts to else if condition
else if(b>a&&b>c) then b is greater than a and c
now checking this condition for 5,4,8 i.e.
else if(4>5&&4>8) / * both the conditions fail */
now the control shifts to the next else if condition
else if(c>a&&c>b) then c is greater than a and b
now checking this condition for 5,4,8 i.e.
else if(8>5&&8>4) / * both conditions are satisfied */
Thus c is greater than a and b.

7. Write a program to find the greatest among ten numbers.
Program:
#include
int main(){
int a[10];
int i;
int greatest;
printf("En ter ten values:");
//Store 10 numbers in an array
for (i = 0; i<10; i++){
scanf("%d" ,&a[i]);
}
//Assume that a[0] is greatest
greatest = a[0];
for (i = 0; i<10; i++){
if (a[i]>greatest){
greatest = a[i];
}
}
printf("\n Greatest of ten numbers is %d", greatest);
return 0;
}
Output:
Enter ten values: 2 53 65 3 88 8 14 5 77 64 Greatest of ten numbers is 88
Explanatio n with example:
Entered values are 2, 53, 65, 3, 88, 8, 14, 5, 77, 64
They are stored in an array of size 10. let a[] be an array holding these values.
/* how the greatest among ten numbers is found */
Let us consider a variable'greatest' . At the beginning of the loop, variable'greatest' is assinged with the value of
first element in the array greatest=a [0]. Here variable'greatest' is assigned 2 as a[0]=2.
Below loop is executed until end of the array'a[]';.
for(i=0; i
{
if(a[i]>gr eatest)
{
greatest= a[i];
}
}
For each value of'i', value of a[i] is compared with value of variable'greatest' . If any value greater than the value
of'greatest' is encountere d, it would be replaced by a[i]. After completion of'for'loop, the value of variable
'greatest' holds the greatest number in the array. In this case 88 is the greatest of all the numbers.
Share this games :

0 comments:

Post a Comment

Related Posts