All programs in c++
********Some input output programs********
Q-1. My first program in c++ "welcome to the c++!".
/*my first program in c++ */
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<"Welcome to the c++!";
getch();
}
Q-2 WAP to take a value from the user and print on the screen.
#include<iostream.h>
#include<conio.h>
void main()
{
int a;
clrscr();
cout<<"Enter the value = ";
cin>>a;
cout<<"a = "<<a;
getch();
}
*********Operators in c++ *********
Arithmetic Operators
Relational Operators
Logical Operators
Assignment operators
Pointer operators
Special Operators
Bitwise operators
**Arithmetic operators**
Binary operator : - All the arithmetic operators are binary operators, since they operate
on two operands at a time.
Such as : -
-- Multiplication (*)
--Divison (/)
--Remainder (%)
--Addition (+)
--Subtraction (-)
Q 3: - WAP to take two values from user and perform all binary operations also print the
result on the screen.
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
cout<<"Enter the value of a and b : - ";
cin >>a>>b;
c=a+b; //Addition
cout<<"\nsum = "<<c;
c=a-b; //Substraction
cout<<"\nSub = "<<c;
c=a*b; //Multiplication
cout<<"\nmul = "<<c;
c=a/b; //Divison
cout<<"\nDiv = "<<c;
c=a%b; //Remainder
cout <<"\nRem = "<<c;
getch();
}
unary operator : - unary operators take just a single [Link] operand can be any
interger or floating point value.
Such as: -
-- Increament operators ( ++ ) Means add 1.
-Pre-increament operators
-Post-increament operators
-- Decreament operators ( - - ) Means Subtract 1.
-Pre-Decreament operators
-Post-Decreament operators
Q 4: -WAP to that perform unary operations like pre-increment ,post -increament,pre-
decreament,post - decreament operations on it and print the result side by side.
#include<iostream.h>
#include<conio.h>
void main()
{
int a=5,b=0;
clrscr();
b=b+(++a); //pre-increament b=0+(6) a=6,b=6
cout << "\n\nb = "<<b;
b=b+(a++); //post-increament b=6+(6) a=7,b=12
cout << "\n\nb = "<<b;
b=b+(--a); //pre -decreament b=12+(6) a=6,b=18
cout << "\n\nb = "<<b;
b=b+(a--); //post -decreament b=18+(6) a=7,b=24
cout << "\n\nb = "<<b;
getch();
}
Q 5 :- WAP to demonstrate the various increament and decrement operations.
#include<iostream.h>
#include<conio.h>
void main()
{
int a=0,b=0,c=0;
clrscr();
cout << "Initial value of a,b,c is "<<a<<" "<<b<<" "<<c;
a=(++b) + (++c);
cout<<"\n\n a = ++b + ++c = "<<a<<" "<<b<<" "<<c;
a=b++ + c++;
cout<<"\n\n a = b++ + c++ = "<<a<<" "<<b<<" "<<c;
a=++b + c++;
cout<<"\n\n a = ++b + c++ = "<<a<<" "<<b<<" "<<c;
a=b-- + c--;
cout<<"\n\n a = b-- + c-- = "<<a<<" "<<b<<" "<<c;
getch();
}
****Relational Operators****
The relational expressions give the value "1" for TRUE result and the value "0" for FALSE
result.
Such as: -
-- Greater than ( > )
-- Greater than or equal ( >= )
-- Less than (< )
-- Less than or equal ( <= )
-- Equal ( == )
-- Not equal ( != )
****Logical Operators****
The relational expressions give the value "1" for TRUE result and the value "0" for FALSE
result.
Such as :-
-- AND ( && )
-- OR ( || )
-- NOT ( ! )
****Assignment operators****
The assignment operator is represented by the equal sign (=).
Such as : -
a=a+b;
or
a+=b;
****Pointer Operator****
Address of operators ( & ) : - It is returns the address of the variable,where
the variable is stored in the main memory of the
computer.
Indirection operator ( * ) : - It is used to get the contents of the element or
cell.
Q-6: - WAP where pointer variable accept address of a variable and print the value of
variable using pointer as well as variable.
#include<iostream.h>
#include<conio.h>
void main()
{
int salary;
int *p;
salary=3000;
p=&salary;
cout<<"\n\nThe value of the salary is "<<salary;
cout<<"\n\nThe value pointed to is "<<*p;
*p=3333;
cout<<"\n\nThe value of salary is "<<salary;
getch();
}
****Special operators****
It is different kind of operators.
such as : -
Conditional operator ( ?: )
Comma operator ( , )
Sizeof operator ( sizeof )
**Conditional operator**
Q-7 : - WAP where the value of the variable "b" is determined according to whether or not
the value of the variable "a" is greater than 25.
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
cout<<"\n\nEnter the value of the variable a : - ";
cin>>a;
b=a>25 ? 100 : 99;
cout<<"\n\nThe value of the variable b is "<<b;
getch();
}
**Comma operator**
Q-8:- WAP to assign a value to a variable through second variable and change second
variable value simultanously using comma operator.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int a,c=5;clrscr();
(a=c,c=7) ;
cout<<"value of a is "<<a;
cout<<"\n\nvalue of c is "<<c;
getch();
}
**Sizeof operator**
Q-9:- WAP to determine the size of int variable,character variable,and float variable.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int a;
char b;
float c;
clrscr();
cout<<"sizeof a is "<<sizeof(a);
cout<<"\n\nsizeof b is "<<sizeof(b);
cout<<"\n\nsizeof c is "<<sizeof(c);
getch();
}
*********Conditional statements*********
Conditional branching , aprogram is to decide whether or not to execute the next statement based
on the resultant value of the current [Link] example, go to 2nd year,if you pass 1st year
exams,else repeat 1st year.
If-Else statement
Multiple if statements
The if-else statement
Nested if-else statements
The switch statement
****If-Else statement****
In the if statement whenever evaluated condition comes out to be true,then the action s are
carried [Link], the given set of actions are ignored.
=> if (expression)
{
statement_1;
}
In the if-else statement whenever evaluated condition comes out to be true,then the action s are
carried [Link], the set of actions of else part will be execute.
=> if (expression)
{
statement_1;
}
else
{
statement_2;
}
Q 10: - WAP to check number is even or odd.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout << "\n\nEnter the number : - ";
cin >> num;
if(num%2==0)
{
cout<<"\n\nThe number is even";
}
else
{
cout<<"\n\nThe number is odd";
}
getch();
}
Q 11:- WAP to input two values and compare which is greater.
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
cout<<"\n\nEnter two values : - ";
cin>>a>>b;
if(a>b)
{
cout<<"a is greater";
}
else
{
cout<<"b is greater";
}
getch();
}
Q-12: - WAP to take three values and compare which one greater.
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
cout<<"\n\nEnter the three values : - ";
cin>>a>>b>>c;
if(a>b && a>c)
{
cout<<"\n\na is greater";
}
else if(b>c)
{
cout<<"\n\nb is greater";
}
else
{
cout<<"\n\nc is greater";
}
getch();
}
Q13:- Wap to calculate bonus,if the number of working years of employeeis greater then or
equal to 3
then a bonus of rs. 2500 is given to the employee.
#include<iostream.h>
#include<conio.h>
void main()
{
int yoj,cy,yos;
clrscr();
cout<<"\nEnter the current year : - ";
cin>>cy;
cout<<"\n\nEnter the year of joining : -";
cin >>yoj;
yos=cy-yoj;
if(yos>=3)
{
cout<<"\n\nYou will get bonus of rs 2500";
}
else
{
cout<<"\n\nSorry no bonus";
}
getch();
}
Q 14 : - WAp to input a character to confirm whether it is an alphabetic or not and if it is
an alpkabetic then check it is in lower case or upper case.
#include<iostream.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
cout<<"\nEnter the alpahbetic character : - ";
cin>>ch;
if(ch>64&&ch<91)
{
cout<<"\n\nThe alphabet is a upper case letter";
}
else if(ch>96&&ch<124)
{
cout<<"\n\nThe alphabet is a lower case letter";
}
else
{
cout<<"\n\nThis is an alphabetic character";
}
getch();
}
Q15 : - WAP to calculate the percentage of total marks of student in five subjects and if
percentage grade
less then 50 fail
>=50 and <60 d
>=60 and <70 c
>=70 and <80 b
>80 a
#include<iostream.h>
#include<conio.h>
void main()
{
float marks;
float per;
clrscr();
cout<<"\nEnter the total marks of the student: - ";
cin>>marks;
per=(marks*100)/500;
if(per<50)
{
cout<<"\n\nFail";
}
else if(per>=50 && per<60)
{
cout<<"\n\ngrade d";
}
else if(per>=60 && per<70)
{
cout<<"\n\ngrade c";
}
else if(per>=70&&per<80)
{
cout<<"\n\ngrade b";
}
else
{
cout<<"grade a";
}
****Switch statement****
Instead of using the if-else-if ladder, the Switch statement is available for handling multiple
choice.
=> switch(variable)
{
case constant1 :
statement;
break;
case constant2 :
statement;
break;
case constant3 :
statement;
break;
.........
.........
.........
defalult :
statement;
}
Q 16: - WAP to input a number from 1 to 7 and display the name of the day(monday to
sunday).
#include<iostream.h>
#include<conio.h>
void main()
{
int num;
clrscr();
cout<<"\nEnter the number between 1 to 7: - ";
cin>>num;
switch(num)
{
case 1:
cout<<"\n\nmonday";
break;
case 2:
cout<<"\n\ntuesday";
break;
case 3:
cout<<"\n\nwednusday";
break;
case 4:
cout<<"\n\nthursday";
break;
case 5:
cout<<"\n\nfriday";
break;
case 6:
cout<<"\n\nsaturday";
break;
case 7:
cout<<"\n\nsunday";
break;
default:
cout<<"please enter between 1 to 7";
}
getch();
}
Q:-17:- WAP to make a calculator and perform arithmetic operations like
addition,substraction,multiplication,division.
#include<iostream.h>
#include<conio.h>
void main()
{
int ch,num1,num2,result;
float result1;
clrscr();
cout<<"\n\[Link]\n\[Link]\n\[Link]\n\[Link]";
cout<<"\n\nEnter your choice : -";
cin>>ch;
cout<<"\n\nEnter number 1 :- ";
cin>>num1;
cout<<"\n\nEnter number 2: -";
cin>>num2;
switch(ch)
{
case 1:
result=num1+num2;
cout<<"\n\nsum of numbers : - "<<result;
break;
case 2:
result=num1-num2;
cout<<"\n\nsub of numbers : - "<<result;
break;
case 3:
result=num1*num2;
cout<<"\n\nmultiplication of numbers : - "<<result;
break;
case 4:
result1=(float)num1/num2;
cout<<"\n\ndivision of numbers : - "<<result1;
break;
default:
cout<<"please enter between 1 to 4";
}
getch();
}
*********Loop constructor*********
The loop or iteration consruct,directs a program to perform a set of operations again and again
until
a specified condition is [Link] condition causes the termination of the loop.
For loop
While loop
Do-while loop
****For loop****
For loop consruct is used to execute a set of statements for a given nuber of [Link] is a
shorthand
method for executing statements in a loop.
=> for(initial condition;test condition;incrementer or decrementer)
{
statement 1;
statement 2;
}
Q 18: -WAP to printing "hello world" 4 times.
#include<iostream.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=0;i<4;i++)
{
cout<<"hello world\n\n";
}
getch();
}
Q 19:- WAP to print 1 to 10 number.
#include<iostream.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
cout<<i<<"\n\n";
}
getch();
}
Q 20: -WAP to input a number at show the table of that number.
#include<iostream.h>
#include<conio.h>
void main()
{
int i,num,a;
clrscr();
cout<<"\n\nEnter the number : -";
cin>>num;
for(i=1;i<=10;i++)
{
a=num*i;
cout<<"\n\n"<<num<<"x"<<i<<"="<<a;
}
getch();
}
Q 21:- WAP to obtain the factorial of a number entered by user.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int fact=1,i,num;
cout << "\n\nEnter the number : - ";
cin >> num;
for(i=1;i<=num;i++)
{
fact=fact*i;
}
cout<<"factorial of a number : - "<<fact;
getch();
}
Q 22: - WAP to check no is prime or not.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num,i;
cout << "\n\nEnter the number : - ";
cin >> num;
for(i=2;i<num;i++)
{
if(num%i==0)
{
cout<<"\n\nThe number is not prime";
break;
}
}
if(num==i)
{
cout<<"\n\nNumber is prime.";
}
Q 23:- WAP to obtain the following outputs.
a). Fibonacci series : - 1 2 3 5 8 13 21 34
b). Gap series : - 0 1 3 6 10 15 21
c). Exponential series : - x - x^2+x^3-x^4+x^5-x^6
--- --- --- --- ---
2! 3! 4! 5! 6!
d). Prime no series : - 2 3 5 7 11
Ans: -
a).
#include<iostream.h>
#include<conio.h>
void main()
{
int size,a=0,b=1,c,i;
cout<< "\n\nEnter the size of the series : -";
cin >> size;
cout << "\n\n\t\t\t";
for(i=0;i<size;i++)
{
c=a+b;
cout <<" "<< c;
a=b;
b=c;
}
getch();
}
b).
#include<iostream.h>
#include<conio.h>
void main()
{
int size,a=0,b=0,c,i;
cout<< "\n\nEnter the size of the series : -";
cin >> size;
cout << "\n\n\t\t\t";
for(i=1;i<=size;i++)
{
c=a+b;
cout <<" "<< c;
b++;
a=c;
}
getch();
}
c).
#include<iostream.h>
#include<conio.h>
void main()
{
int size,a=0,b=0,c,i;
cout<< "\n\nEnter the size of the series : -";
cin >> size;
cout << "\n\n\t\t\t";
for(i=1;i<=size;i++)
{
c=a+b;
cout <<" "<< c;
b++;
a=c;
}
getch();
}
d).
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int j,num,i,size;
cout << "\n\nEnter the size : - ";
cin >> size;
for(i=2;i<size;i++)
{
for(j=2;j<size-1;j++)
{
if(i%j==0)
{
break;
}
}
if(i==j)
{
cout<<" "<<i;
}
}
getch();
}
Q 24: - WAP to obtain the folloeing outputs,
a). * b). * c). **** d). * * * *
** ** *** ***
*** *** ** **
**** **** * *
e). 1 f). A B C D C B A g). A h). 1
1 2 1 ABCBA BB 12
12321 ABA CCC 123
1234321 A BB 12
A 1
Ans: -
a).
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size,j,i;
cout << "\n\nEnter the size of the series : - ";
cin >> size;
for(i=0;i<size;i++)
{
cout << "\n\t\t\t ";
for(j=0;j<=i;j++)
{
cout << "*";
}
}
getch();
}
b).
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size,j,i;
cout << "\n\nEnter the size of the series : - ";
cin >> size;
;
for(i=0;i<size;i++)
{
cout << "\n\t\t\t ";
for(j=i;j<size;j++)
{
cout << " ";
}
for(j=0;j<=i;j++)
{
cout << "*";
}
c).
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size,j,i;
cout << "\n\nEnter the size of the series : - ";
cin >> size;
;
for(i=0;i<size;i++)
{
cout << "\n\t\t\t ";
for(j=i;j<size;j++)
{
cout << "*";
}
}
getch();
}
d).
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size,j,i;
cout << "\n\nEnter the size of the series : - ";
cin >> size;
;
for(i=0;i<size;i++)
{
cout << "\n\t\t\t ";
for(j=0;j<i;j++)
{
cout<<" ";
}
for(j=i;j<size;j++)
{
cout << "*";
}
}
getch();
}
e).
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size,j,i,k;
cout << "\n\nEnter the size of the series : - ";
cin >> size;
for(i=1;i<=size;i++)
{
cout << "\n\t\t\t ";
for(j=i;j<size;j++)
{
cout<<" ";
}
for(j=1;j<=i;j++)
{
cout << j;
k=j;
}
for(j=1;j<i;j++)
{
k--;
cout<<k;
}
}
getch();
}
f).
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size,j,i,k=64;
cout << "\n\nEnter the size of the series : - ";
cin >> size;
for(i=0;i<size;i++)
{
cout << "\n\t\t\t ";
for(j=0;j<i;j++)
{
cout << " ";
}
for(j=i;j<size;j++)
{
k++;
cout << (char)(k);
}
for(j=i;j<size-1;j++)
{
k--;
cout<<(char) (k);
}
k=64;
}
getch();
}
g).
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size,j,i,k=64;
cout << "\n\nEnter the size of the series : - ";
cin >> size;
for(i=0;i<size;i++)
{
cout << "\n\t\t\t ";
for(j=0;j<i;j++)
{
cout << " ";
}
for(j=i;j<size;j++)
{
k++;
cout << (char)(k);
}
for(j=i;j<size-1;j++)
{
k--;
cout<<(char) (k);
}
k=64;
}
getch();
}
h).
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int size,j,i,k;
cout << "\n\nEnter the size of the series : - ";
cin >> size;
for(i=1;i<=size;i++)
{
cout << "\n\t\t\t ";
for(j=i;j<size;j++)
{
cout <<" ";
}
for(j=1;j<=i;j++)
{
cout<<(j);
}
}
for(i=1;i<size;i++)
{
cout << "\n\t\t\t ";
for(j=1;j<=i;j++)
{
cout << " ";
}
int k=1;
for(j=i;j<size;j++)
{
cout<<(k);
k++;
}
}
getch();
}
****While loop****
While loop construct contain the condition [Link] the condition is satisfied,the control
executes the statements following the while loop else,it ignores these statements.
=>While(condition)
{
statement1;
statement 2;
. . .. . . . .
}
Q 25: - WAP to input a five digit number and obtain the reverse of that number.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
long result=0,i,num,rem;
cout << "\n\nEnter the number : - ";
cin >> num;
while(num!=0)
{
rem=num%10;
result=(result*10)+rem;
num=num/10;
}
cout<<"reverse of a number : - "<<result;
getch();
}
Q 26: - WAP to input a number and obtain the sum of the digits.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
long num,rem,sum=0;
cout << "\n\nEnter the number : - ";
cin >> num;
while(num!=0)
{
rem=num%10;
sum+=rem;
num=num/10;
}
cout<<"Sum of digits : - "<<sum;
getch();
}