0% found this document useful (0 votes)
29 views63 pages

27 C++

Uploaded by

jai.r.thakur91
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)
29 views63 pages

27 C++

Uploaded by

jai.r.thakur91
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

Question1:Write a program in C++ to perform various arithmetic operations

with two numbers.


Coding:

#include<iostream.h>

#include<conio.h>

void main(){

int a,b,sum,sub,mul,div,rem;

clrscr();

cout<<"\n enter two numbers:";

cin<<a<<b;

sum=a+b;

sub=a-b;

mul=a*b;

div=a/b;

rem=a%b;

cout<<"\n"<<sum;

cout<<"\n"<<sub;

cout<<"\n"<<mul;

cout<<"\n"<<div;

cout<<"\n"<<rem;

getch();}

Output:
Question2: Write a program in C++ to perform arithmetic operations using
user defined function.
Coding:

#include<stdio.h>

#include<conio.h>

int add(int a,int b)

{return a+b;}

int sub(int a,int b)

{return a-b;}

int mul(int a,int b)

{return a*b;}

float div(int a,int b)

{return(float) a/b;}

void main()

clrscr();

int x,y;

char op;

cout<<"enter operator(=-*/):";

cin>>op;

if(op=='+')

cout<<"result:"<<add(x,y);

else

if(op=='-')
cout<<"result:"<<sub(x,y);

else

if(op=='*')

cout<<"result:"<<mul(x,y);

else

if(op=='/')

cout<<"result:"<<div(x,y);

else cout<<"invalid input:";

getch();

}}

Output:
Question3: Write a program in C++ to find simple interest.
Coding:

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

floatprincipal,rate,time,si;

cout<<"enter principal amount:";

cin>>principal;

cout<<"enter rate of interest:";

cin>>rate;

cout<<"enter time(in years):";

cin>>time;

si=(principal*rate*time)/100;

cout<<"simple interest="<<si;

getch();

Output:
Question4: Write a program in C++ to find compound interest.
Coding:

#include<iostream.h>

#include<conio.h>

#include<math.h>

void main()

clrscr();

floatprincipal,rate,time,amount,ci;

cout<<"enter principal amount:";

cin>>principal;

cout<<"enter rate of interest:";

cin>>rate;

cout<<"enter time(in years):";

cin>>time;

amount=principal*pow((1+rate/100),time);

ci=amount-principal;

cout<<"compuond interest="<<ci;

getch();

Output:
Question5: Write a program in C++ to convert temperature Fahrenheit to
Celsius.
Coding:

#include<iostream.h>

#include<conio.h>

int main()

floatfahrenheit,celsius;

cout<<"enter temperature in Fahrenheit:";

cin>>fahrenheit;

celsius=(fahrenheit-32)*5.0/9.0;

cout<<"temperature in celsius:"<<celsius<<"C"<<endl;

return 0;

Output:
Question6: Write a program in C++ to find the area of circle.
Coding:

#include<iostream.h>

#include<conio.h>

int main()

floatradius,area;

const float PI=3.14159;

cout<<"enter the radius of a circle:";

cin>>radius;

area=PI*radius*radius;

cout<<"area of the circle is:"<<area<<"square units"<<endl;

getch();

Output:
Question7: Write a program in C++ to find the average of N numbers using
while loop.
Coding:

#include<iostream.h>

#include<conio.h>

void main(){

inti,sum=0,n;

floatavg;

clrscr();

cout<<"\n enter a number";

cin>>n;

i=1;

while(i<=n)

sum=sum+i;

i++;

avg=(float)sum/n;

cout<<"\n average of"<<n<<"numbers="<<avg;

getch();}

Output:
Question8: Write a program in C++ to find the average of N numbers using
do while loop.
Coding:

#include<iostream.h>

#include<conio.h>

void main()

inti,sum=0,n;

floatavg;

clrscr();

cout<<"\n enter a number";

cin>>n;

i=1;

do

{sum=sum+i;

i++;}

while(i<=n);

avg=(float)sum/n;

cout<<"\n average of"<<n<<"numbers="<<avg;

getch();

Output:
Question9: Write a program in C++ to find average of N numbers using for
loop.
Coding:

#include<iostream.h>

#include<conio.h>

void main()

inti,sum=0,n;

floatavg;

clrscr();

cout<<"\n enter a number";

cin>>n;

for(i=1;i<=n;i++)

sum=sum+i;

avg=(float)sum/n;

cout<<"\n average of"<<n<<"numbers="<<avg;

getch();

Output:

Question10: Write a program in C++ to print the following pattern.


*

**

***

****
Coding:

#include<iostream.h>

#include<conio.h>

void main()

int i,j,n;

clrscr();

cout<<"\n how many rows:";

cin>>n;

for(i=1;i<=n;i++)

for(j=1;j<=i;j++)

cout<<"*\t";

cout<<"\n";

getch();

Output:
Question11: Write a program in C++ to print the following pattern.
1

12

123

1234

12345
Coding:

#include<iostream.h>

#include<conio.h>

void main()

int i,j,n;

cout<<"\n enter the number of rows:";

cin>>n;

for(i=1;i<=n;i++)

for(j=1;j<=i;j++)

cout<<"\t"<<j;

cout<<endl;

getch();

Output:
Question12: Write a program in C++ to print the number pattern.
1

22

333

4444

55555
Coding:

#include<iostream.h>

#include<conio.h>

void main()

int i,j,n;

cout<<"\n enter the number of rows:";

cin>>n;

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

cout<<"\t"<<i;

cout<<endl;

getch();

Output:
Question13: Write a program in C++ to print a following pattern.

***

*****

*******
Coding

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

inti,j,space;

int rows=4;

for(i=1;i<=rows;i++)

for(space=1;space<=rows-i;space++)

cout<<" ";

for(j=1;j<=(2*i-1);j++)

cout<<"*";

cout<<endl;

}
getch();

Output:

Question14: Write a program in C++ to print the following pattern.


****

***

**

*
Coding

#include<iostream.h>

#include<conio.h>

void main()

int i,k,j,n;

cout<<"\n enter number of rows:";

cin>>n;

for(i=0;i<n;i++)

for(j=0;j<=i;j++)

cout<<" ";

for(k=0;k<n-i;k++)

cout<<"* ";

cout<<endl;

getch();
}

Output:

Q uestion15. WAP in c++ to find if number is positive.


Coding:

#include<iostream.h>

#include<conio.h>

void main()

int num;

clrscr();

cout<<"\n enter a number:";

cin>>num;

if(num>0)

cout<<"\n number is possitive:";

cout<<"\n end of program";

getch();

Output:
Question16: . WAP in c++ to find out whether the entered no. is positive or
negative.
Coding:

#include<iostream.h>

#include<conio.h>

void main()

int num;

clrscr();

cout<<"\n enter a number:";

cin>>num;

if(num>0)

cout<<"\n number is possitive:";

else

cout<<"\n number is negative:";

cout<<"\n end of program:";

getch();

Output:
Question17: WAP in c++ to find weather the no. entered is even or odd.

Coding:

#include<iostream.h>

#include<conio.h>

void main()

int num;

clrscr();

cout<<"\n enter a number:";

cin>>num;

if(num % 2==0)

cout<<"\n numberis even:";

else

cout<<"\n number is odd:";

cout<<"\n end of program:";

getch();

Output:
Q uestion18: Write a program in C++ to find greatest among three number.
Coding:

#include<iostream.h>

#include<conio.h>

void main()

int a, b, c;

clrscr();

cout<<"\n enter three numbers";

cin>>a>>b>>c;

if(a>b)

if(a<c)

cout<<"\n greatest number="<<a;

else

cout<<"\n greatest number="<<c;

else

if(b>c)

{
cout<<"\n greatest number="<<b;

else

cout<<"\n greatest number="<<c;

getch();

Output:
Question19: WAP in c++ to compute the grade of a student using else if
ladder.
Coding:

#include<iostream.h>

#include<conio.h>

void main()

int p,c,m,per,t;

char grade;

clrscr();

cout<<"\n enter marks of 3 subjects";

cin>>p>>c>>m;

t=p+c+m;

per=t/3;

switch(per/10)

case 10:

case 9:

case 8:

grade='A';

break;

case 7:

case 6:

grade='B';

break;
case 5:

case 4:

grade='C';

break;

default:

grade='F';

cout<<"\n grade of student is"<<grade;

getch();

Output:
Question20: Write a program in C++ to compute the grade of student using
switch statement.
Coding:

#include<iostream.h>

#include<conio.h>

void main()

intp,c,m,per,t;

char grade;

clrscr();

cout<<"\n enter marks of three subjects:";

cin>>p>>c>>m;

t=p+c+m;

per=t/3;

switch(per/10)

case 10:

case 9:

case 8:

grade='A';

break;

case 7:

case 6:

grade='B';

break;
case 5:

case 4:

grade='C';

break;

default:

grade='F';

cout<<"\n grade="<<grade;

getch();

Output:
Question21: Write a program in C++ to find factorial of a number.
Coding:

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

intn,i;

long factorial=1;

cout<<"enter a number:";

cin>>n;

if(n<0)

cout<<"factorial is not defined for negative numbers:";

else

for(i=1;i<=n;i++)

factorial=factorial*i;

cout<<"factorial of"<<n<<"="<<factorial;

getch();

}
Output:
Question22: Write a program in C++ to find reverses of a number.
Coding:

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

intnum,reverse=0,digit;

cout<<"enter a number:";

cin>>num;

while(num!=0)

digit=num%10;

reverse=reverse*10+digit;

num=num/10;

cout<<"reversed number="<<reverse;

getch();

Output:
Question23: Write a program in C++ to find whether the number entered is
palindrome or not.
Coding:

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

intnum,original,reverse=0,digit;

cout<<"enter a number:";

cin>>num;

original=num;

while(num!=0)

digit=num%10;

reverse=reverse*10+digit;

num=num/10;

if(original==reverse)

cout<<original<<"is a palindrome";

else

cout<<original<<"is not a palindrome";

getch();

}
Output:
Question24: Write a program in C++ to find whether the number entered is
Armstrong or not.
Coding:

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

intnum,original,sum=0,digit;

cout<<"enter a number:";

cin>>num;

original=num;

while(num!=0)

digit=num%10;

sum=sum+(digit*digit*digit);

num=num/10;

if(sum==original)

cout<<original<<"is an Armstrong number";

else

cout<<original<<"is not an Armstrong number";

getch();

}
Output:
Question25: Write a program in C++ to enter and display the detail of the
student using structure.
Coding:

#include<iostream.h>

#include<conio.h>

void main ()

clrscr();

struct student

int rn;

char s_name[10];

char address[20];

float fees;

};

struct student s1;

cout<<"\n enter roll no, name,address & fees:";

cin>>s1.rn>>s1.s_name>>s1.address>>s1.fees;

cout<<"\n roll no"<<s1.rn<<"\n name"<<s1.s_name<<"\n address"<<s1.address<<"\n


fees"<<s1.fees;

getch();

Output:
Question26: Write a program in C++ to enter and display record of five
students using array of structure elements.
Coding:

#include<iostream.h>

#include<conio.h>
void main()
{
clrscr();
int i;
struct student
{
int rn;
char s_name[10];
char address[20];
float fees;
};
struct student s[5];
for(i=1;i<=5;i++)
{
cout<<"\n enter Roll Number,Name,Address and Fees:";
cin>>s[i].rn>>s[i].s_name>>s[i].address>>s[i].fees;
cout<<"\n Roll Number:"<<s[i].rn<<"\n Name:"<<s[i].s_name<<"\n
Address:"<<s[i].address<<"\n fees:"<<s[i].fees;
}
getch();
}
Output:
Question27:Write a program in C++ to find sum of two numbers using user
defined function and return expression.
Coding:

#include<iostream.h>

#include<conio.h>

int addNumbers(int a,int b)

return a+b;

void main()

clrscr();

int num1,num2,sum;

cout<<"\n enter first number:";

cin>>num1;

cout<<"\n enter second number:";

cin>>num2;

sum=addNumbers(num1,num2);

cout<<"\n sum of"<<num1<<"and"<<num2<<"is:"<<sum;

getch();

Output:
Question28:Write a program in C++ to find the biggest number.
Coding:

#include<iostream.h>

#include<conio.h>

int big(int[],int);

void main()

int a[10],i;

clrscr();

cout<<"\n enter elements of array:";

for(i=0;i<10;i++)

cin>>a[i];

cout<<"\n biggest element="<<big(a,10);

getch();

int big(int a[],int n)

int b,i;

for(i=1;i<n;i++)

if(a[i]>b)

b=a[i];
}

return(b);

Output:
Question29: Write a program in C++ to find sum of diagonal elements of a
matrix.
Coding:

#include<iostream.h>

#include<conio.h>

int trace (int[10][10],int,int);

void main()

int a[10][10],n,m,i,j,sum;

clrscr();

cout<<"\n how many rows and columns:";

cin>>n>>m;

for(i=0;i<n;i++)

for(j=0;j<m;j++)

cin>>a[i][j];

sum=trace(a,n,m);

cout<<"\n sum of diagonal elements of matrix"<<sum;

getch();

int trace (int a[10][10],int n,int m)

{
int i,j,sum=0;

for(i=0;i<n;i++)

for(j=0;j<m;j++)

if(i==j)

sum=sum+a[i][j];

return(sum);

Output:
Question30: Write a program in C++ to interchange value of two variables
using call by value method.
Coding:

#include<iostream.h>

#include<conio.h>

void interchange(int,int);

void main()

int a,b;

clrscr();

cout<<"\n enter two numbers:";

cin>>a>>b;

cout<<"\n value of a &b before calling:"<<a<<b;

interchange(a,b);

cout<<"\n value of a & b after calling function:"<<a<<b;

getch();

void interchange (int x,int y)

int temp;

temp=x;

x=y;

y=temp;

cout<<"\n value of x&y after interchange inside the function"<<x<<y;

}
Output:
Question31: Write a program in C++ to interchange value of two variables
using call by reference method.
Coding:

#include<iostream.h>

#include<conio.h>

void main()

int a,b;

clrscr();

printf("\n Enter two numbers:");

scanf("%d%d",&a,&b);

printf("\n value of a&b before interchange=%d&%d",a,b);

interchange(&a,&b);

printf("\n value of a&b after interchange=%d&%d",a,b);

getch();

void interchange(int*x,int*y)

int temp;

temp=*x;

*x=*y

Output:
Question32: Write a program in C++ to illustrate the concept of one class
with two objects by taking student data.
Coding:

#include<iostream.h>

#include<conio.h>

class student

private:

int rn;

char s_name[10];

char f_name[10];

public:

void readdata()

cout<<"\n enter roll no.,name & father's name";

cin>>rn>>s_name>>f_name;

void writedata()

cout<<"\n roll no.="<<rn<<"\n name="<<s_name<<"\n father's name="<<f_name;

};

void main()

student s1,s2;
s1.readdata();

s2.readdata();

s1.writedata();

s2.writedata();

getch();

Output:
Question33: Write a program in C++ to enter and display the details of
account.
Coding:

#include<iostream.h>

#include<conio.h>

class account

private:

int acc_no;

char acc_name[20];

float balance;

public:

void read()

cout<<"\n enter account no., name and balance=";

cin>>acc_no>>acc_name>>balance;

void show()

cout<<"\n account no.="<<acc_no<<"\n Name="<<acc_name<<"\n balance="<<balance;

};

void main()

class account a1;


a1.read();

a1.show();

getch();

Output:
Question34: Write a program in C++ to print the following pattern.

2 1

3 2 1

4 3 2 1
Coding:

#include<iostream.h>

#include<conio.h>

void main()

int i,j,n;

clrscr();

cout<<"\n enter the number of row:";

cin>>n;

for(i=1;i<=n;i++)

for(j=i;j>=1;j--)

cout<<j<<"\t";

cout<<"\n";

getch();

}
Output:
Question35: Write a program in C++ to print the following pattern.
Coding:

#include<iostream.h>

#include<conio.h>

void main()

int n,i,j;

cout<<"\n enter the number of row";

cin>>n;

for(i=1;i<=n;i++)

for(j=i;j>=1;j--)

cout<<j<<"\t";

cout<<"\n";

getch();

Output:
Question36: Write a program in C++ to print the following pattern.

2 1 2

3 2 1 2 3

4 3 2 1 2 3 4
Coding:

#include<iostream.h>

#include<conio.h>

void main()

int n,i,j,l;

cout<<"\n enter the number of row";

cin>>n;

for(i=1;i<=n;i++)

for(j=i;j<=n-1;j++)

cout<<" ";

for(l=i;l>=1;l--)

cout<<l<<" ";

for(j=2;j<=i;j++)

{
cout<<j<<" ";

cout<<"\n";

getch();

Output:
Question37: Write a program to reverse digits of a number.
Coding:

#include<iostream.h>

#include<conio.h>

void main()

int n,d,rev=0;

clrscr();

cout<<"\n enter a number ";

cin>>n;

while(n>0)

d=n%10;

n=n/10;

rev=rev*10+d;

cout<<"\n reverse order of number="<<rev;

getch();

Output:
Question38: Write a program to find sum of digits of a number.
Coding:

#include<iostream.h>

#include<conio.h>

void main()

int a,d,sum=0;

clrscr();

cout<<”Enter any number:”;

cin>>a;

while(a>0)

d=a%10;

a/=10;

sum+=d;

cout<<”sum=”<<sum;

getch();

Output:
Question39: Write a program in c++ to read and display the student data by
using the concept of single inheritance.
Coding:

#include<iostream.h>

#include<conio.h>

class student

public:

int rn;

char s_name[20];

char add[30];

public:

void read_data()

cout<<"\n enter roll no,name , address";

cin>>rn>>s_name>>add;

void display()

cout<<"\n roll no="<<rn<<"\n name="<<s_name<<"\n addess="<<add;

};

class marks:public student

{
public:

int p,c,m;

void read_data()

student::read_data();

cout<<"\n enter marks of physics, chemistry and maths";

cin>>p>>c>>m;

void display()

student::display();

cout<<"\n marks of physics="<<p;

cout<<"\n marks of chemistry="<<c;

cout<<"\n marks of maths="<<m;

};

void main()

class marks m1;

m1.read_data();

m1.display();

getch();

}
Output:
Question41: Write a program in C++

You might also like