Module-6 C ++ Programs
Module-6 C ++ Programs
1
3. PROGRAM TO IN C++ TO DISPLAY THE MESSAGE IN THE FOLLOWING FORMAT:
/* To display the massage as in the following format*/
WELCOME
To
C++
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout <<"WELCOME\n";
cout <<"To\n";
cout <<"C \n";
getch();
}
Output:
WELCOME
To
C++
4. WRITE A PROGRAM IN C++ TO SUM OF TWO NUMBERS I.E. 200 AND 400:
/* To sum of two numbers*/
#include<iostream.h>
#include<conio.h>
void main()
{
int a , b, sum;
clrscr();
a =200, b=400;
sum = a + b;
cout <<"sum of two numbers is: << sum;
getch();
}
Output:
Sum of two numbers is 600
2
5. WRITE A PROGRAM IN C++ TO SUM OF THREE NUMBERS :
/*To sum of three numbers*/
#include<iostream.h>
#include<conio.h>
void main()
{
int a = 10, b = 20, c = 30, sum;
clrscr();
sum = a + b + c;
cout <<"sum of three numbers is: " << sum;
getch();
}
Output:
sum of three numbers is 60
4
9. PROGRAM TO CALCULATE ARITHMETIC FUNCTIONS AND AVEREAGE OF FOUR
NUMBERS:
/* Program to calculate arithmetic functions*/
#include <iostream.h>
#include <conio.h>
void main()
{
int a = 50, b = 40, c =30, d = 20, sum, sub, mul;
float div, avg;
clrscr();
sum = a + b + c + d;
sub = a - b;
mul = a * b * c * d;
div = a/b;
avg = (a + b + c + d)/4;
cout <<"sum is \n: "<< sum;
cout << "substraction is \n: " << sub;
cout << "product is \n:" << mul << endl;
cout <<"division is \n:" << div <<endl;
cout <<"average is \n:" << avg << endl;
getch();
}
6
Gross salary is
Total deduction is
Net salary is
Cout <<:
It is one of the features of C++ language. The cout << is an object oriented built in function. The
cout pronounced as ‘C out’ is a predefined object and it an output statement. The operator << is
called the insertion operator. The cout << included in iostream.h.
Syntax: cout << string;
Cin >>:
It is an input statement and causes the program to wait for the user to type in a number. The operator
>> is known as extraction operator. It is called predefined object. It is used to accept the values from
the key board into predefined variables. This is used to accept numeric, character and string type of
data. It is included in iostream.h .
Syntax: cin >> number 1;
======================================================================
12. PROGRAM TO ADD THREE NUMBERS BY USING INPUT:
/* Add Three Numbers*/
#include <iostream.h>
#include <conio.h>
void main()
{
int a, b, c, add;
clrscr();
cout <<"Enter a first number:\n";
cin >> a;
cout << "Enter a second number:\n";
cin >> b;
cout << "Enter a third number:\n";
cin << c ;
add = a + b + c;
cout << "sum of three numbers is :” << add;
getch();
}
ouyput:
Enter a first number
10
Enter a second number
20
Enter a third number
30
Sum of three number 60
7
13. PROGRAM TO MULTIPLY THREE NUMBERS BY USING INPUT:
/* Program to multiply three numbers*/
#include <iostream.h>
#include<conio.h>
void main()
{
int a, b, c, prod;
clrscr();
cout << "Enter a first number\n:”;
cin >> a ;
cout << "Enter a second number\n:”;
cin >> b ;
cout << "Enter a third number\n:”;
cin >> c ;
prod = a * b * c;
cout << "Product of three numbers is \n:" << prod ;
getch();
}
output:
Enter a first number
2
Enter a second number
4
Enter a third number
5
Product of three numbers is 40
8
Output:
Enter a number
6
Square of number is 36
16. PROGRAM TO FIND SQUARE, CUBE, FOURTH AND FIFTH A NUMBER BY USING
INPUT:
/* Program a find square, cube, fourth and fifth a number */
#include <iostream.h>
#include <conio.h>
void main()
{
int n, square, cube, fourth, fifth;
clrscr();
cout << “ Enter a number \n";
cin >> n;
square = n * n;
cube = n * n * n;
fourth = n * n * n * n;
fifth = n * n * n * n * n;
cout << “ squre is: “ << square;
cout << “ cube is: “ << cube;
cout << “ fourth is: “ << fourth;
cout << “ fifth is: “ << fifth;
getch();
}
Input:
Enter a number
5
Output:
square is 25
cube is 125
fourth is 625
fifth is 3125
9
17. PROGRAM TO FIND AREA OF A CIRCLE:
/* Program to find area of a circle*/
#include<iostream.h>
#include<conio.h>
void main()
{
int radius;
float circle;
clrscr();
cout << “ Enter a radius : \n" ;
cin >> radius ;
circle = 3.142 * radius * radius;
cout << “ Area of a circle is : “ << circle;
getch();
}
23. PROGRAM TO FIND AREA OF CIRCLE, CIRCUMFERENCE, RECTANGLE AND TRIANGLE IN A SINGLE PROGRAM :
/* Program find area, circumference of circle, rectangle and triangle */
#include <iostream.h>
#include <conio.h> a
void main()
{
float r, circle, cir, l, b, base, height, rectangle, triangle;
clrscr()
12
cout << "Enter a radius:\n";
cin >> r ;
cout << "Enter length:\n" ;
cin >> l ;
cout << "Enter breadth:\n" ;
cin >> b ;
cout << "Enter height:\n" ;
cin >> height ;
cout << "Enter base: \n" ;
cin >> base ;
circle = 3.142 * r * r;
cir = 2 * 3.142 * r;
rectangle = l * b ;
triangle = 0.5 * base * height;
cout << "Area of circle is: \n" << circle ;
cout << "Area of circumference is: \n" << cir ;
cout << "Area of rectangle is: \n" << rectangle ;
cout << "Area of triangle is: \n" << triangle ;
getch();
}
13
25. PROGRAM TO CALCULATE COMPOUND INTEREST:
/* Program to calculate compound interest */
#include <iostream.h>
#include <conio.h>
# include <math.h>
void main()
{
int p, t, r;
float ci, amt;
clrscr();
cout << "Enter principal amount: \n" ;
cin >> p ;
cout << "Enter time : \n" ;
cin >> t ;
cout << "Enter rate of interest:\n" ;
cin >> r ;
ci = p * pow ( ( 1 + r / 100.0 ) , t );
cout << "compound interest is: \n" << ci ;
getch();
}
14
ci = p * pow ( ( 1 + r / 100.0 ) , t );
cout << "simple interest is : \n" << si ;
cout << "compound interest is: \n" << ci ;
return();
}
15
29. PROGRAM TO SOLVE 2 X 2 + 3 X Y + 1:
/*Program to solve 2 x 2 + 3 x y + 1 */
#include <iostream.h>
#include <conio.h>
main()
{
int x, y, z;
clrscr();
cout << "Enter any 2 numbers: \n" ;
cin >> x >> y ;
z = 2 *( x * x ) + 3 * ( x * y ) + 1;
cout << "the z value is : \n" << z ;
getch();
}
16
31. PROGRAM TO FIND FUTURE ANNUITY:
/*future annuity*/
#include<iostream.h>
#include<math.h>
void main()
{
int a, r, n, r1;
float fa;
clrscr();
r1 = r / 100;
cout << "enter the installment amt:\n" ;
cin >> a ;
cout << "enter the rate of interest : \n" ;
cin >> r ;
cout << "enter number of years: \n" ;
cin >> n ;
fa = a / r1 * pow ( ( 1 + r1 ) , n -1);
cout << "future annuity is: \n" fa ;
getch();
}
_________________________________________________________________________
IF STATEMENT:
An if – statement is a single selection or decision statement. When a set of statements have to be
executed when an expression is evaluated to true (non-zero value)
or when a set of statements have to be skipped when an expression is evaluated to false (zero), then
if – statement is used. It is used when we have only one alternative. Hence, it is also called one-way
decision / selection statement.
Syntax: if ( condition)
statement True executed;
if ( condition)
statement 1;
IF--- ELSE STATEMENT:
If one set of activities have to be performed when an expression is evaluated to true and another set
of activities have to be performed when an expression is evaluated to false, then if- else –statement is
used. The if- statement is a simple selection / decision statement that is used when we must choose
between two alternatives. Hence, it is also called two-way decision / selection statement.
Syntax: if (condition)
{
Statement-1;
}
else
{
Statement-2;
}
-------------------------------------------------------------------------------------------------------------------------
17
32. PROGRAM TO FIND BIGGEST AMONG TWO NUMBERS:
/*Program to find biggest among two numbers*/
#include<iostream.h>
#include<conio.h>
void main()
{
int a, b;
clrscr ();
cout << "Enter a first number\n" ;
cin >> a ;
cout << "Enter a second number\n" ;
cin >> b ;
if ( a > b )
cout << " a is big \n: " ;
if ( b > a )
cout << " b is big\n: " ;
getch();
}
18
cout << "b is big number: \n" ;
else
cout << "c is big number:\n" ;
getch();
}
19
else
cout << "b is small\n" ;
getch();
}
20
cout << "Enter any four numbers\n" ;
cin >> a >> b >> c >> d ;
if ( a < b && a < c && a < d )
cout << "a is small \n" ;
else
if ( b < a && b < c && b < d )
cout << "b is small \n" ;
else
if ( c < a && c < b && c < d )
cout << "c is small \n" ;
else
cout << "d is small \n" ;
getch();
}
# include <conio.h>
void main()
{
int a ;
clrscr();
cout << "Enter a number\n" ;
cin >> a ;
if ( a > 0 )
cout << a is positive\n" ;
else
cout << "a is negative\n" ;
getch();
}
21
void main()
int age ;
clrscr();
cout << "Enter your age\n" ;
cin >> age ;
If ( age> = 18)
cout << "Eligibility to vote\n" ;
else
cout << "Not eligibility to vote\n" ;
getch();
}
22
{
int age ;
clrscr();
cout << "enter your age\n" ;
cin >> age ;
if ( age > = 18)
cout << "Eligible to write CET examination\n" ;
else
cout << " Not eligible to write CET examination\n" ;
getch ();
}
42. PROGRAM TO FIND ELIGIBILITY TO MAT EXAMINATION. MARKS SHOULD BE MORE THAN 500 OR EQUAL:
/*Program to find eligibility to write MAT exam & Marks should be more than 500 or equal*/
# include < iostream.h>
# include < conio.h>
void main()
clrscr();
23
43. PROGRAM TO FIND CONDITIONAL MARKS CARD / STATEMENT OF A STUDENT.
INPUT: STUDENT NAME, REGISTER NUMBER, SIX SUBJECTS MARKS.
FIND TOTAL, AVERAGE AND RESULT (GRADE):
/* Program to prepare conditional marks card of a student */
# include < iostream.h>
# include < conio.h>
void main()
{
char name [20];
int regnum, s1, s2, s3, s4, s5, s6, sum;
float avg;
clrscr ();
cout << "Enter Student Name\n" ;
cin >> name ;
cout << "Enter student Register Number \n" ;
cin >> regnum ;
cout << "Enter first subject marks\n" ;
cin >> s1 ;
cout << "Enter second subject marks\n" ;
cin >> s2 ;
cout << "Enter third subject marks\n" ;
cin >> s3 ;
cout << " Enter fourth subject marks\n" ;
cin >> s4 ;
cout << "Enter firth subject marks\n" ;
cin >> a5 ;
cout << "Enter sixth subject marks\n" ;
cin >> s6 ;
sum = s1 + s2 + s3 + s4 + s5 + s6;
avg = sum / 6;
cout << "Student name is \n" << name ;
cout << "Register Number \n" << regnum ;
cout << "Total Marks \n" << sum ;
cout << "Average Marks \n" << avg ;
if ( s1 < 40 || s2 < 40 || s3 < 40 || s4 < 40 || s5 < 40 || s6 < 40)
24
cout <, "Fail\n" ;
else
if ( avg> = 75 )
cout << "Distinction\n" ;
else
if ( avg > = 60 && avg < 75 )
cout << "First class \n" ;
else
if ( avg> = 50 && avg < 60 )
cout << "Second class\n" ;
else
cout << " Pass\n" ;
getch();
}
For Statement:
This is one of the looping or branching statements. A for loop is a control statement using
which the programmer can give instructions to the computer to execute a set of statements
repeatedly for a specified number of times. Since it is required to specify how many times a
set of statements have to be executed, it is also called counter-controlled loop. Once the
specified number of times the loop is executed, the control comes out of the loop.
Syntax: for (initialization; test-condition; increment/decrement)
{
Statements;
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
32
clrscr();
for ( i = 1; i < = 10 ; i ++ )
{
cout << "Enter the 10 Numbers \n: " ;
cin >> a ;
if (a < small )
small = a ;
}
cout << “ the smallest among 10 numbers is : “ << small ;
getch ();
}
-------------------------------------------------------------------------------------------------------------------------------
34
cin >> a [ i ] ;
}
cout << “ sum: “ << sum ;
getch ();
}
-------------------------------------------------------------------------------------------------------------------------
64. Program to find 3*3 Matrix using 2 Dimensional Arrays:
include <iostream.h>
include<conio.h>
void main()
{
clrscr();
int a[3] [3] ;
int i, j;
cout << "Enter Array elements\n: " ;
for ( i = 1; i < = 3 ; i = i + 1 )
{
for ( j = 1 ; j < = 3 ; j = j + 1 )
{
cin >> a [ i ] [ j ] ;
}
}
cout << "The Matrix is\n: " ;
for ( i = 1 ; i < = 3; i = i + 1 )
{
for ( j = 1 ; j < = 3 ; j = j + 1 )
{
cout << a [ i ] [ j ] ;
}
cout << “ \n” ;
}
getch();
}
------------------------------------------------------------------------------------------------------------------------
35
65. Program to find 5*5 Matrix using 2 Dimensional Arrays:
include <iostream.h>
include<conio.h>
void main()
{
clrscr();
int a[5] [5] ;
int i, j;
cout << "Enter Array elements\n: " ;
for ( i = 1; i < = 5 ; i = i + 1 )
{
for ( j = 1 ; j < = 5 ; j = j + 1 )
{
cin >> a [ i ] [ j ] ;
}
}
cout << "The Matrix is\n: " ;
for ( i = 1 ; i < = 5; i = i + 1 )
{
for ( j = 1 ; j < = 5 ; j = j + 1 )
{
cout << a [ i ] [ j ] ;
}
cout << “ \n” ;
}
getch();
}
------------------------------------------------------------------------------------------------------------------------
66. Program to find Quantity Sold of Pens and Pen Cost and Find Total Amount:
include<iostream.h>
include<conio.h>
void main()
{
int qty, price, tamount;
clrscr();
cout << "Enter Quantity sold\n" ;
cin >> qty ;
36
cout << "Enter the price of pen\n" ;
cin >> price ;
tamount = qty * price;
cout << “total amount of pen is\n: ” << tamount ;
getch();
}
--------------------------------------------------------------------------------------------------------------------------
37