39
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();

Abdullah Shahid(0014)

  • Upload
    haris

  • View
    7

  • Download
    3

Embed Size (px)

DESCRIPTION

Abdullah Shahid(0014)

Citation preview

Page 1: Abdullah Shahid(0014)

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;

Page 2: Abdullah Shahid(0014)

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()

Page 3: Abdullah Shahid(0014)

{

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.

Page 4: Abdullah Shahid(0014)

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 (a=10, b=20, c=15, d=8)

#include<iostream.h>

#include<conio.h>

main()

{

Page 5: Abdullah Shahid(0014)

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.

Page 6: Abdullah Shahid(0014)

#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();

Page 7: Abdullah Shahid(0014)

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++)

Page 8: Abdullah Shahid(0014)

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;

Page 9: Abdullah Shahid(0014)

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;

Page 10: Abdullah Shahid(0014)

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";

Page 11: Abdullah Shahid(0014)

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";

Page 12: Abdullah Shahid(0014)

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;

Page 13: Abdullah Shahid(0014)

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>

Page 14: Abdullah Shahid(0014)

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++;

}

Page 15: Abdullah Shahid(0014)

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>

Page 16: Abdullah Shahid(0014)

#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();

Page 17: Abdullah Shahid(0014)

}

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;

}

Page 18: Abdullah Shahid(0014)

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;

}

Page 19: Abdullah Shahid(0014)

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)

Page 20: Abdullah Shahid(0014)

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++;

Page 21: Abdullah Shahid(0014)

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.

Page 22: Abdullah Shahid(0014)

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;

Page 23: Abdullah Shahid(0014)

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;

Page 24: Abdullah Shahid(0014)

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)

Page 25: Abdullah Shahid(0014)

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.

Page 26: Abdullah Shahid(0014)

#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;

Page 27: Abdullah Shahid(0014)

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++)

Page 28: Abdullah Shahid(0014)

{

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;

Page 29: Abdullah Shahid(0014)

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

Page 30: Abdullah Shahid(0014)

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";

Page 31: Abdullah Shahid(0014)

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)

Page 32: Abdullah Shahid(0014)

{

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()

Page 33: Abdullah Shahid(0014)

{

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;

Page 34: Abdullah Shahid(0014)

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}};

Page 35: Abdullah Shahid(0014)

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;

Page 36: Abdullah Shahid(0014)

cout<<employee.age<<endl<<employee.id<<endl<<employee.salary;

getch();

}