Maximum

Whats wrong with this code? And what should I do?

#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;
}

You have already been answered @iker_13 .
Read this.

What are the things should I keep in mind to solve this problem?
This is by far the hardest one I have faced.
Suggestions pleaseā€¦

Is this code even right/correct?

#include<stdio.h>
int main()

{
    int n,i, lar;
    scanf("%d%d",&n,&i);
    for(i<1;i=1;i++);
    if(n>lar)
        n=lar;
    {
        printf("%d",lar);
    }

    return 0;
}

Toph shows CPU limit exceeded

@iker_13 Here are some pointers based on your previous code that you shared here:

  1. Do not print anything other than the answer. No ā€œEnter total number of elements\nā€, no extra texts.

  2. 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.

  3. Print just the largest number at the end as printf("%d\n", largest);. Do not print anything else.

You should only read the value of n here.

Next, fix the the loop: it should start with i = 0 and run while i < n.

Inside the loop, scanf a number and compare it with your current largest. This will then run n times inside the loop.

#include<stdio.h>
int main()
{
    int i,num[99],n;
    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
        scanf("%d",&num[n]);
    }
    int max=num[0];
    for(i=1;i<n;i++)
    {
        if (max<num[i]);
        max=num[i];
    }
    printf("%d\n",num[i]);
    return 0;




}

whats wrong in here?

Looks like you figured it out already.

Itā€™s amazing how much an extra semicolon can mess up. :slight_smile:

numbers=list(map(int,input().split()))
print(max(numbers))

what was the problem??it return output on my editor correctly but when i submit the code they say wrong answer

@tania.238473 The statement says:

The first line of the input will contain N

Here N is the number of integers that you will have read from the next line and see which one is the maximum among them.

Your code is reading just N (from the first line), and not the actual N numbers (from the second line) that you need to compare.

#include<stdio.h>
int main()
{
int a,b,c,d;
scanf(ā€œ%d\nā€,d);
scanf(ā€œ%d %d %dā€,&a,&b,&c);
if(a>b && a>c ){
printf(ā€œ%dā€,a);
}
else if(b>a && b>c){
printf(ā€œ%dā€,b);
}
else if(c>b && c>a){
printf(ā€œ%dā€,c);
}
}

what is my problem

#include <stdio.h>

int main()
{
unsigned int G,N,max=1,final;
int b;
scanf(ā€œ%dā€,&N);
for(b=1;b<=N;b++)
{
scanf(ā€œ%dā€,&G);
if(max>G)
final=max;
else
final=G;
}
printf(ā€œ%dā€,final);
return 0;
}

works just fineā€¦but itā€™s still showing errorā€¦a little help please??

@SonicTitan03
What will happen if A == B == C ?
Your code will not show any output.
Try how can you fix this.

And there can be more or less than 3 numbers in the input.

you are using unsigned int but printing int.
This might be the reason.

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ā€¦:slightly_smiling_face:

Initializing max = 1 is not a problem.
Cause all values are actually greater than 0,
So, 1 is the lowest value here.

However, the wrong comparison can be.

Actually what I was saying wasā€¦for this piece of code-

if(max>G)
final=max;
else
final=G;
}
printf(ā€œ%dā€,final);

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ā€¦:slight_smile:so as u said a freaking comparison ruined my dayā€¦

Well, if it works now then itā€™s fine I think. :slightly_smiling_face:
Thatā€™s what the community is for.
Again, Welcome to the Toph Community.

1 Like

Thanks a thousand times brotherā€¦:smiling_face_with_three_hearts:

1 Like
#include<stdio.h>
int main()
{
    int a,b,c,d;
	scanf("%d\n",d);
    scanf("%d %d %d",&a,&b,&c);
    if(a>b && a>c ){
        printf("%d",a);
    }
    else if(b>a && b>c){
        printf("%d",b);
    }
    else if(c>b && c>a){
        printf("%d",c);
    }
	else{
	    printf("All are equal.");	
	}
} 

problem ???