It's all about if else I am not getting right output can anyone tell me what's wrong with my code | Sololearn: Learn to code for FREE!

0

# It's all about if else I am not getting right output can anyone tell me what's wrong with my code

#include<stdio.h> int main() { int a,b,c,d; printf("\n enter first number:"); scanf("%d",&a); printf("\n enter second number:"); scanf("%d",&b); printf("\n enter third number:"); scanf("%d",&c); printf("\n enter fourth number:"); scanf("%d",&d); if(a>b){ if(a>c){ if(a>d){ printf("\n first number %d is maximum:",a); } else{ printf("\n fourth number %d is maximum:",d); } if(b>c){ printf("\n second number %d is maximum:",b); } else{ printf("\n third number %d is maximum:",c); } if(b>d){ printf("\n second number %d is maximum:",b); } else{ printf("\n fourth number %d is maximum:",d); } if(c>d){ printf("\n third number is maximum:",c); } else{ printf("\n fourth number is maximum:",d); } } } return 0; }

5/21/2022 12:53:22 PM

Sunilsingh Nagarkoti

+2

I created a loop. The loop compares the first element of the array with the next element of the array. If this element is greater, then it compares everything with it. If you found my answer helpful, please leave a like. As you go through the lesson " Conditionals and Loops", it will become clearer to you.

+1

Thnx for your suggestion I can do it with the help of array but problem is here I have done my code find maximum between three numbers but in this cynario I don't know what happening

+1

where is the else part of nested if ?

0

Your code had to be almost completely rewritten. That's better: #include <stdio.h> #include <malloc.h> int main() { int arr[4]; printf("Enter first number:"); scanf("%d", &arr[0]); printf("Enter second number:"); scanf("%d", &arr[1]); printf("Enter third number:"); scanf("%d", &arr[2]); printf("Enter fourth number:"); scanf("%d", &arr[3]); int *ptrVar = malloc(sizeof(*ptrVar)); int *ptrNum = malloc(sizeof(*ptrNum)); *ptrVar = arr[0]; *ptrNum = 1; for (int i = 0; i < sizeof(arr) / sizeof(int); i++) { if(*ptrVar < arr[i]) { *ptrVar = arr[i]; *ptrNum = i+1; } } printf("\n %d-th number %d is maximum!", *ptrNum, *ptrVar); free(ptrVar); free(ptrNum); return 0; }

0

Okay thnx I got it