Thread: Why this program show me unlimited number of ans?

  1. #1
    Registered User abhijitnumber1's Avatar
    Join Date
    Jun 2012
    Location
    kolkata
    Posts
    5

    Talking Why this program show me unlimited number of ans?

    When i run this program it show me unlimited number of same ans.
    But The ans are correct.
    so, how can i avoid this problem. thanks.

    Code:
    /*Write a program that will finds the largest in a series ofnumbers entered by the user. The program must prompt the 
    user to enter one by one. When the user enters 0 ,
    the program must display the largest nonnegative number entered:
        */
    //CREATED BY: ABHIJIT DAS
    //DATE:1.7.2012
    //VER: 1.0
    
    
    #include<stdio.h>
    #include<conio.h>
    main()
    
    
    {
         float a, b, c;
         printf("Enter a number:\n");
         scanf("%f", &a);
         while(a == 0)
         {
                 printf("You must enter a number. Please enter a number\n");
                 scanf("%f", &a);
                 }
                 if(a != 0)
                 {
                      c = +a;
                      }
                      printf("Enter a number:\n");
                      scanf("%f", &b);
                      if(b == 0)
                      {
                           printf("The largest number is %f\n", c);
                           }
                           while(a> 0 && b>0)
                           {
                                    if(b!= 0 && b>c)
                                    {
                                           c = +b;
                                           }
                                           printf("Enter a number:\n");
                                           scanf("%f", &a);
                                           while(a == 0)
                                           {
                                                printf("The largest number is :%f", c);
                                                }
                                           if(a != 0 && a>c)
                                           {
                                                c = +a;
                                                }
                                                printf("Enter a number:\n");
                                                scanf("%f", &b);
                                                }
                                                while(a == 0 || b == 0)
                                                {
                                                        printf("The largest number is %f\n", c);
                                                        }
                                                        
                                                        getch();
                                                        return 0;
                                                        }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    1st, indent your code better.

    2nd, does this look at all problematic?
    Code:
    printf("Enter a number:\n");
    scanf("%f", &a);
    while(a == 0)
    {
        printf("The largest number is :%f", c);
    }
    
    ...
    
    while(a == 0 || b == 0)
    {
        printf("The largest number is %f\n", c);
    }
    3rd:
    Code:
    printf("Enter a number:\n");
    scanf("%f", &a);
    while(a == 0)
    {
        printf("You must enter a number. Please enter a number\n");
        scanf("%f", &a);
    }
    if(a != 0)
    {
        c = +a;
    }
    The if test is redundant, upon reaching that point in the program a is guaranteed to not be equal to 0.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Show a number in the MessageBox
    By Coding in forum Windows Programming
    Replies: 17
    Last Post: 01-27-2008, 03:01 PM
  2. not show my program on taskbar
    By ReXXuSS in forum C Programming
    Replies: 12
    Last Post: 08-19-2006, 03:27 AM
  3. Program doesn't show up
    By Krasimir11 in forum C Programming
    Replies: 7
    Last Post: 01-05-2006, 06:21 PM
  4. Show random number
    By q(-^-)P in forum C++ Programming
    Replies: 12
    Last Post: 09-01-2003, 06:50 AM
  5. Program to show the number of occurrences of words...
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 01-26-2002, 06:44 PM

Tags for this Thread