0% found this document useful (0 votes)
234 views35 pages

Abdullah Shahid (0014)

The document contains 29 programs written in C++ to demonstrate different programming concepts. The programs cover basic input/output, operators, decision making, loops, functions, pointers and address of operators. Some key programs include: Program 1 to print a string, Program 2 to input two numbers, Program 3 to calculate circumference using define, Program 11 to print a pattern using loops, Program 23 to swap values using a user-defined function, Program 26 to display addresses of variables.

Uploaded by

Haris
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
234 views35 pages

Abdullah Shahid (0014)

The document contains 29 programs written in C++ to demonstrate different programming concepts. The programs cover basic input/output, operators, decision making, loops, functions, pointers and address of operators. Some key programs include: Program 1 to print a string, Program 2 to input two numbers, Program 3 to calculate circumference using define, Program 11 to print a pattern using loops, Program 23 to swap values using a user-defined function, Program 26 to display addresses of variables.

Uploaded by

Haris
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Program 1.

Write a program in c++ that will show the name on output screen.

#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
cout<<"University Of Central Punjab";
getch();
}

Program 2.
Write a program in c++ that input a number from user and
display that entered
Number.

#include<iostream.h>
#include<conio.h>
main()
{
int a,b;
clrscr();
cout<<"enter two num"<<endl;
cin>>a>>b;
cout<<"value of a="<<a<<endl;
cout<<"value of b="<<b<<endl;

getch();
}

Program 3.
Write a program in c++ that will make a constant by DEFINE
directive
and input radius from user and display the circumference by
using 2PiR formula.

#include<iostream.h>
#include<conio.h>
#define pi 3.14
main()
{
int r;
cout<<"enter the radius"<<endl;
cin>>r;
cout<<"circumference ="<<2*pi*r;
getch();
}

Program 4.
Write a program in c++ that will calculate the circumference of
circle by using piR2 formula.

#include<iostream.h>
#include<conio.h>
#define pi 3.14

main()
{
int r;
clrscr();
cout<<"enter the radius"<<endl;
cin>>r;
cout<<"circumference of circle="<<pi*r*r;
getch();
}

Program 5.
Write a program in c++ that input a number from user and
dispaly the square of that number.

#include<iostream.h>
#include<conio.h>
main()
{
int a;
clrscr();
cout<<"Enter a Number"<<endl;
cin>>a;
cout<<"Decimile Number="<<a<<endl;
cout<<"Square of Number="<<a*a;
getch();
}

Program 6.

Write a program in c++ that explains the difference of prefix and


postfix operator.

#include<iostream.h>
#include<conio.h>
main()
{
int a=20,b=20;
clrscr();
cout<<"First value="<<a<<endl;
cout<<"Perifix increment="<<a++<<endl;
cout<<"Postfix increament="<<++a<<endl;
cout<<"Second Value="<<b<<endl;
cout<<"Prefix Decreament="<<b--<<endl;
cout<<"postfix Decreament="<<--b;
getch();
}

Program 7.
Write a program in c++ that solve the following expression.
a*b/(-c*31%13)*d

#include<iostream.h>
#include<conio.h>
main()
{

(a=10, b=20, c=15, d=8)

int a=10,b=20,c=15,d=8,e;
clrscr();
cout<<"Display Expression (a*b/(-c*31%13)*d)"<<endl;
e=(a*b/(-c*31%13)*d);
cout<<e;
getch();
}

Program 8.
Write a program in c++ that inputs a character and display its
ASCII code.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
main()
{
int a;
clrscr();
cout<<"Enter a Number"<<endl;
cin>>a;
cout<<'%c'<<a;
getch();
}

Program 9.
Write a program in c++ that input the total bill and calculate the
5% Discount

on that bill.

#include<iostream.h>
#include<conio.h>
main()
{
int p1,p2,p3,p4,p5,tb,dis,ap;
clrscr();
cout<<"Enter the Price of Five Products"<<endl;
cin>>p1>>p2>>p3>>p4>>p5;
tb=p1+p2+p3+p4+p5;
dis=(p1+p2+p3+p4+p5)*0.05;
ap=tb-dis;
cout<<"Amount to be Paid="<<ap;
getch();
}

Program 10.
Write a program in c++ that input height of user in feet and
display it in inches
and centimeters.

#include<iostream.h>
#include<conio.h>
main()
{
int height;

clrscr();
cout<<"Enter Your Height In Feet"<<endl;
cin>>height;
cout<<"Height In Inches="<<height*12<<endl;
cout<<"Height In CM="<<height*12*2.5;
getch();
}

Program 11.
Write a program in c++ that will show the following output using
simple cout function.
*
**
***
****
*****

#include<iostream.h>
#include<conio.h>
main()
{
int a,b;
clrscr();
for(a=1;a<=5;a++)
{

for(b=1;b<=a;b++)
cout<<"*";
cout<<endl;
}
getch();
}

Program 12. Write a program in c++ that input marks from user
and show output
"Congratulations! You have passed." if the marks are 40 or
more.

#include<iostream.h>
#include<conio.h>
main()
{
int com,stat,math,eng,urdu,pak;
float per;
clrscr();
cout<<"Enter The Number of Computer"<<endl;
cin>>com;
cout<<"Enter The Number of Stat"<<endl;
cin>>stat;
cout<<"Enter The Number of Math"<<endl;
cin>>math;
cout<<"Enter The Number of English"<<endl;
cin>>eng;

cout<<"Enter The Number of Urdu"<<endl;


cin>>urdu;
cout<<"Enter The Number of Pak-Study"<<endl;
cin>>pak;
per=((com+stat+math+eng+urdu+pak)*100)/550;
if(per>=40)
cout<<"Congratulation You Have Passed";
else
cout<<"Fail";
getch();
}

Program 13.
Write a program in c++ that three numbers from user and display
the largest number.

#include<iostream.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
cout<<"Enter Three Numbers"<<endl;
cin>>a>>b>>c;
if(a>b||a>c)
cout<<"First Number is large"<<endl;
else if (b>a||b>c)

cout<<"Second Number is large"<<endl;


else if (c>a||c>b)
cout<<"Third Number is large"<<endl;
else if(a=b=c)
cout<<"Same Value";
else
cout<<"Invalid Value";
getch();
}

Program 14.
Write a program in c++ that input a number from user and
display wether the number
is even or odd.

#include<iostream.h>
#include<conio.h>
main()
{
int a;
clrscr();
cout<<"Enter a Number"<<endl;
cin>>a;
if(a%2==0)
cout<<"Even";
else
cout<<"Odd";

getch();
}

Program 15.
Write a program in c++ that input marks from user and display
the grade using mutiple if else statement.

#include<iostream.h>
#include<conio.h>
main()
{
int a,per;
clrscr();
cout<<"Enter your Inter Marks"<<endl;
cin>>a;
per=((a/1100)*100);
if(per>=80)
cout<<"Grade A+";
else if(per>=70)
cout<<"Grade A";
else if(per>=60)
cout<<"Grade B";
else if(per>=50)
cout<<"Grade C";
else if(per>=40)
cout<<"Grade D";
else

cout<<"Grade F";
getch();
}

Program 16.
Write a program in c++ that input a number from user and
display day name against that
number using switch statement.

#include<iostream.h>
#include<conio.h>
main()
{
int a;
clrscr();
cout<<"Enter the Number of The Day Of Week"<<endl;
cin>>a;
switch(a)
{
case 1:
cout<<"Monday";
break;
case 2:
cout<<"Tuesday";
break;
case 3:
cout<<"Wednesday";

break;
case 4:
cout<<"Thursday";
break;
case 5:
cout<<"Friday";
break;
case 6:
cout<<"Saturday";
break;
case 7:
cout<<"Sunday";
break;
default:
cout<<"Invalid Value";
}
getch();
}

Program 17. Write a program in c++ that display "Pakistan" five


times using while loop.

#include<iostream.h>
#include<conio.h>
main()
{
int a=1;

while(a<=5)
{
cout<<"Pakistan"<<endl;
a++;
}
getch();

Program 18.
Write a program in c++ that display counting frim 1 to 10 using
while loop.

#include<iostream.h>
#include<conio.h>
main()
{
int a=1;
clrscr();
while(a<=10)
{
cout<<a<<endl;
a++;
}
getch();
}

Program 19.

Write a program in c++ that display odd numbers between 1 to


100 in decending order
using do while loop.

#include<iostream.h>
#include<conio.h>
main()
{
int a=100;
clrscr();
while(a>=1)
{
cout<<a<<endl;
a--;
}
getch();
}

Program 20.
Write a program in c++ that input a number from user and
display the table of that number using do while loop.

#include<iostream.h>
#include<conio.h>
main()
{
int a,b=1;

clrscr();
cout<<"Enter Your Table Number"<<endl;
cin>>a;
while(b<=10)
{
cout<<a<<"X"<<b<<"="<<a*b;
b++;
cout<<endl;
}
getch();
}

Program 21.
Write a program in c++ that display aphabets from A to Z using
for loop.

#include<iostream.h>
#include<conio.h>
main()
{
for (char a='a';a<='z';a++)
cout<<a<<endl;
getch();
}

Program 22.
Write a program in c++ that display the following output using
nested loop.

*
**
***
****
*****

#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int a,b,c;

for(a=1;a<=5;a++)
{
for(c=1;c<=5-a;c++)
cout<<" ";
for(b=1;b<=a;b++)
cout<<"*";
cout<<endl;
}
getch();
}

Program 23.

Write a program in c++ that input two values from user and swap
them using user defined function.

#include<iostream.h>
#include<conio.h>
void main()
{
float m, n;
cout<<"Enter the values of M and N \n";
cin>>m>>n;
cout<<"Before Swapping:M = N = \n"<<m<<n;
swap(float *m,float *n);
cout<<"After Swapping:M = N = \n"<<m<<n;
}
void swap(float *a, float *b)
{
float temp;

temp = *a;
*a = *b;
*b = temp;
}
getch();
}

Program 24.

Write a program in c++ that input two values from user and
which number is greater using
user defined function.

#include<iostream.h>
#include<conio.h>
int greater(int a,int b);
main()
{
clrscr();
int x,y;
cout<<"Enter 2 Values"<<endl;
cin>>x>>y;
greater(x,y);
getch();
}
int greater(int a,int b)
{
clrscr();
int a,b;
if(a>b)
cout<<"1st Number is Greater:"<<a<<endl;
else
if(b>a)
cout<<"2nd Number is Greater:"<<b;

else
cout<<"Invalid Value";
getch();
}

Program 25.
Write a program in c++ that pass the value by reference to
function and display the table of input number.

#include<iostream.h>
#include<conio.h>
main()
{
int a,b=1;
clrscr();
cout<<"Enter Your Table Number"<<endl;
cin>>a;
while(b<=10)
{
cout<<a<<"X"<<b<<"="<<a*b;
b++;
cout<<endl;
}
getch();
}

Program 26.

Write a program in c++ that display the address of a stored value.

#include<iostream.h>
#include<conio.h>
main()
{
int a,b;
int *p,*q;
clrscr();
cout<<"Enter two Value"<<endl;
cin>>a>>b;
p=&a;
q=&b;
cout<<"Entered values="<<a<<b<<endl;
cout<<"Adress of first value="<<p<<endl;
cout<<"Adress of second value="<<q;
getch();
}

Program 27.
Write a program in c++ that display the address of
numaric,character and decimal values.

#include<iostream.h>
#include<conio.h>
main()
{

int a;
int *p;
char b;
char *q;
float c;
float *r;
clrscr();
cout<<"Enter a integer number"<<endl;
cin>>a;
p=&a;
cout<<"Adress of iteger number="<<p<<endl;
cout<<"Enter a decimale number"<<endl;
cin>>b;
q=&b;
cout<<"Adress of decimale number="<<q<<endl;
cout<<"Enter a char"<<endl;
cin>>c;
r=&c;
cout<<"Adress of character="<<r;

getch();
}

Program 28.
Write a program in c++ that input two number from user and
swap them by using pointer as function argument.

#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int a,b,c;
int *p,*q;
cout<<"Enter Two Numbers"<<endl;
cin>>a>>b;
cout<<"Address of a="<<endl<<p<<endl;
cout<<"Address of b="<<endl<<q<<endl;
cout<<"Address Before Swapping"<<endl<<p<<endl<<q<<endl;
c=a;
a=b;
b=c;
cout<<"Address of a="<<p<<endl;
cout<<"Address of b="<<q<<endl;
cout<<"Address After Swapping"<<endl<<p<<endl<<q;
getch();
}

PROGRAM 29.
Write a program in c++ that input marks of user and display the
grade of user using pointer as function argument.

#include<iostream.h>
#include<conio.h>

main()
{
int marks;
clrscr();
cout<<"enter your marks";
cin>>marks;
if(marks>=90)
cout<<"Grade A+";
else
if(marks>=75)
cout<<"Grade B+";
else
if(marks>=60)
cout<<"Grade C+";
else
if(marks>=50)
cout<<"Grade D+";
else
cout<<"Grade F";
getch();
}

Program 30.
Write a program in c++ that input five integer from user and
stores them in an array and dispaly all values of array without
using loops.

#include<iostream.h>
#include<conio.h>
main()
{
int a[5];
clrscr();
cout<<"Enter five value"<<endl;
cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4];
cout<<"Entered
Value"<<endl<<a[0]<<endl<<a[1]<<endl<<a[2]<<endl<<a[3]<<endl<<a[4];
getch();
}

Program 31.
Write a program in c++ that input five integer from user and
stores them in an array and dispaly all values of array using loops.

#include<iostream.h>
#include<conio.h>
main()
{
int a[5];
clrscr();
cout<<"Enter five Number"<<endl;
for(int i=0;i<=4;i++)
{
cin>>a[i];

}
cout<<"Enterd value"<<a[i];
getch();
}

Program 32.
Write a program in c++ that input five numbers from user and
stores them in an array and display the sum and average of these
values.

#include<iostream.h>
#include<conio.h>
main()
{
int a[5];
clrscr();
cout<<"Enter five Value"<<endl;
for (int i=0;i<=4;i++)
{
cin>>a[i];
}
cout<<"SUM="<<a[0]+a[1]+a[2]+a[3]+a[4]<<endl;
cout<<"Average="<<(a[0]+a[1]+a[2]+a[3]+a[4])/5;
getch();
}

Program 33.

Write a program in c++ that input five numbers from user and
sort them in accending order by using bubble sort.

#include<iostream.h>
#include<conio.h>
main()
{
int a[5],c,d,swap;
cout<<"Enter Five value"<<endl;
for(int i=0;i<=4;i++)
{
cin>>a[i];
}
for (c=0;c<=(4-1);c++)
{
for(d=0;d<=4-c-1;d++)
{
if(a[d]>a[d+1])
{
swap=a[d];
a[d]=a[d+1];
a[d+1]=swap;
}
}}
cout<<"Asscending order"<<endl;
for( i=0;i<=4;i++)

{
cout<<endl<<a[i];
}
getch();
}

Program 34.
Write a program in c++ that input five numbers from user and
sort them in decending order by using bubble sort.

#include<iostream.h>
#include<conio.h>
main()
{
int a[5],c,d,swap;
cout<<"Enter Five value"<<endl;
for(int i=0;i<=4;i++)
{
cin>>a[i];
}
for (c=0;c<=(4-1);c++)
{
for(d=0;d<=4-c-1;d++)
{
if(a[d]<a[d+1])
{
swap=a[d];

a[d]=a[d+1];
a[d+1]=swap;
}
}}
cout<<"Decending Order"<<endl;
for( i=0;i<=4;i++)
{
cout<<endl<<a[i];
}
getch();
}

Program 35.
Write a program in c++ that input five numbers from user and
sort them in accending order by
using selection sort.

#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int number[5];
int i, j, a;
cout<<"Enter the 5 Values\n";
for (i=0; i<4;++i)

cin>>number[i];
{
for (j=i+1; j<4;++j)
{
if (number[i] < number[j])
{
a=number[i];
number[i] = number[j];
number[j] = a;
}
}
}
cout<<"The numbers arranged in Ascending order are given below\n";
for (i=0;i<=4;++i)
{
cout<<"\n"<<number[i];
}
getch();
}

Program 36.
Write a program in c++ that input five numbers from user and
sort them in decending order by
using selection sort.

#include<iostream.h>

#include<conio.h>
main()
{
clrscr();
int number[30];
int i, j, a, n;
cout<<"Enter the value of N\n";
cin>>n;
cout<<"Enter the numbers \n";
for (i=0; i<n;++i)
cin>>number[i];
for (i=0; i<n;++i)
{
for (j=i+1; j<n;++j)
{
if (number[i] < number[j])
{
a=number[i];
number[i] = number[j];
number[j] = a;
}
}
}
cout<<"The numbers arranged in descending order are given below\n";
for (i=0; i<n;++i)
{

cout<<"\n"<<number[i];
}
getch();
}

Program 37.
Write a program in c++ that input name from user and display
the name in reverse order.

#include<iostream.h>
#include<conio.h>
main()
{
char str[10];
char rev[10];
int i=-1,j=0;
cout<<"Enter any string"<<endl;
cin>>str;
while(str[++i]!='\0');
while(i>=0)
rev[j++]=str[--j];
rev[j]='\0';
cout<<"Reverse string"<<rev;

getch();
}

38.Write a program in c++ that input fivr numbers from user and
dispaly the positon of that numbers stored in memory.
#include <iostream.h>
using namespace std;
int main ()
{
int a[5] ;
cout<<"enter 5 numbers";
cin>>a[0]<<a[1]<<a[2]<<a[3]<<a[4]
double *p;
p = balance;
cout << "Array values using pointer " << endl;
for ( int i = 0; i < 5; i++ )
{
cout << "*(p + " << i << ") : ";
cout << *(p + i) << endl;
}

cout << "Array values using balance as address " << endl;
for ( int i = 0; i < 5; i++ )
{
cout << "*(a + " << i << ") : ";
cout << *(a + i) << endl;
}

getch();

Program 39.
Write a program in c++ that input 8 numbers from user and
display that numbers in columns and rows by using 2D arrays.

#include<iostream.h>
#include<conio.h>
main()
{
int a[2][4]={{0,0},{1,2},{2,4},{3,6},{4,8}};
for(int i=0;i<8;i++)
for(int j=0;j<2;j++)
{
cout<<"a["<<i<<"]["<<j<<"]:";
cout<<a[i][j]<<endl;
}
getch();
}

Program 40.
Write a program in c++ that declares a structure to store
employee id and salary of an employee and display it.

#include<iostream.h>
#include<conio.h>
struct db
{

int id,age;
float salary;
};
main()
{
db employee;
employee.age=22;
employee.id=1;
employee.salary=1221.999;
cout<<employee.age<<endl<<employee.id<<endl<<employee.salary;
getch();
}

You might also like