vendredi 6 décembre 2019

I have program the find the largest number of the user input,

I wrote a program that ask the user to input numbers to find the largest one between them. When the user inputs -999 the program will end and display the largest between them.

I have used a do-while loop and it works very well but when it comes to display the largest the output will be 8.

I defined the array as number[0] since I don't know how many numbers the user will input.

This is my code:

#include <stdio.h>
#include <stdlib.h>


int main()
{
        printf("Program to find the largest number\n");

        int num[0],i,large;

        do{
            printf("Please enter a number (type -999 to stop) :");
            scanf("%d",&num[i]);



        }while(num[i] != -999);

        for(i=0;i<num[i];i++){
            if(num[i]>num[i+1])
                large=num[i];
        }

        printf("Answer the largest number is: %d",large);
        printf("\nThank you\n\n");
}

After I run it the output is:

Answer the largest number is: 8
Thank you

Any idea?

Aucun commentaire:

Enregistrer un commentaire