Home Forums C Programming while statement again!

Viewing 1 reply thread
  • Author
    Posts
    • #2165
      SilviaJamieson
      Participant

      Hi everyone,

      Did I do something wrong? I am curious as to why no-one has attempted to point me in the right direction??

      I have to write a C program that finds the average of a list of 5 numbers using while-statement.
      „ Use scanf function to read 5 numbers from keyboard
      „ Use printf function to display the average of a list of numbers on the screen
      „ After displaying the average value, the program should ask user if he/she wants to find the average of another list of numbers

      I have downloaded many tutorials and i find them very difficult to follow, it is kind of like reading a foreign language, i know that most people are probably laughing thinking how simple this is, i will get there eventually!

      Best wishes ……daisy :oops:

    • #3491
      GWILouisaxwzkla
      Participant

      could do:


      /****************************************************************
      * File Name : c:programstempCG.cpp
      * Date : December,7,2008
      * Comments : new project
      * Compiler/Assembler :
      * Modifications :
      *
      *
      *
      *
      *
      * Program Shell Generated At: 2:59:36 p.m.
      =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/


      #include
      //#include
      //#include
      //#include
      //#include
      //#include

      //using namespace std;

      //main function ******************************

      int main ( )
      {

      char choice;
      int number1 , number2 , number3 , number4,number5;
      int average;
      do
      {
      printf ( "enter a number: " );
      scanf ( "%i" , & number1 );
      printf ( "enter a number: " );
      scanf ( "%i" , & number2 );
      printf ( "enter a number: " );
      scanf ( "%i" , & number3 );
      printf ( "enter a number: " );
      scanf ( "%i" , & number4 );
      printf ( "enter a number: " );
      scanf ( "%i" , & number5 );
      printf ( "nthe average of the numbers is: " );
      average = ( number1 + number2 + number3 + number4 + number5 ) / 5;
      getchar (); //get new line characters
      printf ( "%i" , average );
      printf ( " nDo you want to calculate another average ? ( y or n ) " );
      scanf ( "%c" , & choice );
      getchar(); //get newline
      while ( choice != 'y' && choice != 'n' )
      {
      printf ( "wrong input ( enter y or n ) " );
      printf ( " nDo you want to calculate another average ? ( y or n ) " );
      scanf ( "%c" , & choice );
      getchar (); //get newline
      }
      }while ( choice != 'n' );


      return 0 ;
      }





Viewing 1 reply thread
  • The forum ‘C Programming’ is closed to new topics and replies.