Somebody please help. | SoloLearn: Learn to code for FREE!

+1

Somebody please help.

//c program to accept integer value and display the largest number using array.But it is not working properly.Sometimes infinite loop happens to scanf .if infinite loop does 't happen it can t display the correct largest interger.Help! #include<stdio.h> int main() { int a[10],i,la; for(i=0;i<=9 ;i++) { printf("Enter number %d\n",i); scanf("%d",&a[i]); } la=a[0]; for(i=0;i<=9;i++) { if(a[0]<a[i]) { la=a[i]; } } printf("The largest number is %d\n",la); return 0; }

5/29/2020 8:12:54 AM

Coding Gl

5 Answers

New Answer

+2

I see no cause for an infinite loop to happen. It behaved okay when I tried the code. There is a logic error in the line that compares array values to determine which value is the largest. Since la is the variable where the largest value is held, the comparison should be with la. Change if(a[0]<a[i]) to if(la<a[i]) and that will fix the outcome.

+2

Ok i am goona try this

+2

Many thanks Brian,it works

+1

🙂🙂🙂♥️

0

Coding Gl, you are welcome!