#include<stdio.h>
int main()
{
int i,n, lar, elem;
printf("Enter total number of elements\n");
scanf("%d",&elem);
printf("Enter first number\n");
scanf("%d",&n);
lar=n;
for(i=1; i<=elem -1;i++);
{
printf("n\ Enter the number \n");
scanf("%d",&n);
if(n>lar)
lar=n;
}
printf("n\ The largest number is %d",lar);
return 0;
}
@iker_13 Here are some pointers based on your previous code that you shared here:
Do not print anything other than the answer. No āEnter total number of elements\nā, no extra texts.
First take an input n. Then a loop should run n times and read n numbers. And every time you get the number, compare it with the largest one and see if the new number that you read is the largest one.
Print just the largest number at the end as printf("%d\n", largest);. Do not print anything else.
turns out I was actually writing the code wrongā¦whereas I should have written thisā¦
#include <stdio.h>
int main()
{
unsigned int b,G,N,max=1;
scanf(ā%u\nā,&N);
for(b=1;b<=N;b++)
{
scanf(ā%uā,&G);
if(max>G)
max=max;
else
max=G;
}
printf(ā%uā,max);
return 0;
}
I was assigning maxās number to final(which was an unnecessary variable)ā¦so when it was looping it was comparing Gās value to max=1, not finalā¦should have noticed that earlierā¦& thanks for your advice alsoā¦will remember it in futureā¦
my output was going like this-
input: 6
6,5,4,3,2,1
output:1
this was happening because every time G was being inputted it was being compared with 1ā¦first itās okayā¦but the 2nd time it needed to compared with 6 right?..as the first input was 6ā¦but it wasnāt doing thatā¦because we had assigned the previous Gās value in final whereas it was to be assigned as maxās valueā¦thatās why at the end the output was 1 whereas it should have been 6ā¦so as u said a freaking comparison ruined my dayā¦