Do While Loop (Average Marks)

0 Gitesh Khanna · July 7, 2015
#include <stdio.h>
#include <stdlib.h>

int main()
{
    float marks = 0 ;
    float avg = 0 ;
    float test = 0 ;
    float MarksAddup=0 ;

    printf("Enter 0 to stop. \n\n") ;

    do
    {
        printf("Enter Marks here:  ") ;
        scanf("%f" , &marks) ;
        MarksAddup += marks ;
        test++ ;



    }while(marks!=0) ;

    avg = MarksAddup/test ;
    printf("Your Average Marks are : %.2f" , avg) ;


    return 0;
}






Whenever i enter 0 in the program, it stops but it still counts it as a test. So, if i perform 4 tests and then enter 0 , the program takes it as 5 tests.


It can be solved by adding


avg = MarksAddup/--test ;

in the program but, this wasnt a problem in the program that Bucky wrote.
What could be the problem?

Post a Reply

Replies

Oldest  Newest  Rating
0 Gitesh Khanna · July 9, 2015
@San De , thats the exact code i have written.

Btw.
I figured out what was happening, i was just confused in how the while loops work.

Thank you for all your replies though :D
0 San De · July 9, 2015
Something messed up.
This was what I was suggesting..

do
{
printf("Enter Marks here: ") ;
scanf("%f" , &marks) ;
MarksAddup += marks ;
if(marks!=0)
test++ ;



}while(marks!=0) ;
0 c student · July 8, 2015
you can easily initialize test as -1.  note that if test is 0, it will return a nan
  • 1

C

107,197 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator