COMPUHELP HelpDesk
C++ Programming HelpDesk
Solved assignments with and without OOP on this site
154Without OOP
51With OOP
PDFUnsolved
LabPractice
Solved Assignments (Without OOP)
Procedural C++ programs.
Assignments of If and If..Else Statements (10 programs)
#include<iostream>
using namespace std;
int main()
{
int marks;
cout<<"Enter marks";
cin>>marks;
if(marks>=80 && marks<=100)
{
cout<<"A Grade";
}
else if(marks>=60 && marks<80)
{
cout<<"B Grade";
}
else if(marks>=40 && marks<60)
{
cout<<"C Grade";
}
else if(marks>=0 && marks<40)
{
cout<<"Fail";
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter any number ";
cin>>num;
if(num>0)
{
cout<<"Number is Positive";
}
else if(num<0)
{
cout<<"Number is Negative";
}
//else if(num==0)
else
{
cout<<"Number is Zero";
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter any number ";
cin>>num;
if(num%2==0)
{
cout<<"Number is even";
}
else
{
cout<<"Number is odd";
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"Enter any character";
cin>>ch;
if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U')
{
cout<<"character is vowel";
}
else
{
cout<<"character is consonent";
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"Enter any character ";
cin>>ch;
if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U')
{
cout<<"character is vowel";
}
else if(ch=='@'||ch=='!'||ch=='#'||ch=='%'||ch=='&'||ch=='*')
{
cout<<"character is special";
}
else
{
cout<<"character is consonent";
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter any number between 1 to 7:";
cin>>num;
if(num==1)
{
cout<<"Day is Monday";
}
else if(num==2)
{
cout<<"Day is Tuesday";
}
if(num==3)
{
cout<<"Day is Wednesday";
}
if(num==4)
{
cout<<"Day is Thursday";
}
if(num==5)
{
cout<<"Day is Friday";
}
if(num==6)
{
cout<<"Day is Saturday";
}
if(num==7)
{
cout<<"Day is Sunday";
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter any number between 0 and 9:";
cin>>num;
if(num==0)
{
cout<<"You entered Zero";
}
else if(num==1)
{
cout<<"You entered One";
}
else if(num==2)
{
cout<<"You entered Two";
}
else if(num==3)
{
cout<<"You entered Three";
}
else if(num==4)
{
cout<<"You entered Four";
}
else if(num==5)
{
cout<<"You entered Five";
}
else if(num==6)
{
cout<<"You entered Six";
}
else if(num==7)
{
cout<<"You entered Seven";
}
else if(num==8)
{
cout<<"You entered Eight";
}
else if(num==9)
{
cout<<"You entered Nine";
}
else
{
cout<<"wrong input";
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"Print any number between 1 and 12:";
cin>>num);
if(num==1)
{
cout<<"You entered January";
}
else if(num==2)
{
cout<<"You entered Feburary";
}
else if(num==3)
{
cout<<"You entered March";
}
else if(num==4)
{
cout<<"You entered April";
}
else if(num==5)
{
cout<<"You entered May";
}
else if(num==6)
{
cout<<"You entered June";
}
else if(num==7)
{
cout<<"You entered July";
}
else if(num==8)
{
cout<<"You entered August";
}
else if(num==9)
{
cout<<"You entered September";
}
else if(num==10)
{
cout<<"You entered October";
}
else if(num==11)
{
cout<<"You entered November";
}
else if(num==12)
{
cout<<"You entered December";
}
else
{
cout<<"wrong input";
}
return 0;
}#include<iostream>
using namespace std;
#include<stdlib.h>
int main()
{
int choice,num1,num2,res;
cout<<"nPress 1 for addition";
cout<<"nPress 2 for subtraction";
cout<<"nPress 3 for multiplication";
cout<<"nPress 4 for division";
cout<<"nPress 5 for exit";
cin>>choice;
cout<<"nEnter first number:";
cin>>num1;
cout<<"nEnter second number:";
cin>>num2;
if(choice==1)
{
res=num1+num2;
cout<<"The sum is: "<<res;
}
else if(choice==2)
{
res=num1-num2;
cout<<"The difference is: "<<res;
}
else if(choice==3)
{
res=num1*num2;
cout<<"The multiplication is: "<<res;
}
else if(choice==4)
{
res=num1/num2;
cout<<"The division is: "<<res;
}
else if(choice==5)
{
exit(0);
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int a, b ,c;
cout<<"nEnter first number: ";
cin>>a;
cout<<"nEnter second number: ";
cin>>b;
cout<<"nEnter third number: ";
cin>>c;
if(a>b)
{
if(a>c)
{
cout<<"nFirst number is greater";
}
else
{
cout<<"nThird number is greater";
}
}
else
{
if(b>c)
{
cout<<"nSecond number is greater";
}
else
{
cout<<"Third number is greater";
}
}
return 0;
}Assignments of Switch Statement (7 programs)
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"enter any number between 0 to 9:";
cin>>num;
switch(num)
{
case 0:
cout<<"You entered ZERO";
break;
case 1:
cout<<"You entered ONE";
break;
case 2:
cout<<"You entered TWO";
break;
case 3:
cout<<"You entered THREE";
break;
case 4:
cout<<"You entered FOUR";
break;
case 5:
cout<<"You entered FIVE";
break;
case 6:
cout<<"You entered SIX";
break;
case 7:
cout<<"You entered SEVEN";
break;
case 8:
cout<<"You entered EIGHT";
break;
case 9:
cout<<"You entered NINE";
break;
default:
cout<<"wrong input";
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"enter any number between 1 to 12:";
cin>>num;
switch(num)
{
case 1:
cout<<"You entered JANUARY";
break;
case 2:
cout<<"You entered FEBURARY";
break;
case 3:
cout<<"You entered MARCH";
break;
case 4:
cout<<"You entered APRIL";
break;
case 5:
cout<<"You entered MAY";
break;
case 6:
cout<<"You entered JUNE";
break;
case 7:
cout<<"You entered JULY";
break;
case 8:
cout<<"You entered AUGUST";
break;
case 9:
cout<<"You entered SEPTEMBER";
break;
case 10:
cout<<"You entered OCTOBER";
break;
case 11:
cout<<"You entered NOVEMBER");
break;
case 12:
cout<<"You entered DECEMBER";
break;
default:
cout<<"wrong input";
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"enter any number between 1 to 7: ";
cin>>num;
switch(num)
{
case 1:
cout<<"You entered SUNDAY";
break;
case 2:
cout<<"You entered MONDAY";
break;
case 3:
cout<<"You entered TUESDAY";
break;
case 4:
cout<<"You entered WEDNESDAY";
break;
case 5:
cout<<"You entered THURSDAY";
break;
case 6:
cout<<"You entered FRIDAY";
break;
case 7:
cout<<"You entered SATURDAY";
break;
default:
cout<<"wrong input";
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"Enter any character in lowercase: ";
cin>>ch;
switch(ch)
{
case 'a':
cout<<"Character is vowel";
break;
case 'e':
cout<<"Character is vowel";
break;
case 'i':
cout<<"Character is vowel";
break;
case 'o':
cout<<"Character is vowel";
break;
case 'u':
cout<<"Character is vowel";
break;
default:
cout<<"character is consonent";
}
return 0;
}#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int num1,num2,res;
int value;
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
cout<<"n Press 1 for addition";
cout<<"n Press 2 for subtraction";
cout<<"n Press 3 for division";
cout<<"n Press 4 for multilication";
cout<<"n Press 5 for EXIT";
cout<<endl;
cin>>value;
switch(value)
{
case 1:
res=num1+num2;
cout<<"The sum is: "<<res;
break;
case 2:
res=num1-num2;
cout<<"The subtraction is: "<<res;
break;
case 3:
res=num1/num2;
cout<<"The division is: "<<res;
break;
case 4:
res=num1*num2;
cout<<"The multiplication is: "<<res;
break;
default:
exit(0);
}
return 0;
}#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int x=0,y=1,z,i,fac=1;
int value;
cout<<"n Press 1 for fibbonacci series";
cout<<"n Press 2 for factorial";
cout<<"n Press 3 for printing even series";
cout<<"n Press 4 for printing odd series";
cout<<"n Press 5 for EXIT";
cout<<endl;
cin>>value;
switch(value)
{
case 1:
cout<<"Fibbonacci series is:";
cout<<x<<" "<<y;
for(i=1;i<=8;i++)
{
z=x+y;
x=y;
y=z;
cout<<" "<<z;
}
break;
case 2:
for(i=1;i<=5;i++)
{
fac=fac*i;
}
cout<<"factorial of 5 is: "<<fac;
break;
case 3:
cout<<"even series is: ";
for(i=0;i<=10;i=i+2)
{
cout<<i<<" ";
}
break;
case 4:
cout<<"odd series is:";
for(i=1;i<=10;i=i+2)
{
cout<<i<<" ";
}
case 5:
exit(0);
break;
default:
cout<<"wrong input,Please try again";
}
return 0;
}#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int value,n;
char a;
int x=0,y=1,z,i,fac=1;
cout<<"n Press 1 for even numbers";
cout<<"n Press 2 for odd numbers";
cout<<"n Press 3 for square of the number";
cout<<"n Press 4 for character series";
cout<<"n Press 5 for EXIT";
cout<<endl;
cin>>value;
switch(value)
{
case 1:
cout<<"Enter nth number: ";
cin>>n;
cout<<"even series is: ";
for(i=0;i<=n;i=i+2)
{
cout<<i<<" ";
}
break;
case 2:
cout<<"Enter nth number: ";
cin>>n;
cout<<"odd series is: ";
for(i=1;i<=n;i=i+2)
{
cout<<i<<" ";
}
break;
case 3:
cout<<"Enter nth number: ";
cin>>n;
cout<<(n*n);
break;
case 4:
cout<<"n Enter any character in lowercase: ";
cin>>a;
for(i=97;a<=122;a++)
{
cout<<a<<" ";
}
break;
case 5:
exit(0);
break;
default:
{
cout<<"wrong input";
}
}
return 0;
}Assignments of For Loop (17 programs)
#include<iostream>
using namespace std;
int main()
{
int i;
for(i=1;i<=10;i++)
{
cout<<"*";
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i;
for(i=1;i<=7;i++)
{
cout<<"*";
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i;
for(i=1;i<=6;i++)
{
cout<<" *";
cout<<"nCOMPUHELPn";
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i;
for(i=0;i<=9;i++)
{
cout<<i<<" ";
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i;
for(i=9;i>=0;i--)
{
cout<<i<<" ";
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i;
for(i=5;i<=20;i++)
{
cout<<i<<" ";
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i;
for(i=45;i>=7;i--)
{
cout<<i<<" ";
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i;
for(i=1;i<=20;i+=2)
{
cout<<i<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i;
for(i=0;i<=20;i+=2)
{
cout<<i<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i,num;
cout<<"Enter number ";
cin>>num;
for(i=1;i<=num;i+=2)
{
cout<<i<<" ";
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i,num;
cout<<"Enter number ";
cin>>num;
for(i=0;i<=num;i+=2)
{
cout<<i<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i;
for(i=0;i<=50;i+=3)
{
cout<<i<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i,num,a=0,b=1,res=0;
cout<<"Enter the number till you want to print the fibonacci series : ";
cin>>num;
cout<<a<<" "<<b;
for(i=0;res<=num;i++)
{
res=a+b;
a=b;
b=res;
cout<<" "<<res;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i,num,prod=1;
cout<<"Enter the number till you want to print the series : ";
cin>>num;
for(i=0;prod<=num;i++)
{
cout<<prod<<" ";
prod=2*prod;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i,num,fact=1;
cout<<"Enter the number whose factorial you want to print: ";
cin>>num;
for(i=1;i<=num;i++)
{
fact=fact*i;
cout<<i<<"*";
}
cout<<"b="<<fact;
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i,num,sum=0;
cout<<"Enter the number whose sum of series you want to print: ";
cin>>num;
for(i=1;i<=num;i++)
{
sum=sum+i;
cout<<i<<"+";
}
cout<<"b="<<sum;
return 0;
}#include<iostream>
using namespace std;
int main()
{
int m,n,i,res=1;
cout<<"Enter the value for m and n";
cin>>m>>n;
for(i=1;i<=n;i++)
{
res=res*m;
}
cout<<res;
return 0;
}Assignments of Nested For Loop (24 programs)
#include<iostream>
using namespace std;
int main()
{
int row,col;
for(row=1;row<=3;row++)
{
for(col=1;col<=3;col++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int row,col,k=1;
for(row=1;row<=3;row++)
{
for(col=1;col<=3;col++)
{
cout<<k;
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int row,col;
for(row=1;row<=3;row++)
{
for(col=1;col<=3;col++)
{
cout<<col;
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int row,col;
for(row=1;row<=3;row++)
{
for(col=1;col<=3;col++)
{
cout<<row;
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int row,col,k=1;
for(row=1;row<=3;row++)
{
for(col=1;col<=3;col++)
{
cout<<k;
k++;
}
cout<<endl;
}
return 0;
}#include<iostream>u
using namespace std;
int main()
{
int row,col;
for(row=3;row>=1;row--)
{
for(col=1;col<=3;col++)
{
cout<<row;
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int row,col,k=9;
for(row=3;row>=1;row--)
{
for(col=1;col<=3;col++)
{
cout<<k;
k--;
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int row,col;
for(row=3;row>=1;row--)
{
for(col=3;col>=1;col--)
{
cout<<col;
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int row,col;
for(row=1;row<=3;row++)
{
for(col=1;col<=row;col++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int row,col;
for(row=3;row>=1;row--)
{
for(col=1;col<=row;col++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int row,col;
for(row=1;row<=3;row++)
{
for(col=1;col<=row;col++)
{
cout<<col;
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int row,col;
for(row=1;row<=3;row++)
{
for(col=1;col<=row;col++)
{
cout<<row;
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int row,col;
for(row=3;row>=1;row--)
{
for(col=3;col>=row;col--)
{
cout<<row;
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int row,col;
for(row=1;row<=3;row++)
{
for(col=3;col>=row;col--)
{
cout<<row;
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int row,col;
for(row=3;row>=1;row--)
{
for(col=row;col<=3;col++)
{
cout<<col;
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int row,col;
for(row=3;row>=1;row--)
{
for(col=3;col>=row;col--)
{
cout<<col;
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int row,col,k;
for(row=1;row<=3;row++)
{
for(k=row;k>=2;k--)
{
cout<<" ";
}
for(col=3;col>=row;col--)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int row,col,k;
for(row=3;row>=1;row--)
{
for(k=2;k>=row;k--)
{
cout<<" ";
}
for(col=1;col<=row;col++)
{
cout<<col;
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int row,col,k;
for(row=1;row=2;k--)
{
cout<<" ";
}
for(col=row;col<=3;col++)
{
cout<<row;
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int row,col;
for(row=1;row<=3;row++)
{
for(col=1;col<=row;col++)
{
cout<<"* ";
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i,j,k;
for(i=1;i=i;k--)
{
cout<<" ";
}
for(j=1;j<=i;j++)
{
cout<<"* ";
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int row,col;
//65 is ASCII value of A
for(row=65;row<=67;row++)
{
for(col=65;col<=row;col++)
{
cout<<char(col);//typecasting
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int row,col;
//65 is ASCII value of A
for(row=65;row=row;col--)
{
cout<<char(col);
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i,j,k;
//65 is ASCII value of A
for(i=65;i=i;k--)
{
cout<<" ";
}
for(j=65;j<=i;j++)
{
cout<<char(j);
}
cout<<endl;
}
return 0;
}Assignments of While and Do While Loops (8 programs)
#include<iostream>
using namespace std;
int main()
{
int i, m,n=0;
cout<<"enter nth number: ";
cin>>n;
while(m<=n)
{
cout<<m<<" ";
m++;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i,m;
cout<<"enter starting number: ");
cin>>m;
while(m>=0)
{
cout<<m<<" ";
m--;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i,n,cnt=1;
cout<<"enter any number:";
cin>>n;
while(cnt<=10)
{
cout<<endl<<n<<"*"<<cnt<<"="<<n*cnt;
cnt++;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i,num,x=0,y=1,z=1;
cout<<"enter any number:";
cin>>num;
cout<<x<<" "<<y;
do
{
z=x+y;
cout<<" "<<z;
x=y;
y=z;
}
while(z<=(num-x));
return 0;
}#include<iostream>
using namespace std;
int main()
{
int num=1,m1,m2,m3;
float avg=0;
while(num<=5)
{
cout<<"Enter your marks in maths:";
cin>>m1;
cout<<"nEnter your marks in english:";
cin>>m2;
cout<<"nEnter your marks in hindi:";
cin>>m3;
avg=(m1+m2+m3)/3;
cout<<"The average marks is: "<<avg;
cout<<endl;
num++;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int num=0,rev=0;
cout<<"enter a number: ";
cin>>num;
cout<<"The reverse of the number is: ";
while(num>=1)
{
rev=num%10;
num=num/10;
cout<<rev;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i,m,n,res=1,count=1;
cout<<"enter the number: ";
cin>>m;
cout<<"Enter the power: ";
cin>>n;
while(count<=n)
{
res=res*m;
count++;
}
cout<<"the result is: "<<res;
return 0;
}#include<iostream>
using namespace std;
int main()
{
int num=0, fac=1;
cout<<"enter the number: ";
cin>>num;
cout<<"The factorial of the number is: ";
while(num>=1)
{
fac=fac*num;
cout<<num<<"*";
num--;
}
cout<<"b="<<fac;
return 0;
}Assignments of Character (9 programs)
//program to accept a character from the user
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"Enter a character : ";
cin>>ch;
cout<<"n The character is "<<ch;
return 0;
}//Program to accept a character in lower case and display it in uppercase.
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"Enter a character : ";
cin>>ch;
ch=ch-32;
cout<<"n The character is "<<ch;
return 0;
}//program to accept a character from the user and display its ASCII value.
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"Enter a character :";
cin>>ch;
cout<<"n The ASCII value of "<<ch<<" is "<<int(ch);
return 0;
}// program to accept a character in uppercase and display it in lowercase.
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"Enter a character in upper case : ";
cin>>ch;
ch=ch+32;
cout<<"n The character in lower case is "<<ch;
return 0;
}//program to display the ASCII values of 0 and 9.
#include<iostream>
using namespace std;
int main()
{
char ch1,ch2;
ch1='0';
ch2='9';
cout<<"n The ASCII value of 0 is "<<int(ch1);
cout<<"n The ASCII value of 9 is "<<int(ch2);
return 0;
}//program to display the ASCII values fron 0 to 9.
#include<iostream>
using namespace std;
int main()
{
char ch;
ch='0';
int i;
for(i=0;i<=9;i++)
{
cout<<"n The ASCII of "<<ch<<" is "<<int(ch);
ch++;
}
return 0;
}/*program to display the ASCII values of all characters from A to Z.*/
#include<iostream> using namespace std;
int main()
{
char ch;
ch='A';
int i;
for(i=1;i<=26;i++)
{
cout<<"n The ASCII of "<<ch<<" is "<<int(ch);
ch++;
}
return 0;
}/* program to accept a character using getch(),getche()
and getchar() functions and display the character.*/
#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"n Accepting a character using getch() : ";
ch=getch();
cout<<"n The character is "<<ch;
cout<<"n Accepting a character using getche() : ";
ch=getche();
cout<<"n The character is "<<ch;
cout<<"n Accepting a character using getchar() : ";
ch=getchar();
cout<<"n The character is "<<ch;
return 0;
}/* program to let the user press any key on the keyboard until x
but count only how many upper case character keys are pressed on the keyboard?*/
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char ch;
int keys=0;
cout<<"n Press any key including upper case alphabets: ";
while(1)
{
ch=getche();
if((ch>=65)&&(ch<=90))
{
keys++;
}
if((ch=='x')||(ch=='X'))
{
break;
}
}//end of while loop
cout<<"n The uppercase character keys are pressed ("<<keys<<") Times";
getch();
return 0;
}Assignments of Single Dimensional Array (12 programs)
#include<iostream>
using namespace std;
int main()
{
int arr[5],i;
cout<<"Enter array elements ";
for(i=0;i<=4;i++)
{
cin<<arr[i];
}
for(i=0;i<=4;i++)
{
cout<<"n Element is "<<arr[i];
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[5],i;
cout<<"Enter array elements ";
for(i=0;i<=4;i++)
{
cin>>arr[i];
}
for(i=4;i>=0;i--)
{
cout<<"n Element is "<<arr[i];
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[5],arr1[5],i;
cout<<"Enter array elements ";
for(i=0;i<=4;i++)
{
cin>>arr[i]);
}
cout<<"n The copy of array elements is :n";
for(i=0;i<=4;i++)
{
arr1[i]=arr[i];
cout<<"n Element is "<<arr1[i];
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[5],arr1[5],i,j;
cout<<"Enter array elements ";
for(i=0;i<=4;i++)
{
cin>>arr[i];
}
cout<<"n The copy of array elements is :n");
j=0;
for(i=4;i>=0;i--)
{
arr1[j]=arr[i];
cout<<"n Element is ",arr1[j];
j++;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[5],i,sum=0;
cout<<"Enter array elements ";
for(i=0;i<=4;i++)
{
cin>>arr[i];
}
cout<<"nThe sum of array elements is :n";
for(i=0;i<=4;i++)
{
sum=sum+arr[i];
cout<<arr[i]<<"+";
}
cout<<"b="<<sum;
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr1[5],arr2[5],sum[5],i;
cout<<"Enter array elements in first array ";
for(i=0;i<=4;i++)
{
cin>>arr1[i];
}
cout<<"Enter array elements in second array ";
for(i=0;i<=4;i++)
{
cin>>arr2[i];
}
cout<<"n The sum of array elements is :n";
for(i=0;i<=4;i++)
{
sum[i]=arr1[i]+arr2[i];
cout<<" "<<sum[i];
}
return 0;
}#include<iostream> using namespace std;
int main()
{
int arr1[5],arr2[5],diff[5],i;
cout<<"Enter array elements in first array ";
for(i=0;i<=4;i++)
{
cin>>arr1[i];
}
cout<<"Enter array elements in second array ";
for(i=0;i<=4;i++)
{
cin>>arr2[i];
}
cout<<"n THE DIFFERENCE OF EACH ELEMENT OF THE TWO ARRAYS IS:n";
for(i=0;i<=4;i++)
{
diff[i]=arr1[i]-arr2[i];
cout<<" "<<diff[i];
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[5],i,large=0,loc;
cout<<"Enter the elements of an array ";
for(i=0;i<=4;i++)
{
cin>>arr[i];
}
cout<<"The elements of an array is:n ";
for(i=0;i<=4;i++)
{
cout<<"nElement is "<<arr[i];
if(arr[i]>large)
{
large=arr[i];
loc=i+1;
}
}
cout<<"n The greatest element is : "<<large;
cout<<"n The location is: "<<loc;
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[5],i,small,loc;
cout<<"Enter the elements of an array ";
for(i=0;i<=4;i++)
{
cin>>arr[i];
}
small=arr[0];
cout<<"The elements of an array is:n ";
for(i=0;i<=4;i++)
{
cout<<"nElement is "<<arr[i];
if(small>arr[i])
{
small=arr[i];
loc=i+1;
}
}
cout<<"n The smallest element is :"<<small;
cout<<"n The location is: "<<loc;
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[5],i,j,temp;
cout<<"Enter the elements of an array ";
for(i=0;i<=4;i++)
{
cin>>arr[i];
}
for(i=0;i<=4;i++)
{
for(j=i+1;j<5;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<"nThe sorted array is: n";
for(i=0;i<=4;i++)
{
cout<<" "<<arr[i];
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[5],i,j,temp;
cout<<"Enter the elements of an array ";
for(i=0;i<=4;i++)
{
cin>>arr[i];
}
for(i=0;i<=4;i++)
{
for(j=i+1;j<5;j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<"nThe sorted array is: n";
for(i=0;i<=4;i++)
{
cout<<" "<<arr[i];
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[5],i,n,flag=0;
cout<<"Enter the elements of an array ";
for(i=0;i<=4;i++)
{
cin>>arr[i];
}
cout<<"The elements of an array is: n";
for(i=0;i<=4;i++)
{
cout<<" "<<arr[i];
}
cout<<"n Enter any number which you want to search: ";
cin>>n;
for(i=0;i<=4;i++)
{
if(arr[i]==n)
{
flag=1;
cout<<"n Number is found at "<<(i+1)<<" location";
}
}
if(flag==0)
{
cout<<"Number is not found";
}
return 0;
}Assignments of Two Dimensional Array (15 programs)
#include<iostream>
using namespace std;
int main()
{
int arr[3][3],i,j;
cout<<"Enter array elements: ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>arr[i][j];
}
}
cout<<"The array elements are:";
cout<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<" "<<arr[i][j];
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[2][2],arr1[2][2],i,j;
cout<<"Enter array elements: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>arr[i][j];
}
}
cout<<"n The copy of the matrix is:";
cout<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
arr1[i][j]=arr[i][j];
cout<<" "<<arr1[i][j];
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[2][2],arr1[2][2],i,j;
cout<<"Enter array elements: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>arr[i][j];
}
}
cout<<"n The reverse copy of the matrix is: ";
cout<<endl;
for(i=1;i>=0;i--)
{
for(j=1;j>=0;j--)
{
arr1[i][j]=arr[i][j];
cout<<" "<<arr1[i][j];
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[2][2],arr1[2][2],i,j;
cout<<"Enter array elements: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>arr[i][j];
}
}
cout<<"n The reverse copy of the matrix is: ";
cout<<endl;
for(i=1;i>=0;i--)
{
for(j=1;j>=0;j--)
{
arr1[i][j]=arr[i][j];
cout<<" "<<arr1[i][j];
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[2][2],i,j;
cout<<"Enter the elements of the matrix: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>arr[i][j];
}
}
cout<<"n The reverse of the matrix is: ";
cout<<endl;
for(i=1;i>=0;i--)
{
for(j=1;j>=0;j--)
{
cout<<" "<<arr[i][j];
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[2][2],i,j,sum=0;
cout<<"Enter the elements of the matrix: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>arr[i][j];
}
}
cout<<"n The sum of elements is: ";
cout<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
sum=sum+arr[i][j];
cout<<arr[i][j]<<"+";
}
}
cout<<"b="<<sum;
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr1[3][3],arr2[3][3],arr3[3][3],i,j;
cout<<"Enter the elements first matrix: ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>arr1[i][j];
}
}
cout<<"n Enter the element of second matrix: ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>arr2[i][j];
}
}
cout<<"n The sum of two matrices is:n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
arr3[i][j]=arr1[i][j]+arr2[i][j];
cout<<" "<<arr3[i][j];
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr1[3][3],arr2[3][3],arr3[3][3],i,j;
cout<<"Enter the elements first matrix: ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>arr1[i][j];
}
}
cout<<"n Enter the element of second matrix: ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>arr2[i][j];
}
}
cout<<"n The subtraction of two matrices is: ";
cout<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
arr3[i][j]=arr1[i][j]-arr2[i][j];
cout<<" "<<arr3[i][j];
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr1[3][3],arr2[3][3],arr3[3][3],i,j;
cout<<"Enter the elements first matrix: ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>arr1[i][j];
}
}
cout<<"n Enter the element of second matrix: ";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>arr2[i][j];
}
}
cout<<"n The division of two matrices is:";
cout<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
arr3[i][j]=arr1[i][j]/arr2[i][j];
cout<<" "<<arr3[i][j];
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[2][2],arr1[2][2],arr2[2][2],i,j,k;
cout<<"Enter the elements of first matrix: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>arr[i][j];
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<" "<<arr[i][j];
}
cout<<endl;
}
cout<<"n Enter the elements of the second matrix: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>arr1[i][j];
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<" "<<arr1[i][j];
}
cout<<"n");
}
cout<<"nThe multiplication is");
cout<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
arr2[i][j]=0;
for(k=0;k<2;k++)
{
arr2[i][j]=arr2[i][j]+arr[i][k]*arr1[k][j];
}
cout<<" "<<arr2[i][j];
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[2][2],i,j,large=0,k,l;
cout<<"Enter the element: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>arr[i][j];
}
}
cout<<"n The elements of the matrix are: ";
cout<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<" "<<arr[i][j];
}
cout<<endl;
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
if(arr[i][j]>large)
{
large=arr[i][j];
k=i+1;
l=j+1;
}
}
}
cout<<"The greatest element is "<<large<<" and present in "<<k<<" row and in "<<l<<" column";
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[2][2],i,j,small,k,l;
cout<<"Enter the element: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>arr[i][j];
}
}
cout<<"n The elements of the matrix are: ";
cout<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<" "<<arr[i][j];
}
cout<<endl;
}
small=arr[0][0];
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
if(arr[i][j]<small)
{
small=arr[i][j];
k=i+1;
l=j+1;
}
}
}
cout<<"The smallest element is <<"small<<" and present in "<<k<<" row and in "<<l<<" column";
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[2][2],i,j,k,l;
int small;
cout<<"Enter the element: ";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>arr[i][j];
}
}
cout<<"n The elements of the matrix are: ";
cout<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<" "<<arr[i][j];
}
cout<<endl;
}
cout<<"n The transpose of the matrix is: ";
cout<<endl;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<" "<<arr[j][i];
}
cout<<endl;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[3][3],i,j,n,flag=1;
cout<<"Enter the array elements: n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>arr[i][j];
}
}
cout<<"n The elements of array is: ";
cout<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<" "<<arr[i][j];
}
cout<<endl;
}
cout<<"n Enter any no.which you want to search: ";
cin>>n;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(arr[i][j]==n)
{
flag=0;
cout<<"Number is found at "<<(i+1)<<" row and "<<(j+1)<<" column";
}
}
}
if(flag==1)
{
cout<<"number is not found";
}
return 0;
}#include<iostream> using namespace std;
int main()
{
int arr[3][3],i,j,n,flag=1;
cout<<"Enter the array elements: n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>arr[i][j]);
}}
cout<<"n The elements of array is: ");
cout<<"n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"%d",arr[i][j]);
}
cout<<"n");
}
cout<<"n Enter any no.which you want to search: ");
cin>>n);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(arr[i][j]==n)
{
flag=0;
cout<<"Number is found at %d row and %d column",i+1,j+1);
}}}
if(flag==1)
{
cout<<"number is not found");
}
return 0;
}Assignments of Character Array (10 programs)
#include<iostream>
using namespace std;
int main()
{
char name[30];
cout<<"Enter a string ";
//cin>>name;
//cin cannot read spaces
//gets(name);//read the string with spaces
//or
cin.getline(name,30);// read the string with spaces
cout<<"You entered: "<<name;
return 0;
}#include<iostream>
using namespace std;
int main()
{
char name[20],i,j;
cout<<"Enter a string";
gets(name);
for(i=0;name[i]!='';i++);
//Loop with i will count the length of a string
//So, i will store the length of a string
for(j=i-1;j>=0;j--)
{
cout<<name[j];
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
char name[20];
int i;
cout<<"Enter a string ";
gets(name);
for(i=0;name[i]!='';i++);
cout<<"The length of the string is: "<<i;
return 0;
}#include<iostream>
using namespace std;
int main()
{
char name[20];
int i;
cout<<"Enter a string in uppercase: ";
gets(name);
cout<<"nThe string in lowercase: ";
for(i=0;name[i]!='';i++)
{
if((name[i]>='A')&&(name[i]<='Z'))
{
name[i]=name[i]+32;
}
}
cout<<name;
return 0;
}#include<iostream>
using namespace std;
int main()
{
char name[20];
int i;
cout<<"Enter a string in lowercase: ";
gets(name);
cout<<"nThe string in uppercase: ";
for(i=0;name[i]!='';i++)
{
if((name[i]>='a')&&(name[i]<='z'))
{
name[i]=name[i]-32;
}
}
cout<<name;
return 0;
}#include<iostream>
using namespace std;
int main()
{
char name[20];
int i,cnt=0;
cout<<"Enter a string in lowercase:";
gets(name);
cout<<"nThe vowels are: ";
for(i=0;name[i]!='';i++)
{
if((name[i]=='a')||(name[i]=='e')||(name[i]=='i')||(name[i]=='o')||(name[i]=='u')||(name[i]=='A')||(name[i]=='E')||(name[i]=='I')||(name[i]=='O')||(name[i]=='U'))
{
cout<<name[i];
cnt++;
}
}
cout<<"n The number of vowel is: "<<cnt;
return 0;
}#include<iostream>
using namespace std;
int main()
{
char name[20];
int i,cnt=0;
cout<<"Enter a string:";
gets(name);
cout<<"nThe consonents are: ";
for(i=0;name[i]!='';i++)
{
if((name[i]!='a')&&(name[i]!='e')&&(name[i]!='i')&&(name[i]!='o')&&(name[i]!='u')&&(name[i]!='A')&&(name[i]!='E')&&(name[i]!='I')&&(name[i]!='O')&&(name[i]!='U'))
{
cout<<name[i];
cnt++;
}
}
cout<<"n The number of consonents are: "<<cnt;
return 0;
}#include<iostream>
using namespace std;
int main()
{
char name[20],name1[20];
int i,cnt=0;
cout<<"Enter a string ";
gets(name);
cout<<"nThe copy of string: ";
for(i=0;name[i]!='';i++)
{
name1[i]=name[i];
cout<<name[i];
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
char name[50];
int i,wrd=0;
cout<<"Enter a sentence: ";
gets(name);
cout<<"The number of words are: ";
for(i=0;name[i]!='';i++)
{
if(name[i]==' ')
{
wrd++;
}
}
cout<<wrd+1;
return 0;
}#include<iostream>
using namespace std;
int main()
{
char str1[20],str2[20],str3[50];
int i,j;
cout<<"Enter first string: ";
gets(str1);
cout<<"Enter second string: ";
gets(str2);
for(i=0;str1[i]!='';i++)
{
str3[i]=str1[i];
}
for(j=0;str2[j]!='';j++)
{
str3[i]=str2[j];
i++;
}
str3[i]='';
cout<<endl<<str3;
return 0;
}Assignments of Pointer (7 programs)
#include<iostream>
using namespace std;
int main()
{
int num1,num2,res;
int *aptr,*bptr,*cptr;
cout<<"Enter first number ";
cin>>num1;
cout<<"Enter second number ";
cin>>num2;
aptr=&num1;
bptr=&num2;
cptr=&res;
*cptr=(*aptr)+(*bptr);
cout<<"n sum is "<<*cptr;
}#include<iostream>
using namespace std;
int main()
{
int num1,num2,temp;
int *aptr,*bptr;
cout<<"Enter first number ";
cin>>num1;
cout<<"Enter second number ";
cin>>num2;
cout<<"n Values before swapping is : num1= "<<num1<<" num2= "<<num2;
aptr=&num1;
bptr=&num2;
temp=*aptr;
*aptr=*bptr;
*bptr=temp;
cout<<"n Values after swapping is : num1= "<<*aptr<<" num2= "<<*bptr;
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[5],i,*ptr;
for(i=0;i<5;i++)
{
cout<<"Enter array elements ";
cin>>arr[i];
}
cout<<"n The reverse of elements are: n";
for(i=4;i>=0;i--)
{
ptr=&arr[i];
cout<<" "<<*ptr;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[5],i,*ptr,sum=0;
for(i=0;i<5;i++)
{
cout<<"Enter array elements ";
cin>>arr[i];
}
cout<<"n The sum of array elements is: ";
for(i=0;i<=4;i++)
{
ptr=&arr[i];
sum=sum+(*ptr);
}
cout<<sum;
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr1[5],arr2[5],sum[5],i;
int *ptr1,*ptr2,*res;
cout<<"Enter array elements in first array ";
for(i=0;i<=4;i++)
{
cin>>arr1[i];
}
cout<<"Enter array elements in second array ";
for(i=0;i<=4;i++)
{
cin>>arr2[i];
}
cout<<"n The sum of array elements is :n";
for(i=0;i<=4;i++)
{
ptr1=&arr1[i];
ptr2=&arr2[i];
res=&sum[i];
*res=*ptr1+(*ptr2);
cout<<" "<<*res;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int i,num,*ptr;
cout<<"Enter the number for the limit of series: ";
cin>>num;
for(i=0;i<=num;i++)
{
ptr=&i;
cout<<" "<<*ptr;
}
return 0;
}#include<iostream>
using namespace std;
int main()
{
int arr[3][3],i,j,sum=0;
int *ptr;
cout<<"Enter array elements: n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>arr[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
ptr=&arr[i][j];
sum=sum+(*ptr);
}
}
cout<<"nThe sum of array elements is: "<<sum;
return 0;
}Assignments of Function (14 programs)
#include<iostream>
using namespace std;
void sum() //definition or body of sum()
{
int num1,num2,num3,num4,res;
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
cout<<"Enter third number: ";
cin>>num3;
cout<<"Enter fourth number: ";
cin>>num4;
res=num1+num2+num3+num4;
cout<<"n The sum of all numbers is "<<res;
}
int main()
{
cout<<"Calling sum function: n";
//function with no arguments and no return type
sum();//calling sum() function
return 0;
}#include<iostream>
using namespace std;
int num1,num2,res;//global declaration
void input()
{
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
}//end of input function
void sum()
{
res=num1+num2;
cout<<"n The sum of numbers is "<<res;
}//end of sum function
void subtract()
{
if(num1>num2)
{
res=num1-num2;
}
else
{
res=num2-num1;
}
cout<<"n The subtraction of numbers is "<<res;
}//end of subtract function
void multiply()
{
res=num1*num2;
cout<<"n The multiplication of numbers is "<<res;
}//end of multiply function
void division()
{
res=num1/num2;
cout<<"n The division of numbers is "<<res;
}//end of division function
int main()
{
char ch;
cout<<"nPress + for addition n";
cout<<"nPress - for subtraction n";
cout<<"nPress * for multiplication n";
cout<<"nPress / for division n";
cin>>ch;
switch(ch)
{
case '+':
input();
sum(); //calling sum function
break;
case '-':
input();
subtract();//calling subtract() function
break;
case '*':
input();
multiply();//calling multiply() function
break;
case '/':
input();
division();//calling division() function
break;
default:
cout<<"Invalid choice";
}
return 0;
}//end of main#include<iostream>
using namespace std;
void odd(int n)
{
int i;
cout<<"n The odd series is : ";
for(i=1;i<=n;i+=2)
{
cout<<" "<<i;
}//end of loop
}//end of function
int main()
{
int num;
cout<<"Enter the nth number: ";
cin>>num;
odd(num);
return 0;
}//end of main#include<iostream>
using namespace std;
void even(int n)
{
int i;
cout<<"n The even series is : ";
for(i=0;i<=n;i+=2)
{
cout<<" "<<i;
}//end of loop
}//end of function
int main()
{
int num;
cout<<"Enter the nth number: ";
cin>>num;
even(num);
return 0;
}//end of main#include<iostream>
using namespace std;
void display(char ch1,int n)
{
int i;
for(i=1;i<=n;i++)
{
cout<<" "<<ch1;
}
}
int main()
{
char ch;
int num;
cout<<"Enter a character: ";
cin>>ch;
cout<<"Enter the limit: ";
cin>>num;
display(ch,num);
return 0;
}//end of main#include<iostream>
using namespace std;
void series(int a,int b)
{
int i;
cout<<"n The series is : ";
for(i=a;i<=b;i++)
{
cout<<" "<<i;
}
}
int main()
{
int m,n;
cout<<"Enter starting value: ";
cin>>m;
cout<<"Enter ending value: ";
cin>>n;
series(m,n);
return 0;
}//end of main#include<iostream>
using namespace std;
void is_pos_neg(int n)
{
if(n>0)
cout<<"Number is positive";
else if(n<0)
cout<<"Number is negative";
else
cout<<"You entered zero";
}
int main()
{
int num;
cout<<"Enter any number: ";
cin>>num;
is_pos_neg(num);
return 0;
}//end of main#include<iostream>
using namespace std;
void fibonacci(int n)
{
int i,j=0,k=1,m;
cout<<j<<" "<<k;
for(i=1;i<=n;i++)
{
m=j+k;
j=k;
k=m;
if(m<=n)
cout<<" "<<m;
}//end of loop
}//end of function
int main()
{
int num;
cout<<"Enter ending value: ";
cin>>num;
fibonacci(num);
return 0;
}//end of main#include<iostream>
using namespace std;
void to_upper(char a)
{
int i;
cout<<"The alphabet in uppercase is: ";
if((a>='a')&&(a<='z'))
{
a=a-32;
}//end of if
cout<<a;
}//end of function
int main()
{
char ch;
cout<<"Enter an alphabet in lowercase: ";
cin>>ch;
to_upper(ch);
return 0;
}//end of main#include<iostream>
using namespace std;
void to_lower(char a)
{
int i;
cout<<"The alphabet in lowercase is: ";
if((a>='A')&&(a<='Z'))
{
a=a+32;
}//end of if
cout<<a;
}//end of function
int main()
{
char ch;
cout<<"Enter an alphabet in Uppercase: ";
cin>>ch;
to_lower(ch);
return 0;
}//end of main#include<iostream>
using namespace std;
void series(int n)
{
int i,j,sum=0;
cout<<"Series: ";
for(i=1;i<=n;i++)
{
j=i*i;
sum=sum+j;
cout<<j<<"+";
}
cout<<"b="<<sum;
}//end of function
int main()
{
int num;
cout<<"Enter a number: ";
cin>>num;
series(num);
return 0;
}//end of main#include<iostream>
using namespace std;
int factorial(int n)
{
int i,fac=1;
cout<<"The factorial is: ";
for(i=n;i>=1;i--)
{
fac=fac*i;
cout<<i<<"*";
}
return(fac);
}//end of function
int main()
{
int num,ans;
cout<<"Enter a number: ";
cin>>num;
ans=factorial(num);
cout<<"b="<<ans;
return 0;
}//end of main#include<iostream>
using namespace std;
void swap(int a,int b)
{
int c;
cout<<"nThe value of numbers before swapping is: ";
cout<<"n First: "<<a;
cout<<"n Second : "<<b;
c=a;
a=b;
b=c;
cout<<"nThe value of numbers after swapping is:";
cout<<"n First: "<<a;
cout<<"n Second: "<<b;
}//end of function
int main()
{
int x,y;
cout<<"Enter First number: ";
cin>>x;
cout<<"Enter Second number: ";
cin>>y;
swap(x,y);
return 0;
}//end of main#include<iostream>
using namespace std;
int fact(int value)
{
int ans;
if((value==0)||(value==1))
return(1);
else
ans=value*fact(value-1);
return(ans);
}//end of function
int main()
{
int i,n,fac;
cout<<"Enter any number: ";
cin>>n;
fac=fact(n);
cout<<"The factorial is: ";
for(i=n;i>=1;i--)
{
cout<<i<<"*";
}
cout<<"b="<<fac;
return 0;
}//end of mainAssignments of Structure (8 programs)
#include<iostream>
using namespace std;
struct Student
{
char name[10];
int rollno;
float fee;
};
int main()
{
Student st;
cout<<"Enter name: ";
gets(st.name);
cout<<"Enter rollno: ";
cin>>st.rollno;
cout<<"Enter fee: ";
cin>>st.fee;
cout<<"------------------";
cout<<"nName is "<<st.name;
cout<<"nRollno is "<<st.rollno;
cout<<"nFee is "<<st.fee;
return 0;
}//end of main#include<iostream>
using namespace std;
struct Student{ int rollno; int marks[5];};int main(){ Student st; int i; cout<<"Enter rollno: "; cin>>st.rollno; for(i=0;i<5;i++) { cout<<"Enter marks in "<<(i+1)<<" subject: "; cin>>st.marks[i]; } cout<<"------------------"; cout<<"nRollno is "<<st.rollno; for(i=0;i<5;i++) { cout<<"nMarks in "<<(i+1)<<" subject are: "<<st.marks[i]; } return 0;
}//end of main#include<iostream>
using namespace std;
struct Student
{
char name[20];
int marks[9];
};
int main()
{
Student st; int i,sum=0; float avg; cout<<"Enter name: "; gets(st.name); for(i=0;i<9;i++) { cout<<"Enter marks in "<<(i+1)<<" subject: "; cin>>st.marks[i]; sum=sum+st.marks[i]; } avg=sum/9; cout<<"nName is "<<st.name; cout<<"nAverage is: "<<avg;
return 0;
}//end of main#include<iostream>
using namespace std;
struct employee
{
char name[10];
int salary;
int id;
};
int main()
{
int i;
employee emp[5];
for(i=0;i<5;i++)
{
cout<<"Enter name ";
//gets(emp[i].name);
cin>>emp[i].name;
cout<<"Enter salary ";
cin>>emp[i].salary;
cout<<"Enter id ";
cin>>emp[i].id;
}
for(i=0;i<5;i++)
{
cout<<"n Name is "<<emp[i].name;
cout<<"n Salary is "<<emp[i].salary;
cout<<"n Id is "<<emp[i].id;
cout<<"n--------------------------";
}
return 0;
}//end of main#include<iostream>
using namespace std;
struct student
{
char name[10];
int subject[5];
};
int main()
{
student st[5];
int sum,avg,i,j;
for(i=0;i<5;i++)
{
sum=0;
cout<<"Enter name ";
cin>>st[i].name;
cout<<"nEnter the marks of "<<(i+1)<<" studentnn";
for(j=0;j<5;j++)
{
cout<<"Enter the marks of "<<(j+1)<<" subject ";
cin>>st[i].subject[j];
sum=sum+st[i].subject[j];
}
avg=sum/5;
cout<<"n Average marks of "<<(i+1)<<" student is "<<avg<<endl<<endl;
}
return 0;
}//end of main#include<iostream>
using namespace std;
struct student
{
char name[10];
int subject[5];
};
int main()
{
student st,*ptr;
ptr=&st;
int sum=0,avg,i;
cout<<"Enter name: ";
gets(ptr->name);
for(i=0;i<5;i++)
{
cout<<"Enter the marks of "<<(i+1)<<" subject: ";
cin>>ptr->subject[i];
sum=sum+ptr->subject[i];
}
avg=sum/5;
cout<<"n Average of five subjects is "<<avg;
return 0;
}//end of main#include<iostream>
using namespace std;
struct student
{
char name[10];
int subject[5];
};
int main()
{
student st[5],*ptr;
int sum,avg,i,j;
for(i=0;i<5;i++)
{
ptr=&st[i];
sum=0;
cout<<"Enter name: ";
cin>>ptr->name;
cout<<"nEnter the marks of "<<(i+1)<<" studentn";
for(j=0;j<5;j++)
{
cout<<"nEnter the marks of "<<(j+1)<<" subject: ";
cin>>ptr->subject[j];
sum=sum+ptr->subject[j];
}
avg=sum/5;
cout<<"n Average marks of "<<(i+1)<<" student is "<<avg;
cout<<"nn-----------------------------------n";
}
return 0;
}//end of main#include<iostream>
using namespace std;
struct student
{
int rollno;
char name[10];
int fee;
}st;
void display(student *st)
{
cout<<"n Name is "<<st->name;
cout<<"n Rollno is "<<st->rollno;
cout<<"n Fee is "<<st->fee;
}
int main()
{
cout<<"Enter name : ";
cin>>st.name;
cout<<"Enter rollno : ";
cin>>st.rollno;
cout<<"Enter fee : ";
cin>>st.fee;
display(&st);
return 0;
}//end of mainAssignments of File Handling (13 programs)
#include<iostream> using namespace std;
int main()
{
FILE *fptr;
fptr=fopen("first.txt","w");
fcout<<fptr,"Welcome to Compuhelp");
cout<<"Data Saved");
fclose(fptr);
return 0;
}//end of main#include<iostream> using namespace std;
int main()
{
FILE *fptr;
int num;
fptr=fopen("first.txt","w");
cout<<"Enter a number: ");
cin>>num);
fcout<<fptr,"%d",num);
cout<<"Data saved");
fclose(fptr);
return 0;
}//end of main#include<iostream> using namespace std;
int main()
{
FILE *fptr;
int num;
fptr=fopen("first.txt","w");
cout<<"Enter a number ");
cin>>num);
fcout<<fptr,"%d",num);
cout<<"Data saved");
fclose(fptr);
fptr=fopen("first.txt","r");
fscanf(fptr,"%d",&num);
cout<<"nnValue of num= %d",num);
fclose(fptr);
return 0;
}//end of main#include<iostream> using namespace std;
#include<stdlib.h>
int main()
{
FILE *fptr;
int num;
fptr=fopen("first.txt","w");
cout<<"Enter a number ");
scanf("%d",&num);
fcout<<fptr,"%d",num);
cout<<"Data saved");
fclose(fptr);
fptr=fopen("first.txt","r");
fscanf(fptr,"%d",&num);
cout<<"n Now writing into another file");
fptr=fopen("second.txt","w");
fcout<<fptr,"%d",num);
fclose(fptr);
return 0;
}//end of main#include<iostream> using namespace std;
#include<stdlib.h>
int main()
{
FILE *fptr;
char ch;
fptr=fopen("string.txt","w");
if(fptr==NULL)
{
cout<<"Error in opening file ");
exit(1);
}
fcout<<fptr,"welcome you in COMPUHELP");
cout<<"Data savedn");
fclose(fptr);
fptr=fopen("string.txt","r");
while(ch!=EOF)
{
ch=fgetc(fptr);
cout<<"%c",ch);
}
fclose(fptr);
return 0;
}//end of main#include<iostream> using namespace std;
#include<stdlib.h>
int main()
{
FILE *fptr;
char ch;
int count=0;
fptr=fopen("string.txt","w");
if(fptr==NULL)
{
cout<<"Error in opening file ");
exit(1);
}
fcout<<fptr,"welcome you in COMPUHELP");
cout<<"Data savedn");
fclose(fptr);
fptr=fopen("string.txt","r");
while(ch!=EOF)
{
ch=fgetc(fptr);
cout<<"%c",ch);
count++;
}
cout<<"n The number of characters are: %d",count);
fclose(fptr);
return 0;
}//end of main#include<iostream> using namespace std;
#include<stdlib.h>
int main()
{
FILE *fptr;
char ch;
int count=0;
fptr=fopen("string.txt","w");
if(fptr==NULL)
{
cout<<"Error in opening file ");
exit(1);
}
fcout<<fptr,"welcome you in COMPUHELP");
cout<<"Data savedn");
fclose(fptr);
fptr=fopen("string.txt","r");
while(ch!=EOF)
{
ch=fgetc(fptr);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
{
count++;
cout<<"%c ",ch);
}
}
cout<<"n The number of vowels are: %d",count);
fclose(fptr);
return 0;
}//end of main#include<iostream> using namespace std;
#include<stdlib.h>
int main()
{
FILE *fptr;
char ch;
int upr=0,lwr=0;
fptr=fopen("string.txt","w");
if(fptr==NULL)
{
cout<<"Error in opening file ");
exit(1);
}
fcout<<fptr,"welcome you in COMPUHELP");
cout<<"Data savedn");
fclose(fptr);
fptr=fopen("string.txt","r");
while(ch!=EOF)
{
ch=fgetc(fptr);
if(ch>=65 && ch<=90)
{
upr++;
//cout<<"%c ",ch);
}
if(ch>=97 && ch<=122)
{
lwr++;
//cout<<"%c ",ch);
}
}
cout<<"n The number of Uppers are: %d",upr);
cout<<"n The number of Lowers are: %d",lwr);
fclose(fptr);
return 0;
}//end of main#include<iostream> using namespace std;
#include<stdlib.h>
int main()
{
FILE *fptr;
ch;
int space=0,word=0;
fptr=fopen("string.txt","w");
if(fptr==NULL)
{
cout<<"Error in opening file ");
exit(1);
}
fcout<<fptr,"welcome you in COMPUHELP");
cout<<"Data savedn");
fclose(fptr);
fptr=fopen("string.txt","r");
while(ch!=EOF)
{
ch=fgetc(fptr);
if(ch==' ')
{
space++;
word++;
}
}
cout<<"n The number of Spaces are: %d",space);
cout<<"n The number of Words are: %d",(word+1));
fclose(fptr);
return 0;
}//end of main#include<iostream> using namespace std;
#include<stdlib.h>
int main()
{
FILE *fptr;
char ch;
int line=0;
fptr=fopen("string.txt","w");
if(fptr==NULL)
{
cout<<"Error in opening file ");
exit(1);
}
fcout<<fptr,"welcome you in COMPUHELPn Thanks to Join CompuhelpnThanksnBye");
cout<<"Data savedn");
fclose(fptr);
fptr=fopen("string.txt","r");
while(ch!=EOF)
{
ch=fgetc(fptr);
if(ch=='n')
{
line++;
}
}
cout<<"n The number of Lines are: %d",(line+1));
fclose(fptr);
return 0;
}//end of main#include<iostream> using namespace std;
int main()
{
FILE *fptr;
int num[10];
int i,even=0,odd=0;
fptr=fopen("first.txt","w");
cout<<"Enter number's ");
for(i=0;i<10;i++)
{
cin>>num[i]);
}
for(i=0;i<10;i++)
{
fcout<<fptr,"%d ",num[i]);
}
cout<<"Data saved");
fclose(fptr);
fptr=fopen("first.txt","r");
for(i=0;i<10;i++)
{
fscanf(fptr,"%d",&num[i]);
if(num[i]%2==0)
{
even++;
}
else
{
odd++;
}
}
cout<<"n Number of even numbers are %d",even);
cout<<"n Number of odd numbers are %d",odd);
fclose(fptr);
return 0;
}//end of main#include<iostream> using namespace std;
#include<stdlib.h>
int main()
{
FILE *fptr;
char ch,ch1,name[50];
int line=0;
fptr=fopen("string.txt","w");
if(fptr==NULL)
{
cout<<"Error in opening file ");
exit(1);
}
cout<<"Enter a string ");
gets(name);
fcout<<fptr,"%s",name);
cout<<"Data savedn");
fclose(fptr);
fptr=fopen("string.txt","r");
cout<<"Enter another character ");
scanf("%c",&ch1);
while(ch!=EOF)
{
ch=fgetc(fptr);
if(ch==ch1)
{
ch=ch-32;
cout<<"%c",ch);
}
else
{
cout<<"%c",ch);
}
}
fclose(fptr);
return 0;
}//end of main#include<iostream> using namespace std;
#include<stdlib.h>
struct student
{
char name[20];
int rollno;
float fee;
};//end of structure
int main()
{
struct student st;
FILE *fptr;
char ch,ch1,name[50];
int line=0;
fptr=fopen("data.txt","w");
if(fptr==NULL)
{
cout<<"Error in opening file ");
exit(1);
}
cout<<"Enter a name ");
gets(st.name);
cout<<"Enter rollno ");
cin>>st.rollno);
cout<<"Enter fee ");
scanf("%f",&st.fee);
fcout<<fptr,"%s",st.name);
fcout<<fptr,"n%d",st.rollno);
fcout<<fptr,"n%f",st.fee);
cout<<"nData saved");
fclose(fptr);
return 0;
}//end of mainSolved Assignments (Using OOP)
Class-based C++ solutions.
Assignments of Classes and Objects (6 programs)
#include<iostream>using namespace std;class Math{ int num1,num2,num3;public: void input() { cout<<"Enter first number: "; cin>>num1; cout<<"Enter second number: "; cin>>num2; } void sum() { num3=num1+num2; cout<<"Sum is "<<num3; }};//end of classint main(){ Math mt; mt.input(); mt.sum(); return 0;}#include<iostream>using namespace std;class Student{ char name[10]; int rollno; float fee;public: void input(); void output();};//end of classvoid Student::input(){ cout<<"Enter name "; cin>>name; cout<<"Enter rollno "; cin>>rollno; cout<<"Enter fees "; cin>>fee;}void Student::output(){ cout<<"nName is "<<name; cout<<"nRollno is "<<rollno; cout<<"nFee is "<<fee;}int main(){ Student st; st.input(); st.output(); return 0;}#include<iostream>
using namespace std;
class Student
{
int rollno,sum;
float marks[2],per;
public:
void get_data()
{
int i;
sum=0;
cout<<"Enter rollno ";
cin>>rollno;
for(i=0;i<2;i++)
{
cout<<"Enter marks ";
cin>>marks[i];
sum=sum+marks[i];
}
}
void percent()
{
per=sum/2;
cout<<"nPercentage is "<<per;
}};
int main()
{
Student st[2];
int i;
for(i=0;i<2;i++)
{
st[i].get_data();
cout<<"nPercentage of student "<<(i+1);
st[i].percent();
cout<<endl<<"----------------------------------"<<endl;
}
return 0;
}#include<iostream>
using namespace std;
class Demo{ int num; public: void get_data() { cout<<"Enter number "; cin>>num; } void sum(Demo dm) { int res; res=num+dm.num; cout<<"Sum is "<<res; }
};int main(){ Demo d1,d2; d1.get_data(); d2.get_data(); d1.sum(d2); return 0;}#include<iostream>
using namespace std;
class Demo{ int num; public: void get_data() { cout<<"Enter number "; cin>>num; } void sum(Demo &dm) { int temp; cout<<"nValues before swapping are "<<num<<" and "<<dm.num; temp=num; num=dm.num; dm.num=temp; cout<<"nValues after swapping are "<<num<<" and "<<dm.num; }};int main(){ Demo d1,d2; d1.get_data(); d2.get_data(); d1.sum(d2); return 0;
}#include<iostream>
using namespace std;
class Demo{ int num1,num2,num3; public: void max();};inline void Demo::max(){ cout<<"Enter first number "; cin>>num1; cout<<"Enter second number "; cin>>num2; cout<<"Enter third number "; cin>>num3; if(num1>num2 && num1>num3) { cout<<"num1 is greater "; } else if(num2>num1 && num2>num3) { cout<<"num2 is greater "; } else { cout<<"num3 is greater "; }}int main(){ Demo d1; d1.max(); return 0;}Assignments of Constructor And Destructor (5 programs)
#include<iostream>
using namespace std;
class Rectangle{ private: int length; int breadth; public: Rectangle() { length=10; breadth=20; cout<<"The length of the rectangle is: "<<length<<endl; cout<<"The breadth of the rectangle is: "<<breadth; } };//end of classint main(){Rectangle rect;return 0;
}#include<iostream>
using namespace std;
class Rectangle{ private: int length; int breadth; int rect; public: Rectangle(int l,int b) { length=l; breadth=b; } void area() { rect=length*breadth; cout<<"The area of the rectangle is:"<<rect; }};//end of classint main(){ Rectangle rect1(10,10);
Rectangle rect2(40,60);
rect1.area();
rect2.area();
return 0;
}#include<iostream>
using namespace std;
class Rectangle{ private: int length; int breadth; int rect; public: Rectangle() { length=100; breadth=200; cout<<"This is the length and breadth of the default constructor "<<endl; cout<<"The length of the rectangle is: "<<length<<endl; cout<<"The breadth of the rectangle is: "<<breadth<<endl; } Rectangle(int l,int b) { length=l; breadth=b; rect=length*breadth; cout<<"This is parameterized constructor"<<endl; cout<<"The area of the rectangle is: "<<rect; }};//end of classint main(){ Rectangle r; Rectangle r1(10,10);
return 0;
}#include<iostream>
using namespace std;
class Rectangle{ private: int length; int breadth; int area; public: void input() { cout<<"Enter the length of a rectangle "; cin>>length; cout<<"Enter the breadth of a rectangle "; cin>>breadth; } void copyValue(Rectangle r) { length=r.length; breadth=r.breadth; } void calcArea() { area=length*breadth; cout<<"n Area of rectangle is "<<area; }};//end of classint main(){ Rectangle r1,r2; r1.input(); r2.copyValue(r1); r1.calcArea(); r2.calcArea();
return 0;
}#include<iostream>
using namespace std;
class Demo{ int num; public: Demo() { num=100; cout<<"Default constructor "<<num; } ~Demo() { cout<<"nThis is Destructor "; }};int main(){ Demo dm;
return 0;
}Assignments of Function Overloading (2 programs)
#include<iostream>
using namespace std;
class Math{ private: int num1; int num2; int num3; float n1,n2,n3; public: /*void sum(int a1,int a2) { num1=a1; num2=a2; num3=num1+num2; cout<<"The sum of two number is:"<<num3; }*/ void sum(float f1,float f2) { n1=f1; n2=f2; n3=n1+n2; cout<<"nThe sum of two number is:"<<n3; }};int main(){ Math m1; m1.sum(10,20); m1.sum(10.1,20.1);
return 0;
}#include<iostream>
using namespace std;
class Area{private:int length;int breadth;int ar;int side;public:void area(int l,int b){length=l;breadth=b;ar=length*breadth;cout<<"The area of the rectangle is:";cout<<ar;cout<<"n";}void area(int s){side=s;ar=side*side;cout<<"The area of the square is:";cout<<ar;}};int main(){Area a1;a1.area(10,10);a1.area(12);
return 0;
}Assignments of Operator Overloading (3 programs)
#include<iostream>
using namespace std;
class Maths{ int num;public: friend Maths operator+(Maths,Maths); void input() { cout<<"Enter number "; cin>>num; } void display() { cout<<"Value after addition "<<num; }};Maths operator+(Maths m1,Maths m2){ Maths temp; temp.num=m1.num+m2.num; return temp;}int main(){ Maths m1,m2,m3; int ans; m1.input(); m2.input(); m3=m1+m2; m3.display();
return 0;
}#include<iostream>
using namespace std;
class Maths
{
int num;
public:
friend int operator>(Maths,Maths);
void input()
{
cout<<"Enter number ";
cin>>num;
}
};
int operator>(Maths m1,Maths m2)
{
if(m1.num>m2.num)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
Maths m1,m2;
m1.input();
m2.input();
if(m1>m2)
{
cout<<endl<<"m1 is greater ";
}
else
{
cout<<endl<<"m2 is greater ";
}
return 0;
}#include<iostream>
using namespace std;
class Maths
{
int num;
public:
friend int operator==(Maths,Maths);
void input()
{
cout<<"Enter number ";
cin>>num;
}
};
int operator==(Maths m1,Maths m2)
{
if(m1.num==m2.num)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
Maths m1,m2;
m1.input();
m2.input();
if(m1==m2)
{
cout<<endl<<"both objects are equal ";
}
else
{
cout<<endl<<"both objects are not equal ";
}
return 0;
}Assignments of Friend Function Friend Class (5 programs)
#include<iostream>
using namespace std;
class Demo
{
int num1;
int num2;
friend void usedata();
public:
void input()
{
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
}
void display()
{
cout<<"num1= "<<num1<<" num2= "<<num2;
}
};
void usedata()
{
Demo d1;
d1.num1=100;
d1.num2=200;
cout<<"n The values of num1 & num2 in usedata function are: ";
cout<<endl<<"num1= "<<d1.num1<<" num2= "<<d1.num2;
}
int main()
{
Demo d;
d.input();
d.display();
usedata();/* This function is only the friend function not
the member function of class Demo.So, it will
not be called with an object of a class Demo.*/
return 0;
}#include<iostream>
using namespace std;
class Demo
{
int num1;
int num2;
public:
friend void sum();
void input()
{
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
}
void output()
{
cout<<"num1= "<<num1<<" num2= "<<num2;
}
};
void sum()
{
Demo d1;
int res;
d1.input();
res=d1.num1+d1.num2;
cout<<"The sum of num1 & num2 in sum function is: "<<res;
}
int main()
{
Demo d;
d.input();
d.output();
cout<<endl<<endl;
sum();
return 0;
}#include<iostream>
using namespace std;
class Demo1
{
int num1;
public:
friend void sum();
void input1()
{
cout<<"Enter the value for num1 in Demo1 class ";
cin>>num1;
}
};//end of Demo1
class Demo2
{
int num2;
public:
friend void sum();
void input2()
{
cout<<"Enter the value for num2 in Demo2 class ";
cin>>num2;
}
};//end of Demo2
void sum()
{
Demo1 d1;
Demo2 d2;
int res;
d1.input1();
d2.input2();
res=d1.num1+d2.num2;
cout<<"n Sum of Demo1-:num1 and Demo2-:num2 is: "<<res;
}
int main()
{
sum();//calling of friend function sum()
return 0;
}#include<iostream>
using namespace std;
class Test1
{
int num1;
public:
friend void greater_fun();
void input1()
{
cout<<"Enter the value for num1 in Test1 class ";
cin>>num1;
}
};//end of Test1
class Test2
{
int num2;
public:
friend void greater_fun();
void input2()
{
cout<<"Enter the value for num2 in Test2 class ";
cin>>num2;
}
};//end of Test2
void greater_fun()
{
Test1 t1;
Test2 t2;
t1.input1();
t2.input2();
if(t1.num1>t2.num2)
{
cout<<"n Test1 class num1 is greater "<<t1.num1;
}
else
{
cout<<"n Test2 class num2 is greater "<<t2.num2;
}
}
int main()
{
greater_fun();
return 0;
}#include<iostream>
using namespace std;
class Maths1{ int num1; public: friend class Maths3; void input1() { cout<<"Enter the value for num1 in Maths1 class "; cin>>num1; cout<<endl; }};//end of Maths1class Maths2{ int num2; public: friend class Maths3; void input2() { cout<<"Enter the value for num2 in Maths2 class "; cin>>num2; cout<<endl; }};//end of Maths2class Maths3{ Maths1 m1; Maths2 m2; public: void swap1() { m1.input1(); m2.input2(); int temp; cout<<"n Values of Maths1 class num1 & Maths2 class num2 before swapping are : n"; cout<<"Maths1 num1= "<<m1.num1; cout<<"nMaths2 num2= "<<m2.num2; cout<<endl<<endl; temp=m1.num1; m1.num1=m2.num2; m2.num2=temp; cout<<"n Values of Maths1 class num1 & Maths2 class num2 after swapping are : n"; cout<<"Maths1 num1= "<<m1.num1; cout<<"nMaths2 num2= "<<m2.num2; }};//end of Maths3int main(){ Maths3 m3; m3.swap1(); return 0;
}Assignments of Static Data Member and Static Member Function (3 programs)
#include<iostream>
using namespace std;
class Sample
{
static int n;
public:
Sample()
{
n++;
cout<<"Object "<<n<<" created"<<endl;
}
};//end of class
int Sample::n=0;
int main()
{
Sample s1,s2,s3;
return 0;
}#include<iostream>
using namespace std;
class Rectangle
{
static int n;
int length;
int breadth;
int ar;
public:
Rectangle()
{
n++;
cout<<endl<<n<<" rectangle is created";
}
void input()
{
cout<<"nnEnter the length and breadth of Rectangle ";
cin>>length>>breadth;
}
void area()
{
ar=length*breadth;
cout<<"n Area of rectangle is "<<ar;
}
};int Rectangle::n;
int main()
{
Rectangle r1,r2,r3;
r1.input();
r1.area();
return 0;
}#include<iostream>
using namespace std;
class Rectangle
{
static int n;
int length;
int breadth;
int ar;
public:
void init()
{
// n=0;
n++;
cout<<"n"<<n<<" rectangle is created";
cout<<"nnEnter the length and breadth of Rectangle ";
cin>>length>>breadth;
}
void area()
{
ar=length*breadth;
cout<<"n Area of rectangle is "<<ar;
}
}; int Rectangle::n;
int main()
{
Rectangle r1,r2,r3;
r1.init();
r1.area();
r2.init();
r2.area();
r3.init();
r3.area();
return 0;
}Assignments of Inheritance (6 programs)
#include<iostream>
using namespace std;
class Student
{
private:
int rollno;
char name[20];
public:
void get_data();
void show_data();
};
void Student::get_data()
{
cout<<"Enter the rollno. of the student:";
cin>>rollno;
cout<<"Enter the name of the student:";
cin>>name;
}
void Student::show_data()
{
cout<<"nThe rollno of the student: "<<rollno;
cout<<"nThe name of the student: "<<name;
}
class Marks:public Student
{
private:
float marks[5];
public:
void get_marks();
void percentage();
};
void Marks::get_marks()
{
int i;
cout<<"nEnter marks : ";
for(i=0;i<5;i++)
{
cin>>marks[i];
}
}
void Marks::percentage()
{
int sum=0;
int i;
int pctage;
for(i=0;i<5;i++)
{
cout<<endl<<"Marks are: "<<marks[i];
sum=sum+marks[i];
}
pctage=sum/5;
cout<<endl<<"Percentage is "<<pctage<<"%";
}
int main()
{
Marks m1;
m1.get_data();
m1.show_data();
m1.get_marks();
m1.percentage();
return 0;
}#include<iostream>
using namespace std;
class Student{private:int rollno;char name[20];public:void get_data(){cout<<"Enter the roll no of the student";cin>>rollno;cout<<"Enter the name of the student";cin>>name;}void show_data(){cout<<"nThe roll no of the student is:";cout<<rollno;cout<<"nThe name of the student is";cout<<name;cout<<"nn";}};//end of class studentclass Teacher{private:char name[20];float salary;char sub[10];public:void get_data1(){cout<<"Enter the name of the teacher";cin>>name;cout<<"Enter the salary of the teacher";cin>>salary;cout<<"Enter the subject of the teacher";cin>>sub;}void show_data1(){cout<<"nThe name of the teacher is:";cout<<name;cout<<"nThe salary of the teacher is:";cout<<salary;cout<<"nThe subject of the teacher is:";cout<<sub;cout<<"n";}};class School:public Student,public Teacher{private:int no_of_student;int no_of_teacher;public:void get_data2(){cout<<"Enter the no. of student";cin>>no_of_student;cout<<"n";cout<<"Enter the no. of the teacher";cin>>no_of_teacher;}void show_data2(){cout<<"nThe no. of the students are:";cout<<no_of_student;cout<<"nThe no. of the teachers are:";cout<<no_of_teacher;}};int main(){School s1;s1.get_data();s1.show_data();s1.get_data1();s1.show_data1();s1.get_data2();s1.show_data2();
return 0;
}#include<iostream>
using namespace std;
class Student{private:int rollno;char name[20];public:void get_data(){cout<<"Enter the roll no of the student: ";cin>>rollno;cout<<"Enter the name of the student: ";cin>>name;}void show_data(){cout<<"The roll no of the student is: "<<rollno<<endl;cout<<"The name of the student is: "<<name<<endl<<endl;}};class Attendance:public Student{protected:int no_of_absentees;public:void get_data(){cout<<"Enter the no. of absentees: ";cin>>no_of_absentees;}void show_data(){cout<<"The no.of absentees are: ";cout<<no_of_absentees;}};class Accounts:public Attendance{private:float fees;float bal;public:void get_data(){cout<<"n";cout<<"Enter the fees of the student is: ";cin>>fees;cout<<"Enter the bal of the student is: ";cin>>bal;}void cal_total_amt(){int total_fees;total_fees=fees-bal;cout<<"The total fees of the student is: ";cout<<total_fees;}};int main(){Accounts a1;a1.Student::get_data();a1.Student::show_data();a1.Attendance::get_data();a1.Attendance::show_data();a1.Accounts::get_data();a1.cal_total_amt();
return 0;
}#include<iostream>
using namespace std;
class Student{protected:int rollno;char name[20];public:void get_data(){cout<<"Enter the roll no of the student ";cin>>rollno;cout<<"Enter the name of the student ";cin>>name;}void show_data(){cout<<"The roll no of the student is: "<<rollno<<endl;cout<<"The name of the student is "<<name<<endl<<endl;}};class Marks:public Student{private:float marks[5];public:void get_marks();void percentage();};void Marks::get_marks(){int i;cout<<"Enter the marks of the student: ";for(i=0;i<5;i++){cin>>marks[i];}}void Marks::percentage(){int sum=0;int i;int pctage;for(i=0;i<5;i++){cout<<marks[i]<<" ";sum=sum+marks[i];}pctage=sum/5;cout<<"n";cout<<"The percentage of the student is: "<<pctage<<endl<<endl;};class Sports:public Student{private:char game_name[10];float score;public:void get_score(){cout<<"Enter the name of the game: ";cin>>game_name;cout<<"n";cout<<"Enter the score of the game: ";cin>>score;cout<<"n";}void show_score(){cout<<"The name of the game is: "<<game_name<<endl;cout<<"The score of the game is: "<<score;}};int main(){Marks m1;Sports s1;m1.get_data();m1.show_data();m1.get_marks();m1.percentage();s1.get_data();s1.show_data();s1.get_score();s1.show_score();
return 0;
}#include<iostream>
using namespace std;
class A{public:void show(){cout<<"nHere is the base class";}};class B:virtual public A{ public:void show1(){ cout<<"nHere is the show1 function of B class";}};class C:virtual public A{public:void show2(){cout<<"nHere is the show2 function of c class";}};class D:public B,public C{public:void show3(){cout<<"nHere is the show3 fuction of d class";}};int main(){D d1;d1.show();d1.show1();d1.show2();d1.show3();
return 0;
}#include<iostream>
using namespace std;
class Student
{
protected:
int rollno;
char name[20];
public:
void get_data()
{
cout<<"Enter the roll no of the student ";
cin>>rollno;
cout<<"Enter the name of the student ";
cin>>name;
}
void show_data()
{
cout<<"The roll no of the student is: ";
cout<<rollno;
cout<<"n";
cout<<"The name of the student is ";
cout<<name;
cout<<"nn";
}
};
class Test:public Student
{
protected:
float marks[5];
public:
void gettest();
void showtest();
};
void Test::gettest()
{
int i;
for(i=0;i<5;i++)
{
cout<<"Enter the marks of the students: ";
cin>>marks[i];
//cout<<"n";
}}
void Test::showtest()
{
int i;
cout<<"The marks of the student: ";
for(i=0;i<5;i++)
{
cout<<endl<<marks[i];
}
}
class Address
{
private:
char city[10];
public:
void get_add()
{
cout<<"nnEnter the name of the city ";
cin>>city;
}
void show_add()
{
cout<<"nThe name of the city is: ";
cout<<city;
}
};
class Result:public Test,public Address
{
private:
float total;
float avg;
public:
Result()
{
total=0;
}
void get_result();
void show_result()
{
cout<<"nThe total marks of the student: ";
cout<<total;
cout<<"n";
cout<<"The avg of the student is: ";
cout<<avg;
}};
void Result::get_result()
{
int i;
for(i=0;i<5;i++)
{
total=total+marks[i];
}
avg= total/5;
}
int main()
{
Result r1;
r1.get_data();
r1.show_data();
r1.gettest();
r1.showtest();
r1.get_result();
r1.show_result();
r1.get_add();
r1.show_add();
return 0;
}Assignments of Virtual Function Pure Virtual Function (2 programs)
//program to accept a character from the user
#include<iostream>
using namespace std;
class base
{
public:
virtual void show()
{
cout<<" n show function of base class ";
}
};
class derived:public base
{
public:
void show()
{
cout<<" n show function of derived class ";
}
};
int main()
{
derived d;
base *bptr;
bptr=&d;
bptr->show();
return 0;
}//Program to accept a character in lower case and display it in uppercase.
#include<iostream>
using namespace std;
class Shape
{
virtual void area()=0;
virtual void perimeter()=0;
};
class Rectangle:public Shape
{
int length,breadth,ar,per;
public:
Rectangle()
{
cout<<"Enter length and breadth of rectangle: ";
cin>>length>>breadth;
}
void area()
{
ar=length*breadth;
cout<<"n Area of rectangle is "<<ar;
}
void perimeter()
{
per=2*(length+breadth);
cout<<"n Perimeter of rectangle is "<<per;
}
};
class Square:public Shape
{
int side,ar,per;
public:
Square()
{
cout<<"Enter side of square: ";
cin>>side;
}
void area()
{
ar=side*side;
cout<<"nn Area of square is "<<ar;
}
void perimeter()
{
per=4*side;
cout<<"n Perimeter of square is "<<per;
}
};
int main()
{
Rectangle rect;
Square sq;
rect.area();
rect.perimeter();
sq.area();
sq.perimeter();
return 0;
}Assignments of File Handling (11 programs)
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int num; ofstream fout; fout.open("c:/website/cplusassignments/abc.txt"); cout<<"Enter any number "; cin>>num; fout<<num; cout<<"Data saved "; fout.close();
return 0;
}//end of main#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int num1,num2; ofstream fout; fout.open("e:/website/cplusassignments/abc.txt"); cout<<"Enter first number "; cin>>num1; cout<<"Enter second number "; cin>>num2; fout<<num1<<"t"<<num2; cout<<"Data saved "; fout.close();
return 0;
}//end of main#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int rollno; char name[20]; ofstream fout("c:/website/cplusassignments/first.txt"); //calling of parameterized constructor //fout.open("c:/website/cplusassignments/first.txt"); cout<<"Enter name "; cin.getline(name,20); cout<<"Enter rollno "; cin>>rollno; fout<<rollno<<"t"<<name; cout<<"Data saved "; fout.close();
return 0;
}//end of main#include<iostream>
#include<fstream>
using namespace std;
#include<stdlib
int main()
{
int num; ifstream fin("c:/website/cplusassignments/abc.txt"); fin>>num; cout<<"Number is "<<num; fin.close();
return 0;
}//end of main#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int rollno;
char name[30];
fstream finout("c:/website/cplusassignments/first.txt",ios::in);
finout>>rollno;
finout.getline(name,20);
cout<<"n Rollno is "<<rollno;
cout<<"n Name is "<<name;
finout.close();
return 0;
}//end of main#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char str[100];
int i=1,n=3;
fstream finout;
while(i<=n)
{
finout.open("c:/website/cplusassignments/second.txt",ios::app);
cout<<"Enter a string ";
cin.getline(str,100);
finout<<str<<endl;
i++;
}
finout.close();
return 0;
}//end of main#include<iostream>
#include<fstream>using namespace std;int main(){ int num; char str[20]; fstream finout("c:/website/cplusassignments/first.txt",ios::out); cout<<"Enter number and a string "; cin>>num>>str; finout<<num<<" "<<str; cout<<"n Data saved "; finout.close(); finout.open("c:/website/cplusassignments/first.txt",ios::in); finout>>num>>str; finout.close(); finout.open("c:/website/cplusassignments/second.txt",ios::out); finout<<num<<" "<<str; cout<<"Number is "<<num; cout<<"n string is "<<str; finout.close(); return 0;
}//end of main#include<iostream>
#include<fstream>using namespace std;int main(){ int num,n; fstream finout("c:/website/cplusassignments/forth.txt",ios::out); cout<<"Enter a number "; cin>>num; finout<<num; cout<<"Data saved "; finout.close(); finout.open("c:/website/cplusassignments/forth.txt",ios::in); finout>>n; cout<<"n Number is "<<n; finout.close(); return 0;
}//end of main#include<iostream>
#include<fstream>using namespace std;class File{ int rollno; char name[10]; public: void input() { cout<<"Enter name "; cin.getline(name,20); cout<<"Enter rollno "; cin>>rollno; } void write() { ofstream fout("c:/website/cplusassignments/third.txt"); fout<<rollno<<"t"<<name; cout<<"Data saved "; fout.close(); }};int main(){ File f; f.input(); f.write();
return 0;
}//end of main#include<iostream>
#include<fstream>using namespace std;class File{ int rollno; char name[10]; public: void read() { ifstream fin("c:/website/cplusassignments/third.txt"); fin>>rollno>>name; cout<<"Rollno is : "<<rollno; cout<<"nName is : "<<name; fin.close(); }};int main(){ File f; f.read();
return 0;
}//end of main#include<iostream>
#include<fstream>using namespace std;int main(){ int num; fstream finout("c:/website/cplusassignments/abc.txt",ios::app); cout<<"Enter any number "; cin>>num; finout<<"t"<<num; cout<<"n Data saved "; finout.close();
return 0;
}//end of mainAssignments of Dynamic Memory Allocation (8 programs)
#include<iostream>
using namespace std;
class Test
{
public:
void show()
{
cout<<"Show function of Test class";
}
};//end of class
int main()
{
Test *ptr;
ptr=new Test();
//dynamic memory allocation using new operator
ptr->show();
return 0;
}#include<iostream>
using namespace std;
class Test
{
public:
void show()
{
cout<<"Show function of Test class";
}
};//end of class
int main()
{
Test *ptr;
ptr=new Test();
//dynamic memory allocation using new operator
ptr->show();
delete ptr;
//delete operator will delete the memory
return 0;
}#include<iostream>
using namespace std;
int main(){ int *ptr1,*ptr2; int num1,num2,res; cout<<"Enter first number "; cin>>num1; cout<<"Enter second number "; cin>>num2; ptr1=new int(num1); ptr2=new int(num2); res=*ptr1+*ptr2; cout<<"Result is "<<res;
return 0;
}#include<iostream>
using namespace std;
int main(){ int i,n; cout<<"How many numbers you want to enter ?"<<endl; cin>>n; int *arr=new int(n); cout<<"Enter "<<n<<" numbers"<<endl; for(i=0;i<n;i++) { cin>>arr[i]; } cout<<"Reverse of array elements are "<<endl; for(i=n-1;i>=0;i--) { cout<<arr[i]<<" "; } cout<<endl; delete[] arr;
return 0;
}#include<iostream>
using namespace std;
class Demo{ private: int num; public: void input() { cout<<"Enter the value for num "; cin>>num; } void display() { cout<<"nValue of num = "<<num; }};int main(){ Demo *ptr=new Demo; ptr->input(); ptr->display();
delete ptr;
return 0;
}#include<iostream>
using namespace std;
int main(){ int *ptr1=new int(10); int *ptr2=new int(20); int temp; cout<<"Values before swapping are: "<<*ptr1<<" and "<<*ptr2; temp=*ptr1; *ptr1=*ptr2; *ptr2=temp; cout<<"nValues after swapping are: "<<*ptr1<<" and "<<*ptr2; delete ptr1,ptr2;
return 0;
}#include<iostream>
using namespace std;
int main(){ int *m=new int(2); int *n=new int(3); int *res=new int(1); int i; for(i=1;i<=*n;i++) { *res=(*m) * (*res); } cout<<*res; delete m,n,res;
return 0;
}#include<iostream>
using namespace std;
int main(){
int i,n,large=0;
cout<<"How many numbers you want to enter ?"<<endl;
cin>>n;
int *arr=new int(n);
cout<<"Enter "<<n<<" numbers"<<endl;
for(i=0;i<n;i++)
{
cin>>arr[i];
}
for(i=0;i<n;i++)
{
if(arr[i]>large)
{
large=arr[i];
}
}
cout<<"nLargest element of an array is "<<large;
return 0;
}Unsolved C++ Assignments (PDF)
Download and solve C++ assignment problems. Contact COMPUHELP faculty for the latest PDF.
Online/offline training for C, C++, Java, Python, web technologies & more at Sector 46-C & Sector 22-C, Chandigarh.