Oct 8, 2013

C++ program to find the sum and average of given numbers using for loop

Unknown | 10:19 AM | 3 Comments so far
The for loop is the most commonly used statement in C++. This loop consists of three expressions. The first expression is used to initialize the index value, the second to check whether or not the loop is to be continued again and the third to change the index value for further iteration.
                         
Sometimes, the operations carried out by the while loop can also be done by using the for loop.
However, it is the programmer who has to decide which loop to be used in a given situation.

The syntax of the for loop is:
for (expression_1; expression_2; expression_3)
statement;
Now I am going to tell you a C++ program using the for loop. This program to find the sum and average of given numbers. The C++ program code is following bellow




// A program to find the sum and average of given numbers
#include <iostream.h>
void main (void)
{
int n;
cout <<"How many numbers ?\n";
cin >>n;
float sum = 0;
float a;
for (int i = 0; i <= n-1; ++i) {
cout <<"Enter a number \n";
cin >>a;
sum = sum+a;
}
float av;
av = sum/n;
cout <<"sum = " << sum << endl;
cout <<" Average = " << av << endl;
}
Output the above program

How many numbers ?
4
Enter a number
3
Enter a number
2
Enter a number
1
Enter a number
4
sum = 10
Average = 2.5
Read more ...

Oct 1, 2013

Display the name of the week with C++ program

Unknown | 2:37 PM | Be the first to comment!
Hi,
Today I am going to give you a simple C++ program. This following program structures display the name of the week, depending upon the number entered through the keyboard using the switch-case statementThe switch statement is a special multi way decision maker that tests whether an expression matches one of the numbers of constant values, and braces accordingly.




However, Type this following code on Turbo C++ and run your program.



// A program to display the name of the week, depending upon the number entered through the keyboard
#include <iostream.h>
void main (void)
{
int day;
cout << "Enter a number between 1 to 7 \n";
cin >>day;
switch (day) {
case 1 :
cout <<"Monday \n";
break;
case 2 :
cout <<"Tuesday \n";
break;
case 3 :
cout <<"Wednesday \n";
break;
case 4 :
cout <<" Thursday \n";
break;
case 5 :
cout <<" Friday \n";
break;
case 6 :
cout <<" Saturday \n";
break;
case 7 :
cout <<" Sunday \n";
break;
} // end of switch statement
} // end of main program

Output of the above program:

Enter a number between 1 to 7
4
Thursday

If you face any problem please inform us by commenting. We always ready to serve you.
Thanks! 
Read more ...
Twitter Delicious Facebook Digg Stumbleupon Favorites More

Search