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 statement. The 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 ...

Sep 27, 2013

Find Prime number with C++ Program

Unknown | 4:10 PM | Be the first to comment!
Do you know what is a prime number?
A natural number greater than 1 which has only two divisors 1 and it is called a prime number. For example: 7 is a prime number. Because it has only two divisors 1 and 7 (itself).




Now I am going to tell you how to find a prime number by using C++ program. Just type these code below

//C++ Program to find Prime number
#include<iostream.h>
#include<conio.h>
        void main()
        {
         //clrscr();
         int number,count=0;
cout<<"Enter number to check it is Prime or not ";
          cin>>number;
           for(int a=1;a<=number;a++)
               {
                if(number%a==0)
                   {
                  count++;
                   }
               }
       if(count==2)
         {
          cout<<" PRIME NUMBER \n";
         }
       else
         {
          cout<<" NOT A PRIME NUMBER \n";
         }
       //getch();
       }

Thanks! 
Read more ...

Sep 17, 2013

C++ program to find the largest value among any four numbers

Unknown | 11:14 AM | Be the first to comment!
Today I am going to tell you how to find the largest value among any four numbers with a simple C++ program. You can also see how to find the largest value of any three numbers. 






Anyway, the C++ program code is bellow:


// A C++ program to find the largest value among any four numbers
#include <iostream.h>
void main(void)
{
 float a,b,c,d;
 cout << "Enter any four numbers \n";
 cin >>a >>b >>c >>d;
 if (a > b) {
if (a > c) {
if (a > d)
cout <<"Largest value" << a << endl;
else cout <<"Largest value" << d << endl;
}
else {
if (c > d)
cout <<"Largest value" << c << endl;
else cout <<"Largest value" << d << endl;
}
} // end of outer if part
else {
if (b > c) {
if (b > d)
cout <<"Largest value" << b << endl;
else cout <<"Largest value" << d << endl;
}
else {
if (c > d)
cout <<"Largest value" << c << endl;
else cout <<"Largest value" << d << endl;
}
} // end of outer else part
} // end of main program

Output:


Enter any four numbers 
10   40    50    70 
Largest value 70

Thanks! 
Read more ...

Sep 14, 2013

A C++ program to find the largest value of any three numbers

Unknown | 1:20 PM | Be the first to comment!
Now I am telling you how to find the largest value of any three numbers with C++ program.

If you enter any three numbers you will get the largest value from these. It's so simple and easy program.




However, Let's start our program. 

#include <iostream.h>
void main( void)
{
 float x,y,z;
 cout << "Enter any three numbers \n";
 cin >> x >> y >> z;
 if (x > y) {
if (x > z)
cout <<"Largest value" << x << endl;
else
cout <<"Largest value" << z << endl;
}
else if (y > z)
cout <<"Largest value" << y << endl;
else
cout <<"Largest value" << z << endl;
} // end of main program

You can see this output:

Enter any three numbers
10     40      50
Largest value 50
Read more ...

Sep 10, 2013

The program of compound interest in C

Unknown | 10:12 AM | Be the first to comment!
Today I am going to tell you how to get Compound Interest with C program. It is not very difficult. Very simple and easy program.

However, Let's start our program. Open your Turbo C program and enter the following codes:






#include <stdio.h>
#include <conio.h>
#include <math.h>
void main (){
float p, r, n, i, f;
clrscr ();
printf("Please enter a value for the principal (p) : );
scanf ("%f", &p);
printf("Please enter a value for the interest rate (r) : );
scanf ("%f", &r);
printf("Please enter a value for the number of Years (n) : );
scanf ("%f", &n);
i=r/100;
i=i+l
f=p*(pow (i,n));
printf ("\n The Final value (F) is : %. 3f",f);
getch ();
}



Then compile and run.

Thanks!
Read more ...

Sep 4, 2013

Simple program in C++

Unknown | 2:20 PM | Be the first to comment!
Today I am going to tell you how to create a simple C++ program.

Suppose we would like to display the message “Hello world! This is my first program” on your video screen.

#include <iostream. h>
void main()  // program heading
  {  //  begin
     cout  << "Hello world! This is my first program";
  }  //  end

The function main() must be placed before the begin statement. It invokes another function to perform its job. The { symbol or notation is used as the begin statement. The declaration of variables and the type of operations are placed after the begin statement. The end statement is denoted by the symbol  }. In C++ , the semicolon (;) serves the purpose of the statement terminator rather than a separator.

 Statements are terminated by a semicolon and are grouped within braces {….}. Most statement contains expression, sequences of operators, function calls, variables and constants that specify computation.

Variable and function names are of arbitrary length and consist of upper and lower case letters, digits and underscore and they may not start with a numeral. All C++ keywords are written in lowercase letters.
Any statement or functions within comments are not executable and they are ignored by the compiler.
Read more ...
Twitter Delicious Facebook Digg Stumbleupon Favorites More

Search