Home › Forums › C Programming › while statement again!
- This topic has 1 reply, 2 voices, and was last updated 16 years ago by GWILouisaxwzkla.
- AuthorPosts
- December 5, 2008 at 3:44 am #2165SilviaJamiesonParticipant
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 numbersI 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:
- December 7, 2008 at 8:21 pm #3491GWILouisaxwzklaParticipant
could do:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970<br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : December,7,2008<br />* Comments : new project<br />* Compiler/Assembler :<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 2:59:36 p.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include <br />//#include <br />//#include <br />//#include <br />//#include <br />//#include <br /><br />//using namespace std;<br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />char choice;<br />int number1 , number2 , number3 , number4,number5;<br />int average;<br />do<br />{<br />printf ( "enter a number: " );<br />scanf ( "%i" , & number1 );<br />printf ( "enter a number: " );<br />scanf ( "%i" , & number2 );<br />printf ( "enter a number: " );<br />scanf ( "%i" , & number3 );<br />printf ( "enter a number: " );<br />scanf ( "%i" , & number4 );<br />printf ( "enter a number: " );<br />scanf ( "%i" , & number5 );<br />printf ( "nthe average of the numbers is: " );<br />average = ( number1 + number2 + number3 + number4 + number5 ) / 5;<br />getchar (); //get new line characters<br />printf ( "%i" , average );<br />printf ( " nDo you want to calculate another average ? ( y or n ) " );<br />scanf ( "%c" , & choice );<br />getchar(); //get newline<br />while ( choice != 'y' && choice != 'n' )<br />{<br />printf ( "wrong input ( enter y or n ) " );<br />printf ( " nDo you want to calculate another average ? ( y or n ) " );<br />scanf ( "%c" , & choice );<br />getchar (); //get newline<br />}<br />}while ( choice != 'n' );<br /><br /><br />return 0 ;<br />}<br /><br /><br /><br /><br /><br />
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.