Biggest of 3 numbers code confused me - c

I wrote a piece of code which attempts to tell which of three user-inputted numbers is the greatest. However, I am unable to understand why my code breaks for the input 3, 1, 2 and works for the input 55, 54, 56.
My code:
main()
{
int a,b,c;
printf("enter three numbers");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
printf("%d is greatest",a);
if(b>a && b>c)
printf("%d is greatest",b);
else printf("%d is greatest",c);
getch();
}
What am I doing that causes this error, and what can I do to fix it?

You are missing "else if", that's for sure.
main()
{
int a,b,c;
printf("enter three numbers: ");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
printf("%d is greatest",a);
else if(b>a && b>c)
printf("%d is greatest",b);
else
printf("%d is greatest",c);
}

You need to add an else before the line if(b>a && b>c).
i.e.
if(b>a && b>c)
should be
else if(b>a && b>c)

What people say here are true, but for best practice I would reduce the checks in the ifs:
main()
{
int a,b,c;
printf("enter three numbers: ");
scanf("%d %d %d",&a,&b,&c);
if(a>=b) //it's not b.
{
if(a>=c)
{
printf("%d is greatest",a);
}
else
{
printf("%d is greatest",c);
}
}
else // here you know that b > a, then it's not a.
{
if(b>=c)
{
printf("%d is greatest",b);
}
else
{
printf("%d is greatest",c);
}
}
}

try this
condition? exp1: exp2;
evaluates to
if condition is true then return exp1 else return exp2
int main(){
int a,b,c;
printf("enter three numbers");
scanf("%d %d %d",&a,&b,&c);
int d = (a >= b)? a: b;
d = (d >= c)? d: c;
printf("%d is greatest", d);
}

Your second if statement should be else if

Just add a simple else if statement to your code and it should work fine as in :
main()
{
int a,b,c;
printf("enter three numbers");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
printf("%d is greatest\n",a);
else if(b>a && b>c)
printf("%d is greatest\n",b);
else printf("%d is greatest\n",c);
//getch();
}

Why don't you try this. Its neater. The problem with your code was that you were missing an else which could have been put along with if.
main() {
int a,b,c;
printf("enter three numbers");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
printf("%d is greatest\n",a);
else if(b>c)
printf("%d is greatest\n",b);
else printf("%d is greatest\n",c);
//getch();
}

#define MAX(a,b) (((a)>=(b))?(a):(b))
#define MAX3(a,b,c) MAX(MAX(a,b),c)

Related

I can't get the printf result if the third conditional is true

I know I can write this code with fewer lines and I did it before, but I tried with an "if else" chain to explore different ways to code the same program since I'm still learning the basics. But doing this way I can't get the result if the third conditional(C is the greater one) is true. Only if the third conditional is true the program finish without printing anything. What am I doing wrong?
#include <stdio.h>
#include <conio.h>
int main(){
int a, b, c = 0;
printf("Please insert three different numbers:\n");
printf("Insert A value:");
scanf("%d", &a);
printf("Insert B value:");
scanf("%d", &b);
printf("Insert C value:");
scanf("%d", &c);
//comparing A, B and C to find the greater number
if(a > b){
if (a > c){
printf("\nThe greater is A: %d", a);
}
}else if (b > a){
if(b > c){
printf("\nThe greater is B: %d", b);
}
}else{
printf("\nThe greater is C: %d", c);
}
getch();
return 0;
}
Change you if statements to this:
if(a > b && a > c){
printf("\nThe greater is A: %d", a);
}else if (b > a && b > c){
printf("\nThe greater is B: %d", b);
}else if (c > a && c > b){
printf("\nThe greater is C: %d", c);
}
Also, using the same structure you have, you can use this:
if(a > b){
if (a > c){
printf("\nThe greater is A: %d", a);
}else {
printf("\nThe greater is C: %d", c);
}
}else if (b > a){
if(b > c){
printf("\nThe greater is B: %d", b);
}else{
printf("\nThe greater is C: %d", c);
}

Write a program that will take three integers as input and will print the second largest number

I have tried this program to take 3 integers and print the 2nd largest number:
#include<stdio.h>
int main()
{
int a,b,c,max2;
printf("Enter 3 integers: ");
scanf("%d%d%d",&a,&b,&c);
max2=a;
if(a>b){
max2=b;
printf("")
}
return 0;
}
Now i am stuck here. I am unable to find the logic behind this code. What can I do?
This is not the Logic which you can understand, it'll not give you the right, expected result.
Code:
#include <stdio.h>
int main()
{
int a, b, c;
printf("Values: ");
scanf("%d%d%d", &a, &b, &c);
if(a>b && a>c)
{
if(b>c)
printf("2nd largest: %d", b);
else
printf("2nd largest: %d", c);
}
else if(b>c && b>a)
{
if(c>a)
printf("2nd largest: %d", c);
else
printf("2nd largest: %d", a);
}
else if(a>b)
printf("2nd largest: %d", a);
else
printf("2nd largest: %d", b);
return 0;
}
You should compare all the three variables to get the 2nd largest among those numbers.
Output:
Values: 32 31 12
2nd largest: 31
Explanation:
First pick any variable and compare it with the other two variables like if(a>b && a>c), if its true, it means a is the largest and any of the two variables b and c is the 2nd largest, so inside the if(a>b && a>c) block there's a comparison if(b>c), if true then b is the 2nd largest otherwise c is the second largest. Similarly, compare the other two variables for if they are the largest. e.g. else if(b>c && b>a) and else if(c>a && c>b).
One method is to sort these three numbers and then print the middle one:
#include <stdio.h>
static inline void swap_if_out_of_order (int *p, int *q)
{
if (*p > *q) {
int t = *p;
*p = *q;
*q = t;
}
}
int main (void)
{
int a, b, c;
printf("Enter three integers\n");
if (scanf("%d%d%d", &a, &b, &c) == 3) {
swap_if_out_of_order(&a, &b);
swap_if_out_of_order(&b, &c);
swap_if_out_of_order(&a, &b);
printf("Second greatest: %d\n", b);
}
}
Or, without sorting, with at most three comparisons:
#include <stdio.h>
int main (void)
{
int a, b, c, m;
printf("Enter three integers\n");
if (scanf("%d%d%d", &a, &b, &c) == 3) {
if (a > b) {
if (b > c) m = b;
else if (a > c) m = c;
else m = a;
} else if (a > c) m = a;
else if (b > c) m = c;
else m = b;
printf("Second greatest: %d\n", m);
}
}
or, likely the most efficient way with max and min functions:
#include <stdio.h>
static inline int min (int x, int y) { return x < y ? x : y; }
static inline int max (int x, int y) { return x > y ? x : y; }
int main (void)
{
int a, b, c;
printf("Enter three integers\n");
if (scanf("%d%d%d", &a, &b, &c) == 3)
printf("Second greatest: %d\n", max(min(a, b), min(max(a, b), c)));
}
JS recursion:
function f(a,b,c) {
if(a>=b && c<b) return b;
if(a>b) return f(a,c,b);
return f(b,a,c);
}
f(2,12,0) // 2
#include <stdio.h>
main()
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
// largest //
if(a>b&&a>c)
printf("largest=%d",a);
if(b>a&&b>c)
printf("largest=%d",b);
if(c>b&&c>a)
printf("largest=%d",c);
// second largest//
if(a>b&&a<c)
printf("\nscenond largest=%d",a);
if(b>a&&b<c)
printf("\nscenond largest=%d",b);
if(c>a&&c<b)
printf("\nscenond largest=%d",c);
}
this will output the largest and the second largest number.

URI 1101 Sequence of Numbers and Sum

#include <stdio.h>
int main() {
int M,N,i=0,sum=0;
while(M>0 || N>0){
scanf("%d%d",&M,&N);
if(M<=0 || N<=0){
break;
}
else if(M==N){ printf("%d Sum=%d\n",N,M); }
else if(M<N){
for(i=M; i<=N; i++){
printf("%d ",i);
sum+=i;
}
printf("Sum=%d\n",sum);
sum=0;
}
else if(M>N){
for(i=N; i<=M; i++){
printf("%d ",i);
sum+=i;
}
printf("Sum=%d\n",sum);
sum=0;
}
}
return 0;
}
what is wrong with this code? URI judge saying 100% wrong answer but I tried all the test cases and it passed in those cases
I'm not sure, but before while loop you should get scanf("%d%d",&M,&N); one time.
Because the m and n is not defined.
Like this way :
scanf("%d%d",&M,&N);
while(M>0 || N>0){
scanf("%d%d",&M,&N);
if(M<=0 || N<=0){
break;
}
...

program to find the largest and smallest among three entered numbers and also display whether the identified largest/smallest number is even or odd

This is my homework and i am stuck with how should i identify that the smallest/largest number is even or odd.
#include <stdio.h>
void main()
{
int num1,num2,num3;
printf("Enter three numbers\n");
scanf("%d %d %d",&num1,&num2,&num3);
if(num1<num2 && num1<num3){
printf("\n%d is the smallest",num1);
}
else if(num2<num3){
printf("\n%d is the smallest",num2);
}
else{
printf("\n%d is the smallest",num3);
}
if(num1>num2 && num1>num3){
printf("\n%d is largest",num1);
}
else if(num2>num3){
printf("\n%d is largest",num2);
}
else{
printf("\n%d is largest",num3);
}
getch();
return 0;
}
Use 2 variables, one to store the smallest, one to store the largest.
int min, max;
Then, assign the variable :
if (num1 < num2)
min = num1;
if (num3 < min)
min = num3;
printf("%d is the largest number", min);
To know if a number is odd or even, the remainder (also called modulo) of its division by 2 will be 0 (for even) or 1 (for odd) :
int modulo = min % 2;
if (modulo == 0)
printf("%d is even", min);
else
printf("%d is odd", min);
/*Write a program to find the largest and smallest among three entered numbers and
also display whether the identified largest/smallest number is even or odd*/
#include <stdio.h>
#include <conio.h>
void main()
{
// start the programme
int a, b, c, large, small;
printf("Enter three numbers : \n");
scanf("%d%d%d", &a, &b, &c);
if (a > b && a > c)
{
printf("\n%d is largest", a);
large = a;
}
else if (b > c)
{
printf("\n%d is largest", b);
large = b;
}
else
{
printf("\n%d is largest", c);
large = c;
}
if (a < b && a < c)
{
printf("\n%d is smallest", a);
small = a;
}
else if (b < c)
{
printf("\n%d is smallest", b);
small = b;
}
else
{
printf("\n%d is smallest", c);
small = b;
}
if (large % 2 == 0)
{
printf("\n %d is even", large);
}
else
{
printf("\n %d is odd", large);
}
if (small % 2 == 0)
{
printf("\n %d is even", small);
}
else
{
printf("\n %d is odd", small);
}
getch();
// end the programme
}

I am trying to arrange three 3 numbers in ascending manner using if statement, but this doesn't work for the third number. What can be done about it?

#include <stdio.h>
int main()
{
int a, b, c, temp;
scanf("%d %d %d", &a, &b, &c);
if (a > b)
{
temp = a;
a = b;
b = temp;
}
else if (b > c)
{
temp = b;
b = c;
c = temp;
}
else if (c > a)
{
temp = c;
c = a;
a = temp;
}
printf("%d %d %d", a, b, c);
return 0;
}
If I put 8,6,3, the output comes 6,8,3. It doesn't change the last number. I am trying to arrange three 3 numbers in ascending manner using if statement, but this doesn't work for the third number. What can be done about it?
It easiest if you first find the smallest, then make sure the remaining two are correct :
int main()
{
int a, b, c, temp;
int ret = scanf("%d %d %d", &a, &b, &c);
if (ret != 3) {
printf("scanf() error\n");
exit(1);
}
// get smallest into a
if ((b < a) && (b < c)) {
temp = a;
a = b;
b = temp;
} else if ((c < a) && (c < b)) {
temp = a;
a = c;
c = temp;
}
// a is smallest, check b and c
if (c < b) {
temp = b;
b = c;
c = temp;
}
printf("%d %d %d", a, b, c);
return 0;
}
You need to use if instead of else if as you want to compare a with b, b with c and a with c (the three and not only one of them). Moreover, as you are moving the numbers you have to take into account where they are moved for the last comparison. And your third condition was wrong. So this should be what you are trying to do:
#include <stdio.h>
int main(){
int a, b, c, temp;
scanf("%d %d %d", &a, &b, &c);
if (a > b){
temp = a;
a = b;
b = temp;
}
if (b > c){
temp = b;
b = c;
c = temp;
if (a > b){
temp = a;
a = b;
b = temp;
}
}
else if (a > c){
temp = c;
c = a;
a = temp;
}
printf("%d %d %d", a, b, c);
return 0;
}
I think you have misunderstood the concept of if else if structure, In your case it is not working for third number because the execution will reach to else if part only when the if condition is false.
#include <stdio.h>
int main()
{
int a,b,c,temp;
scanf("%d %d %d",&a,&b,&c);
if(a>b) //evaluates to true.
{
temp=a;
a=b;
b=temp;
}
else if(b>c) // not able to execute.
{
temp=b;
b=c;
c=temp;
}
else if(c>a) // not able to execute
{
temp=c;
c=a;
a=temp;
}
printf("%d %d %d",a,b,c);
return 0;
}
a = 8
b = 6
c = 3
checking a>b evaluates to true hence swapped
now:
a = 6 // your output
b = 8
c = 3
you may need to go over the concept of if else structure once again
#include<stdio.h>
int main()
{
int a ,b,c;
printf("Enter the number : \n");
scanf("%d %d %d",&a,&b,&c);
if((a>b)&&(a>c))
{
if(b>c)
printf("%d %d %d",a,b,c);
else
printf("%d %d %d",a ,c,b);
}
else if((b>c)&&(b>a))
{
if(c>a)
printf("%d %d %d",b,c,a);
else
printf("%d %d %d",b,a,c);
}
else if((c>a)&&(c>b))
{
if(a>b)
printf("%d %d %d",c,a,b);
else
printf("%d %d %d",c,b,a);
}
return 0;
}
The easiest way is to use an array instead of three individual variables. Then use qsort for getting the input sorted.
Like:
#include <stdio.h>
#include <stdlib.h>
// Compare function for qsort
int cmp(const void *p1, const void *p2)
{
if (*(int*)p1 < *(int*)p2) return -1;
if (*(int*)p2 < *(int*)p1) return 1;
return 0;
}
int main()
{
int arr[3];
if (scanf("%d %d %d", &arr[0], &arr[1], &arr[2]) != 3) exit(1);
// Sort the input
qsort(arr, sizeof(arr)/sizeof(int), sizeof(int), cmp);
printf("%d %d %d\n", arr[0], arr[1], arr[2]);
return 0;
}
#include <stdio.h>
int main()
{
int a,b,c,temp,min;
scanf("%d %d %d",&a,&b,&c);
if(a>b)
{
temp=a;
a=b;
b=temp;
}
if(c<a)
{
min=c;
c=b;
b=a;
a=min;
}
else if(c>a && b<c) {
min=c;
c=b;
b=min;
}
printf("%d %d %d",a,b,c);
return 0;
}
you are comparing using else if, if any one condition satisfies it won't execute the other else condition.

Resources