0% found this document useful (0 votes)
86 views

Using Namespace Main: #Include

The program takes a character as input from the user and checks its ASCII value to determine if it is a capital letter, small letter, digit or special symbol. It prints the corresponding output after the checks.

Uploaded by

yohans shegaw
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views

Using Namespace Main: #Include

The program takes a character as input from the user and checks its ASCII value to determine if it is a capital letter, small letter, digit or special symbol. It prints the corresponding output after the checks.

Uploaded by

yohans shegaw
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 279

#include<iostream>

using namespace std;

int main()
{
cout<<"subject " <<"\tmarks"<<"\nmathematic\t"
<<90<<"\ncomputer\t"<<77<<"\nchemistry\t"<<69;

return 0;
}

#include<iostream>
using namespace std;
intmain()
{ int a,b,c;
cout<< "\nEnter first number : ";
cin>>a;
cout<<"\nEnter second number : ";
cin>>b;
c=a+b;
cout<<"\nThe Sum is : "<<c;

return 0;
#include<iostream>
using namespace std;
int main()
{ float F,C;
cout<< "\nEnter temperature in Farenheit : ";
cin>>F;
C=5*(F-32)/9;
cout<<"Temperature in celcius is : "<<C;

return 0;
}

#include<iostream>
using namespace std;
int main()
{ int p,r,t,i;
cout<<"Enter Principle : ";
cin>>p;
cout<<”Enter raduise”
cin>>r;
cout<<"Enter Time : ";
cin>>t;
i=(p*r*t)/100;
cout<<"Simple interest is : "<<i;

return 0;
}
#include<iostream>
using namespace std;
int main()
{ char ch;
cout<<"\nEnter any character : ";
cin>>ch;
cout<<"ASCII equivalent is : "<<static_cast<int>(ch);

return 0;
}
#include<iostream>
using namespace std;
int main()
{ int a,b,temp;
cout<<"\nEnter two numbers : ";
cin>>a>>b;
temp=a;
a=b;
b=temp;
cout<<"\nAfter swapping numbers are : ";
cout<<a<<" "<<b;

return 0;
}
Enter two numbers : 78 6
After swapping numbers are : 6 78

#include<iostream>
using namespace std;
int main()
{ float r,area;
cout<< "\nEnter radius of circle : ";
cin>>r;
area = 3.14*r*r;

cout<<"Area of circle : "<<area;

return 0;
}
#include<iostream>
using namespace std;
int main()
{ int a;
cout<<"Enter any non-zero Number : ";
cin>>a;
(a>0)?cout<<"Number is positive":cout<<"Number is
negative";

return 0;
}
#include<iostream>
using namespace std;
intmain()
{ int a;
cout<<"Enter the Number : ";
cin>>a;
(a%2==0)?cout<<"Number is even":cout<<"Number is odd";

return 0;
}
#include<iostream>
using namespace std;
int main()
{ int a,b;
cout<<"\nEnter two numbers : ";
cin>>a>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"\nAfter swapping numbers are : ";
cout<<a<<" "<<b;

return 0;
}
#include <iostream>
using namespace std;
intmain()
{ int a,b,c,greatest;
cout<<"Enter three numbers : ";
cin>>a>>b>>c;
greatest=(a>b&&a>c)?a:(b>c)?b : c;
cout<<"Greatest number is "<<greatest;

return 0;
}

#include<iostream>
using namespace std;
int main()
{ int amt,R500,R100,R50,R20,R10,R5,R1;
cout<<"Enter amount : ";
cin>>amt;
R500=amt/500;
amt=amt%500;
R100=amt/100;
amt=amt%100;
R50=amt/50;
amt=amt%50;
R20=amt/20;
amt=amt%20;
R10=amt/10;
amt=amt%10;
R5=amt/5;
amt=amt%5;
R1=amt;
cout<<"Rs.500 : "<<R500<<"\nRs.100 : "<<R100<<"\nRs. 50 :
"<<R50<< "\nRs. 20 : "<<R20<<"\nRs. 10 : "<<R10<<"\nRs.
5 : "<<R5<<"\nRe. 1 : "<<R1;

return 0;
}

#include<iostream>
using namespace std;
int main()
{ char ch;
cout<< "\nEnter any character : ";
cin>>ch;
ch++;
cout<<"Next character is : "<<ch;

return 0;
}

#include<iostream>
using namespace std;
int main()
{
int days,y,m,d;
cout<<"Enter no. of days : ";
cin>>days;
y=days/365;
days=days%365;
m=days/30;
d=days%30;
cout<<"Years : "<<y<<"\nMonths : "<<m<<"\nDays : "<<d;

return 0;
}

#include<iostream>
using namespace std;

int main()
{
int a;
cout<<"Enter any number : ";
cin>>a;
if(a%2==0)
cout<<"The number is even"; else
cout<<"The number is odd";

return 0;
}
7 What is the output of following program?

int x = 10, y; int x = 10, y;


y = x++; y = x++;
cout << y; cout << x;

Show the answer. Show the answer.


10 11

int x = 10; int x = 10, y;


x++; y = ++x;
cout << x; cout << y;

Show the answer. Show the answer.


11 11

int x = 10; int x = 10;


cout << ++x; cout << x++;

Show the answer. Show the answer.


11 10

What is the output of following program?

int x = 10,y; int x = 10,y;


y = x + x++; y = ++x + x++ + x;
cout << y; cout << y;

Show the answer. Show the answer.


21 35

9 What is the output of following program?


cout << setw(5) << 77 << endl;
cout << setw(5) << 100 << endl;
cout << setw(5) << 12312 << endl;

Show the answer.


77
100
12312

10 What is the output of following program?


float net = 5689.2356;
cout.precision(2);
cout.setf(ios::fixed | ios::showpoint);
cout << net << endl;

Show the answer.


5689.24

« back
#include<iostream>
using namespace std;

int main()
{
int a;
cout<<"Enter any number:";
cin>>a;
if(a>0)
cout<<"The absolute value of number is:"<<a; else
cout<<"The absolute value of number is:"<<-(a);

return 0;
}
#include<iostream>
using namespace std;

int main()
{
int totalexp, qty, price, discount;

cout<<"Enter quantity:";
cin>>qty;
cout<<"Enter price:";
cin>>price;

totalexp=qty*price;
if(totalexp>5000)
{
discount=(totalexp*0.1);
totalexp=totalexp-discount;
}

cout<<"Total Expense is Rs. "<<totalexp;

return 0;
}

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

int main()
{
int cp,sp,result;

cout<<"Enter cost price of item : ";


cin>>cp;
cout<<"Enter selling price of item : ";
cin>>sp;

result=sp-cp;
if(result>0)
cout<<"Profit : "<<result; else
if(result<0)
cout<<"Loss : "<<-(result); else
cout<<"No profit no loss";

getch(); return 0;
}

#include<iostream>
using namespace std;
int main()
{
int ram_age,sulabh_age,ajay_age;
cout<<"Enter Ram age:";
cin>>ram_age;
cout<<"Enter Sulabh age:";
cin>>sulabh_age;
cout<<"Enter Ajay age:";
cin>>ajay_age;
if (ram_age<sulabh_age && ram_age<ajay_age)
cout<<"Ram is youngest"; else
if(sulabh_age<ram_age && sulabh_age<ajay_age)
cout<<"Sulabh is youngest"; else
cout<<"Ajay is youngest";

return 0;
}
include<iostream>
using namespace std;
int main()
{
int angle1,angle2,angle3;
cout<<"Enter the three angles of triangle:";
cin>>angle1>>angle2>>angle3;
if (angle1+angle2+angle3==180)
cout<<"Triangle is valid"; else
cout<<"Triangle is not valid";

return 0;
}

#include<iostream>
using namespace std;
int main()
{
int year;

cout<<"Enter the year : ";


cin>>year;
if( (year%400==0 || year%100!=0) &&(year%4==0))
cout<<"It is a leap year"; else
cout<<"It is not a leap year";

return 0;
}
#include<iostream>
using namespace std;
int main()
{ float basic_salary, gross_salary, HRA, DA;
cout<<"Enter basic salary of Employee : ";
cin>>basic_salary;
if (basic_salary<1500)
{
HRA=0.1*basic_salary;
DA=0.9*basic_salary;
} else {
HRA=500;
DA=0.98*basic_salary;
}

gross_salary=basic_salary+HRA+DA;
cout<<"Gross salary is : "<<gross_salary;

return 0;
}
include<iostream>
using namespace std;
intmain()
{
int calls; float bill;
cout<<"Enter number of calls : ";
cin>>calls;
if(calls<=100)
bill=200; else if (calls>100 && calls<=150)
{
calls=calls-100;
bill=200+(0.60*calls);
} else if (calls>150 && calls<=200)
{
calls=calls-150;
bill=200+(0.60*50)+(0.50*calls);
} else {
calls=calls-200;
bill=200+(0.60*50)+(0.50*50)+(0.40*calls);
}

cout<<" Your bill is Rs."<<bill;

return 0;
}
#include<iostream>
#include<math.h>
using namespace std;

intmain()
{
float a,b,c,d,root1,root2;
cout<<"Enter value of a, b and c : ";
cin>>a>>b>>c;
d=b*b-4*a*c;
if(d==0)
{
root1=(-b)/(2*a);
root2=root1;
cout<<"Roots are real & equal";
} else if(d>0)
{
root1=-(b+sqrt(d))/(2*a);
root2=-(b-sqrt(d))/(2*a);
cout<<"Roots are real & distinct";
} else {
root1=(-b)/(2*a);
root2=sqrt(-d)/(2*a);
cout<<"Roots are imaginary";
}

cout<<"\nRoot 1= "<<root1<<"\nRoot 2= "<<root2;

return 0;
}
#include<iostream>
using namespace std;
int main()
{
int sub1,sub2,sub3,sub4,sub5,percentage;
cout<<"Enter marks of five subjects : ";
cin>>sub1>>sub2>>sub3>>sub4>>sub5;
percentage=(sub1+sub2+sub3+sub4+sub5)/5;
if(percentage>=60)
cout<<"Ist division"; else if(percentage>=50)
cout<<"IInd division"; else if(percentage>=40)
cout<<"IIIrd division"; else
cout<<"Fail" ;

return 0;
}

Any character is entered by the user; write a program to determine whether


the character entered is a capital letter, a small case letter, a digit or a special
symbol.The following table shows the range of ASCII values for various
characters.

Characters ASCII Values


A–Z 65 – 90
a–z 97 – 122
0–9 48 – 57
special symbols 0 - 47, 58 - 64, 91 - 96, 123 – 127
Source Code
Output

#include<iostream>
using namespace std;
intmain()
{
char ch;
cout<<"Enter any character:";
cin>>ch;
if (ch>=65 && ch<=90)
cout<<"Character is a capital letter"; else if
(ch>=97 && ch<=122)
cout<<"Character is a small letter"; else if
(ch>=48 && ch<=57)
cout<<"Character is a digit"; else if ((ch>0 &&
ch<=47)||(ch>=58 && ch<=64)||
(ch>=91 && ch<=96)||(ch>=123 && ch<=127))
cout<<"Character is a special symbol";

return 0;
}include<iostream>
using namespace std;
int main()
{
int i=1;
while(i<=10)
{
cout<<i<<"\n";
i++;
}

return 0;
}

#include<iostream>
using namespace std;
int main()
{
int i=1,sum=0;
while(i<=10)
{
sum+=i;
i++;
}

cout<<"Sum :"<<sum;

return 0;
}
#include<iostream>
using namespace std;
int main()
{
int n,fact=1;
cout<<"Enter any number : ";
cin>>n;
while(n>=1)
{
fact*=n;
n--;
}

cout<<"Factorial :"<<fact;

return 0;
}
Two numbers are entered through the keyboard. Write a program to find the
value of one number raised to the power of another.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,p,r=1;
cout<<"Enter the base number and exponent ";
cin>>n>>p;
for(int i=1;i<=p;i++)
r=r*n;

cout<<"Result :"<<r;

return 0;
}

Write a program to reverse any given integer number.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,t,r,rev=0;
cout<<"Enter any number : ";
cin>>n;
t=n;
while(t>0)
{
r=t%10;
t=t/10;
rev=rev*10+r;
}

cout<<"Reverse of number "<<n<<" is "<<rev;

return 0;
}
Write a program to sum of digits of given integer number.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,t,r,sum=0;
cout<<"Enter any number : ";
cin>>n;
t=n;
while(t>0)
{
r=t%10;
sum+=r;
t=t/10;
}

cout<<"Sum of digits of number "<<n<<" is "<<sum;


return 0;
rite a program to check given number is prime or not.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n;
bool flag=false;
cout<<"Enter any number : ";
cin>>n;
for(int i=2;i<n;i++)
{ if(n%i==0)
{
flag=true; break;
}
}
if(flag==false && n>1)
cout<<"Number is prime"; else
cout<<"Number is not prime";

return 0;
}
Write a program to calculate HCF of Two given number.

Source Code
Output

#include<iostream>
using namespace std;
intmain()
{
int dividend, divisor, rem, hcf;
cout<<"Enter two numbers : ";
cin>>dividend>>divisor;
while(rem!=0)
{
rem=dividend%divisor; if(rem==0)
hcf=divisor; else {
dividend=divisor;
divisor=rem;
}
}

cout<<"HCF is : "<<hcf;

return 0;
}

Write a program to enter the numbers till the user wants and at the end it
should display the count of positive, negative and zeros entered.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n, sum_p=0, sum_n=0, sum_z=0; char choice;
do {
cout<<"Enter number ";
cin>>n;
if(n>0)
sum_p++; else if(n<0)
sum_n++; else
sum_z++;

cout<<"Do you want to Continue(y/n)? ";


cin>>choice;

}while(choice=='y'|| choice=='Y');

cout<<"Positive Number :"<<sum_p<<"\nNegative


Number :"<<sum_n<<"\nZero Number :"<<sum_z;

return 0;
}

Write a program to enter the numbers till the user wants and at the end it
should display the maximum and minimum number entered.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n, max=0, min=32767; char choice;
do {
cout<<"Enter number : ";
cin>>n;
if(n>max)
max=n; if(n<min)
min=n;

cout<<"Do you want to Continue(y/n)? ";


cin>>choice;

}while(choice=='y' || choice=='Y');

cout<<"Maximum Number :"<<max<<"\nMinimum


Number :"<<min;

return 0;
}
Write a program to print all Armstrong numbers between 1 and 500. If sum
of cubes of each digit of the number is equal to the number itself, then the
number is called an Armstrong number.
For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,digit1,digit2,digit3;
for(int i=1;i<=500;i++)
{
digit1=i/100;
digit2=i/10 - digit1*10;
digit3=i%10;
if(digit1*digit1*digit1 + digit2*digit2*digit2 +
digit3*digit3*digit3 == i)
cout<<i<<endl;
}

return 0;
}

Write a program to print Fibonacci series of n terms where n is input by user


0 1 1 2 3 5 8 13 24 .....

Source Code
Output

#include<iostream>
using namespace std;

int main()
{
int f=0,s=1,t,n;

cout<<"Enter Number of terms of Series : ";


cin>>n;

cout<<f<<" "<<s<<" ";


for(int i=3;i<=n;i++)
{
t=f+s;
cout<<t<<" ";
f=s;
s=t;
}

return 0;
}
Write a program to calculate the sum of following series where n is input by
user.
1 + 1/2 + 1/3 + 1/4 + 1/5 +…………1/n

Source Code
Output

#include<iostream>
using namespace std;
int main()
{ int i,n; float sum=0;

cout<<"Enter the value of n ";


cin>>n;
for(i=1;i<=n;i++)
sum += 1.0/i;

cout<<"Sum : "<<sum;

return 0;
}
Compute the natural logarithm of 2, by adding up to n terms in the series
1 - 1/2 + 1/3 - 1/4 + 1/5 -... 1/n
where n is a positive integer and input by user.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{ int i,n,sign=-1; float sum=0;
cout<<"Enter the value of n ";
cin>>n;
for(i=1;i<=n;i++)
{
sign *= -1;
sum += sign*1.0/i;
}
cout<<"log 2 : "<<sum;

return 0;
}
Write C++ program to print following pattern:
i) ii) iii)
********** * *
********** ** **
********** *** ***
********** **** ****
***** *****
iv) v) 1 vi) 1
* 222 212
*** 33333 32123
4444444 4321234
***** 555555555 543212345
*******
*********
Source Code

//Solution of (i)
#include<iostream>
using namespace std;
int main()
{ int i,j; for(i=1;i<=4;i++)
{ for(j=1;j<=10;j++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (ii)
#include<iostream>
using namespace std;
int main()
{ int i,j; for(i=1;i<=5;i++)
{ for(j=1;j<=i;j++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (iii)
#include<iostream>
using namespace std;
int main()
{ int i,j,k; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=1;k<=i;k++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (iv)
#include<iostream>
using namespace std;
int main()
{ int i,j,k; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=1;k<2*i;k++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (v)
#include<iostream>
using namespace std;
intmain()
{ int i,j,k; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=1;k<2*i;k++)
cout<<i;
cout<<endl;
}

return0;
}

//Solution of (vi)
#include<iostream>
using namespace std;
intmain()
{ int i,j,k,l; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=i;k>=1;k--)
cout<<k; for(l=2;l<=i;l++)
cout<<l;
cout<<endl;
}

return0;
}
Write a program to compute sinx for given x. The user should supply x and a
positive integer n. We compute the sine of x using the series and the
computation should use all terms in the series up through the term involving
x
sin x = x - x3/3! + x5/5! - x7/7!+x9/9! .........

Source Code
Output

#include<iostream>
using namespace std;
int main()
{ int i,j,n,fact,sign=-1; float x, p=1,sum=0;
cout<<"Enter the value of x : ";
cin>>x;
cout<<"Enter the value of n : ";
cin>>n;
for(i=1;i<=n;i+=2)
{
fact=1; for(j=1;j<=i;j++)
{
p=p*x;
fact=fact*j;
}
sign=-1*sign;
sum+=sign*p/fact;
}
cout<<"sin "<<x<<"="<<sum;
return 0;
}
Write a program to compute the cosine of x. The user should supply x and a
positive integer n. We compute the cosine of x using the series and the
computation should use all terms in the series up through the term involving
xn
cos x = 1 - x2/2! + x4/4! - x6/6! -.....

Source Code
Output

#include<iostream>

using namespace std;

int main()

{
int i,j,n,fact,sign=-1;
float x, p=1,sum=0;

cout<<"Enter the value of x : ";

cin>>x;

cout<<"Enter the value of n : ";

cin>>n;

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

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

p=p*x;

fact=fact*j;

sum+=sign*p/fact;

sign=-1*sign;

cout<<"cos "<<x<<"="<<1+sum;

return 0;

}
Write a program which input principal, rate and time from user and calculate
compound interest.
CI = P(1+R/100)T - P

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cmath>
using namespace std;
int main()
{ float P,R,T,CI;
cout<<"Enter Principal, Rate and Time : ";
cin>>P>>R>>T;
CI=P*pow((1+R/100),T) - P;
cout<<"Compound Interest is : "<<CI;
return 0;
}
Write a program to compute area of triangle. Sides are input by user.
Area = sqrt(s*(s-a)*(s-b)*(s-c))
where s=(a+b+c)/2

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cmath>
using namespace std;
int main()
{ float a,b,c,s,Area;
cout<<"Enter three sides of triangle : ";
cin>>a>>b>>c;
s=(a+b+c)/2;
Area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"Area of triangle is : "<<Area;

return 0;
}
Write a program to check character entered is alphabet, digit or special
character.

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cctype>
using namespace std;
int main()
{ char ch;
cout<<"Enter any character :";
ch=getchar(); if(isalpha(ch))
cout<<"Alphabet"; else if(isdigit(ch))
cout<<"Number"; else
cout<<"Special Character";
return 0;
}
Write a program which display a number between 10 to 100 randomly.

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cstdlib>
#include <ctime>
using namespace std;
int main()
{ int n;
srand(time(0));
n = rand() % 91 + 10;
cout<<"The randomly selected number is :"<<n;
return 0;
}
Write a program which accept a letter and display it in uppercase letter.

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cctype>
using namespace std;
int main()
{ char ch;
cout<<"Enter any character :";
ch=getchar();
ch=toupper(ch);
cout<<ch;

return 0;
}
Write a C++ program to implement the Number Guessing Game. In this
game the computer chooses a random number between 1 and 100, and the
player tries to guess the number in as few attempts as possible. Each time
the player enters a guess, the computer tells him whether the guess is too
high, too low, or right. Once the player guesses the number, the game is
over.

ANSI C++
Pre-Standard C++
Output
#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main()

{
int num, guess, tries = 0;

srand(time(0)); //seed random number generator

num = rand() % 100 + 1; // random number between 1 and 100

cout << "Guess My Number Game\n\n";


do
{
cout << "Enter a guess between 1 and 100 : ";

cin >> guess;

tries++;
if (guess > num)

cout << "Too high!\n\n";


else if (guess < num)

cout << "Too low!\n\n";


else

cout << "\nCorrect! You got it in " << tries << "
guesses!\n";

} while (guess != num);

cin.ignore();

cin.get();
return 0;

}
Write a C++ program using function which accept two integers as an
argument and return its sum. Call this function from main( ) and print the
results in main( ).

ANSI C++
Output
#include<iostream>
using namespace std;
int sum(int, int);
int main()
{ int x,y,s;
cout<<"Enter first number : ";
cin>>x;
cout<<"Enter second number : ";
cin>>y;
s=sum(x,y);
cout<<"The sum is : "<<s;

return 0;
}
int sum(int a, int b)
{
int total;
total=a+b; return total;
}
Write a function to calculate the factorial value of any integer as an argument.
Call this function from main( ) and print the results in main( ).

ANSI C++
Output

#include<iostream>
using namespace std;
int factorial(int);
int main()
{ int x,f;
cout<<"Enter number : ";
cin>>x;
f=factorial(x);
cout<<"The factorial is :"<<f;
return 0;
}
int factorial(int a)
{ int fact=1;
while(a>=1)
{
fact=fact*a;
a--;
}
return fact;
}
Write a function that receives two numbers as an argument and display all
prime numbers between these two numbers. Call this function from main( ).

ANSI C++
Output
#include<iostream>
using namespace std;
void showprime(int, int);
int main()
{
int x,y;
cout<<"Enter first number : ";
cin>>x;

cout<<"Enter second number : ";


cin>>y;
showprime(x,y);

return 0;
}
void showprime(int a, int b)
{ bool flag;
for(int i=a+1;i<=b;i++)
{

flag=false; for(int j=2;j<i;j++)


{
if(i%j==0)
{
flag=true; break;
}
}
if(flag==false && i>1)
cout<<i<<endl;
}
}
Raising a number to a power p is the same as multiplying n by itself p times.
Write a function called power that takes two arguments, a double value for n
and an int value for p, and return the result as double value. Use default
argument of 2 for p, so that if this argument is omitted the number will be
squared. Write the main function that gets value from the user to test power
function.

ANSI C++
Output

#include<iostream>
using namespace std;
double power(double,int=2);
int main()
{ int p; double n,r;
cout << "Enter number : ";
cin >> n;
cout << "Enter exponent : ";
cin >> p;
r = power(n,p);
cout << "Result is " << r;
r = power(n);
cout << "\nResult without passing exponent is " << r;
return 0;
}
double power(double a, int b)
{ double x = 1; for(int i = 1; i <= b; i++)
x = x * a; return x;
}
Write the output of the following program :
#include <iostream>
using namespace std;

void X(int &A, int &B)


{
A = A + B;
B = A - B;
A = A - B;
}

int main()
{
int a = 4, b = 18;
X(a,b);
cout << a << ", " << b;

return 0;
}

Show the answer.


18, 4

7 Write the output of the following program:

#include <iostream>
using namespace std;

void X(int A, int &B)


{
A = A + B;
B = A - B;
A = A - B;
}

int main()
{
int a = 4, b = 18;
X(a,b);
cout << a << ", " << b;

return 0;
}
Show the answer.
4, 4

8 Write the output of the following program:


#include <iostream>
using namespace std;

void Execute(int &B, int C = 100)


{
int temp = B + C;
B += temp;
if (C == 100)
cout << temp << " " << B << " " << C << endl;
}

int main()
{
int M = 90, N = 10;
Execute(M);
cout << M << " " << N << endl;
Execute(M, N);
cout << M << " " << N << endl;

return 0;
}

Show the answer.


190 280 100
280 10
570 10

9 Give the output of the following program


#include <iostream>
using namespace std;

int global = 10;

void func(int &x, int y)


{
x = x - y;
y = x * 10;
cout << x << ", " << y << '\n';
}
int main()
{
int global = 7;
func (::global, global);
cout << global << ", " << ::global << '\n';

func(global, ::global);
cout << global << ", " << ::global << '\n';

return 0;
}

Show the answer.


3, 30
7, 3
4, 40
4, 3

10 Write the output of the following program :


#include <iostream>
using namespace std;

static int i = 100;


void abc()
{
static int i = 8;
cout << "first = " << i++ << endl;
}

int main()
{
static int i = 2;
abc();
cout << "second = " << i << endl;
abc();

return 0;
}

Show the answer.


first = 8
second = 2
first = 9

11 Write the output of the following program:


#include <iostream>
using namespace std;

int func(int &x, int y = 10)


{
if (x % y == 0)
return ++x;
else
return y--;
}

int main()
{
int p = 20, q = 23;

q = func(p, q);
cout << p << " " << " " << q << endl;

p = func (q);
cout << p << " " << " " << q << endl;

q = func (p);
cout << p << " " << " " << q << endl;

return 0;
}

Show the answer.


20 23
10 23
11 11

Write a program that lets the user perform arithmetic operations on two
numbers. Your program must be menu driven, allowing the user to select the
operation (+, -, *, or /) and input the numbers. Furthermore, your program
must consist of following functions:
1. Function showChoice: This function shows the options to the user and
explains how to enter data.
2. Function add: This function accepts two number as arguments and returns
sum.
3. Function subtract: This function accepts two number as arguments and
returns their difference.
4. Function mulitiply: This function accepts two number as arguments and
returns product.
5. Function divide: This function accepts two number as arguments and
returns quotient.

ANSI C++
Output
#include <iostream>
using namespace std;
void showChoices();float add(float, float);float
subtract(float, float);float multiply(float, float);float
divide(float, float);
int main()
{ float x, y; int choice; do {
showChoices();
cin >> choice; switch (choice)
{ case 1:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Sum " << add(x,y) <<endl; break;
case 2:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Difference " << subtract(x,y) <<endl;
break; case 3:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Product " << multiply(x,y) <<endl;
break; case 4:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Quotient " << divide(x,y) <<endl;
break; case 5: break; default:
cout << "Invalid input" << endl;
}
}while (choice != 5);
return 0;
}
void showChoices()
{
cout << "MENU" << endl;
cout << "1: Add " << endl;
cout << "2: Subtract" << endl;
cout << "3: Multiply " << endl;
cout << "4: Divide " << endl;
cout << "5: Exit " << endl;
cout << "Enter your choice :";
}
float add(float a, float b)
{ return a + b;
}
float subtract(float a, float b)
{ return a - b;
}
float multiply(float a, float b)
{ return a * b;
}
float divide(float a, float b)
{ return a / b;
}
Write a C++ program to find the sum and average of one dimensional integer
array.

ANSI C++
Output
#include<iostream>
using namespace std;
intmain()
{ int Arr[100],n,i,sum=0;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{

cout<<"Enter element "<<i+1<<":";


cin>>Arr[i];
}
for(i=0;i<n;i++)
sum+=Arr[i];

cout<<"\nThe sum of Array is :"<<sum;


cout<<"\nThe average of Array is :"<<sum/i;
return 0;
}
Write a C++ program to swap first and last element of an integer 1-d array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,i,temp;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{

cout<<"Enter element "<<i+1<<":";


cin>>Arr[i];
}

temp=Arr[0];
Arr[0]=Arr[n-1];

Arr[n-1]=temp;

cout<<"\nArray after swapping"<<endl;


for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}
Write a C++ program to swap first and last element of an integer 1-d array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,i,temp;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{

cout<<"Enter element "<<i+1<<":";


cin>>Arr[i];
}

temp=Arr[0];
Arr[0]=Arr[n-1];

Arr[n-1]=temp;

cout<<"\nArray after swapping"<<endl;


for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}
Write a C++ program to find the largest and smallest element of an array.
ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,i,small,large;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}

small=Arr[0];
large=Arr[0];
for(i=1;i<n;i++)
{ if(Arr[i]<small)
small=Arr[i]; if(Arr[i]>large)
large=Arr[i];
}

cout<<"\nLargest element is :"<<large;


cout<<"\nSmallest element is :"<<small;
return 0;
}
Write a C++ program to reverse the element of an integer 1-D array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,temp,i,j;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}
for(i=0,j=n-1;i<n/2;i++,j--)
{
temp=Arr[i];
Arr[i]=Arr[j];
Arr[j]=temp;
}

cout<<"\nReverse array"<<endl;
for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}
Write a menu driven C++ program with following option
a. Accept elements of an array
b. Display elements of an array
c. Sort the array using insertion sort method
d. Sort the array using selection sort method
e. Sort the array using bubble sort method
Write C++ functions for all options. The functions should have two
parameters name of the array and number of elements in the array.

ANSI C++
Output
#include<iostream>
using namespace std;
void accept(int Arr[], int s);void display(int Arr[], int
s);void isort(int Arr[], int s);void ssort(int Arr[], int
s);void bsort(int Arr[],int s);
int main()
{ int Arr[100],n,choice;
cout<<"Enter Size of Array ";
cin>>n; do {
cout<<"\n\nMENU";
cout<<"\n1. Accept elements of array";
cout<<"\n2. Display elements of array";
cout<<"\n3. Sort the array using insertion sort
method";
cout<<"\n4. Sort the array using selection sort
method";
cout<<"\n5. Sort the array using bubble sort method";
cout<<"\n6. Exit";
cout<<"\n\nEnter your choice 1-6 :";
cin>>choice;
switch(choice)
{ case 1: accept(Arr,n);
break; case 2: display(Arr,n);
break; case 3: isort(Arr,n); break;
case 4: ssort(Arr,n); break; case 5:
bsort(Arr,n); break; case 6: break;
default:cout<<"\nInvalid choice";
}
}while(choice!=6);
return 0;
}
void accept(int Arr[], int s)
{ for(int I=0;I<s;I++)
{
cout<<"Enter element "<<I+1<<":";
cin>>Arr[I];
}
}
void display(int Arr[], int s)
{
cout<<"The elements of the array are:\n"; for(int
I=0;I<s;I++)
cout<<Arr[I]<<" ";
}
void isort(int Arr[], int s)
{ int I,J,Temp; for(I=1;I<s;I++)
{
Temp=Arr[I];
J=I-1; while((Temp<Arr[J]) && (J>=0))
{
Arr[J+1]=Arr[J];
J--;
}
Arr[J+1]=Temp;
}
}
void ssort(int Arr[], int s)
{ int I,J,Temp,Small; for(I=0;I<s-1;I++)
{
Small=I; for(J=I+1;J<s;J++) //finding the
smallest element
if(Arr[J]<Arr[Small])
Small=J; if(Small!=I)
{
Temp=Arr[I]; //Swapping
Arr[I]=Arr[Small];
Arr[Small]=Temp;
}
}
}
void bsort(int Arr[],int s)
{ int I,J,Temp; for(I=0;I<s-1;I++)
{ for(J=0;J<(s-1-I);J++)
if(Arr[J]>Arr[J+1])
{
Temp=Arr[J]; //swapping
Arr[J]=Arr[J+1];
Arr[J+1]=Temp;
}
}
}
P is one-dimensional array of integers. Write a C++ function to efficiently
search for a data VAL from P. If VAL is present in the array then the function
should return value 1 and 0 otherwise.

ANSI C++
Output
#include<iostream>
using namespace std;
bool lsearch(int Arr[], int s, int VAL);
int main()
{ int Arr[100],n,val; bool found;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}

cout<<"Enter the number you want to search ";


cin>>val;

found=lsearch(Arr,n,val);
if(found)
cout<<"\nItem found"; else
cout<<"\nItem not found";
return 0;
}
bool lsearch(int Arr[], int s, int VAL)
{ for(int I=0; I<s; I++)
{ if(Arr[I]==VAL) return true;
} return false;
}
Suppose a one-dimensional array AR containing integers is arranged in
ascending order. Write a user-defined function in C++ to search for an
integer from AR with the help of Binary search method, returning an integer
0 to show absence of the number and integer 1 to show presence of the
number in the array. Function should have three parameters : (i) array AR (ii)
the number to be searched and (iii) the number of elements N in the array.

ANSI C++
Output
#include<iostream>
using namespace std;
bool bsearch(int AR[], int N, int VAL);
int main()
{ int AR[100],n,val; bool found;

cout<<"Enter number of elements you want to insert ";


cin>>n;

cout<<"Enter element in ascending order\n";


for(int i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>AR[i];
}

cout<<"\nEnter the number you want to search ";


cin>>val;

found=bsearch(AR,n,val);
if(found)
cout<<"\nItem found"; else
cout<<"\nItem not found";

return 0;
}
bool bsearch(int AR[], int N, int VAL)
{ int Mid,Lbound=0,Ubound=N-1; while(Lbound<=Ubound)
{
Mid=(Lbound+Ubound)/2; if(VAL>AR[Mid])
Lbound=Mid+1; else if(VAL<AR[Mid])
Ubound=Mid-1; else
return true;
} returnfalse;
}
Suppose A, B, C are arrays of integers of size M, N, and M + N respectively.
The numbers in array A appear in ascending order while the numbers in array
B appear in descending order. Write a user defined function in C++ to
produce third array C by merging arrays A and B in ascending order. Use A,
B and C as arguments in the function.

ANSI C++
Output
#include<iostream>
using namespace std;
void Merge(int A[], int B[],int C[], int N,int M, int &K);

int main()
{ int A[100], B[100], C[200],i,n,m,k;

cout<<"\nEnter number of elements you want to insert in


first array ";
cin>>n;

cout<<"Enter element in ascending order\n";


for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>A[i];
}

cout<<"\nEnter number of elements you want to insert in


second array ";
cin>>m;

cout<<"Enter element in descending order\n";


for(i=0;i<m;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>B[i];
}

Merge(A,B,C,n,m,k);

cout<<"\nThe Merged Array in Ascending Order"<<endl;


for(i=0;i<k;i++)
{
cout<<C[i]<<" ";
}
return 0;
}

void Merge(int A[], int B[], int C[], int N, int M, int &K)
{ int I=0, J=M-1;
K=0;
while (I<N && J>=0)
{ if (A[I]<B[J])
C[K++]=A[I++]; else if (A[I]>B[J])
C[K++]=B[J--]; else {
C[K++]=A[I++];
J--;
}
}
for (int T=I;T<N;T++)
C[K++]=A[T]; for (int T=J;T>=0;T--)
C[K++]=B[T];
}
uppose X. Y, Z are arrays of integers of size M, N, and M + N respectively. The
numbers in array X and Y appear in descending order. Write a user-defined
function in C++ to produce third array Z by merging arrays X and Y in
descending order.

ANSI C++
Output
#include<iostream>
using namespace std;
void Merge(int A[], int B[], int C[], int N, int M, int &K);

int main()
{ int A[100], B[100], C[200],i,n,m,k;

cout<<"\nEnter number of elements you want to insert in


first array ";
cin>>n;

cout<<"Enter element in descending order\n";


for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>A[i];
}

cout<<"\nEnter number of elements you want to insert in


second array ";
cin>>m;
cout<<"Enter element in descending order\n";
for(i=0;i<m;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>B[i];
}

Merge(A,B,C,n,m,k);

cout<<"\nThe Merged Array in Descending Order"<<endl;


for(i=0;i<k;i++)
{
cout<<C[i]<<" ";
}
return 0;
}

void Merge(int A[], int B[], int C[], int N, int M, int &K)
{ int I=0, J=0;
K=0;
while (I<N && J<M)
{ if (A[I]>B[J])
C[K++]=A[I++]; else if (A[I]<B[J])
C[K++]=B[J++]; else {
C[K++]=A[I++];
J++;
}
}
for (int T=I;T<N;T++)
C[K++]=A[T]; for (int T=J;T<M;T++)
C[K++]=B[T];
}
Write a program to find the length of string.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout<<"Enter string: ";


cin.getline(str, 80);
int i; //Hold length of string

for(i = 0; str[i] != '\0'; i++);

cout << "Length of string is: " << i;


return 0;
}
Assignments » String » Set1 »Solution 2

Write a program to display string from backward.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout<<"Enter string: ";


cin.getline(str, 80);
int l; //Hold length of string

//Find the length of the string


for(l = 0; str[l] != '\0'; l++);
//Display the string backwards
for(int i = l -1; i >= 0; i--)
{
cout << str[i];
}

return 0;
}
Write a program to count number of words in string.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout << "Enter a string: ";


cin.getline(str,80);
int words = 0; // Holds number of words

for(int i = 0; str[i] != '\0'; i++)


{ if (str[i] == ' ') //Checking for spaces
{
words++;
}
}

cout << "The number of words = " << words+1 << endl;
return 0;
}
Write a program to concatenate one string contents to another.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str1[80], str2[80];
cout<<"Enter first string: ";
cin.getline(str1, 80);

cout<<"Enter second string: ";


cin.getline(str2, 80);
int l = 0; //Hold length of first string

//Find length of first string.


for(l = 0; str1[l] != '\0'; l++);
//Adding second string content in first
for(int i = 0; str2[i] != '\0'; i++)
{
str1[l++] = str2[i];
}

str1[l] ='\0';

cout << "\nThe first string after adding second string


content:\n\n" << str1;

return 0;
}
Write a C++ program to compare two strings they are exact equal or not.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str1[80], str2[80];

cout<<"Enter first string: ";


gets(str1);
cout<<"Enter second string: ";
gets(str2);
int i; for (i = 0; str1[i] == str2[i] && str1[i]!= '\0'
&& str2[i] != '\0'; i++);
if(str1[i] - str2[i] == 0)
cout << "Strings are equal"; else
cout << "Strings are not equal";
return 0;
}
rite a program to check a string is palindrome or not.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout<<"Enter string: ";


cin.getline(str, 80);
int l; //Hold length of string

//finding length of string


for(l = 0; str[l] !='\0'; l++);
//Comparing first element with last element till middle
of string
int i; for(i = 0; (i < l/2) && (str[i] == str[l - i -
1]); i++);
if(i == l/2)
cout << "Palindrome"; else
cout << "Not a palindrome";
return 0;
}

Write the output of the following program. Assume that all necessary header
files are included.
void Encrypt(char T[])
{
for (int i = 0; T[i] != '\0'; i += 2)
if (T[i] == 'A' || T[i] == 'E')
T[i] = '#';
else if (islower(T[i]))
T[i] = toupper(T[i]);
else
T[i] = '@';
}

int main()
{
char text[]="SaVE EArtH";
Encrypt(text);
cout << text << endl;

return 0;
}

Show the answer.


@a@E@E#rTH

12 Write the output of the following program


int main()
{
char name[] = "CoMPutER";
for (int x = 0; x < strlen(name);
x++)
if (islower(name [x]))
name [x] =
toupper(name[x]);
else
if (isupper(name[x]))
if (x % 2 == 0)
name[x] =
tolower(name[x]);
else
name[x] =
name[x-1];

cout << name;

return 0;
}
Show the answer.
cOmmUTee

Two numbers are entered through the keyboard. Write a program to find the
value of one number raised to the power of another.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,p,r=1;
cout<<"Enter the base number and exponent ";
cin>>n>>p;
for(int i=1;i<=p;i++)
r=r*n;

cout<<"Result :"<<r;

return 0;
}

Write a program to reverse any given integer number.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,t,r,rev=0;
cout<<"Enter any number : ";
cin>>n;
t=n;
while(t>0)
{
r=t%10;
t=t/10;
rev=rev*10+r;
}

cout<<"Reverse of number "<<n<<" is "<<rev;

return 0;
}
Write a program to sum of digits of given integer number.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,t,r,sum=0;
cout<<"Enter any number : ";
cin>>n;
t=n;
while(t>0)
{
r=t%10;
sum+=r;
t=t/10;
}

cout<<"Sum of digits of number "<<n<<" is "<<sum;

return 0;
rite a program to check given number is prime or not.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n;
bool flag=false;
cout<<"Enter any number : ";
cin>>n;
for(int i=2;i<n;i++)
{ if(n%i==0)
{
flag=true; break;
}
}
if(flag==false && n>1)
cout<<"Number is prime"; else
cout<<"Number is not prime";

return 0;
}
Write a program to calculate HCF of Two given number.
Source Code
Output

#include<iostream>
using namespace std;
intmain()
{
int dividend, divisor, rem, hcf;
cout<<"Enter two numbers : ";
cin>>dividend>>divisor;
while(rem!=0)
{
rem=dividend%divisor; if(rem==0)
hcf=divisor; else {
dividend=divisor;
divisor=rem;
}
}

cout<<"HCF is : "<<hcf;

return 0;
}

Write a program to enter the numbers till the user wants and at the end it
should display the count of positive, negative and zeros entered.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n, sum_p=0, sum_n=0, sum_z=0; char choice;
do {
cout<<"Enter number ";
cin>>n;
if(n>0)
sum_p++; else if(n<0)
sum_n++; else
sum_z++;

cout<<"Do you want to Continue(y/n)? ";


cin>>choice;

}while(choice=='y'|| choice=='Y');

cout<<"Positive Number :"<<sum_p<<"\nNegative


Number :"<<sum_n<<"\nZero Number :"<<sum_z;

return 0;
}

Write a program to enter the numbers till the user wants and at the end it
should display the maximum and minimum number entered.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n, max=0, min=32767; char choice;
do {
cout<<"Enter number : ";
cin>>n;
if(n>max)
max=n; if(n<min)
min=n;

cout<<"Do you want to Continue(y/n)? ";


cin>>choice;

}while(choice=='y' || choice=='Y');

cout<<"Maximum Number :"<<max<<"\nMinimum


Number :"<<min;

return 0;
}
Write a program to print all Armstrong numbers between 1 and 500. If sum
of cubes of each digit of the number is equal to the number itself, then the
number is called an Armstrong number.
For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,digit1,digit2,digit3;
for(int i=1;i<=500;i++)
{
digit1=i/100;
digit2=i/10 - digit1*10;
digit3=i%10;
if(digit1*digit1*digit1 + digit2*digit2*digit2 +
digit3*digit3*digit3 == i)
cout<<i<<endl;
}

return 0;
}

Write a program to print Fibonacci series of n terms where n is input by user


0 1 1 2 3 5 8 13 24 .....

Source Code
Output

#include<iostream>
using namespace std;

int main()
{
int f=0,s=1,t,n;

cout<<"Enter Number of terms of Series : ";


cin>>n;
cout<<f<<" "<<s<<" ";
for(int i=3;i<=n;i++)
{
t=f+s;
cout<<t<<" ";
f=s;
s=t;
}

return 0;
}
Write a program to calculate the sum of following series where n is input by
user.
1 + 1/2 + 1/3 + 1/4 + 1/5 +…………1/n

Source Code
Output

#include<iostream>
using namespace std;
int main()
{ int i,n; float sum=0;

cout<<"Enter the value of n ";


cin>>n;
for(i=1;i<=n;i++)
sum += 1.0/i;

cout<<"Sum : "<<sum;

return 0;
}
Compute the natural logarithm of 2, by adding up to n terms in the series
1 - 1/2 + 1/3 - 1/4 + 1/5 -... 1/n
where n is a positive integer and input by user.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{ int i,n,sign=-1; float sum=0;
cout<<"Enter the value of n ";
cin>>n;
for(i=1;i<=n;i++)
{
sign *= -1;
sum += sign*1.0/i;
}
cout<<"log 2 : "<<sum;

return 0;
}
Write C++ program to print following pattern:
i) ii) iii)
********** * *
********** ** **
********** *** ***
********** **** ****
***** *****
iv) v) 1 vi) 1
* 222 212
*** 33333 32123
4444444 4321234
***** 555555555 543212345
*******
*********
Source Code

//Solution of (i)
#include<iostream>
using namespace std;
int main()
{ int i,j; for(i=1;i<=4;i++)
{ for(j=1;j<=10;j++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (ii)
#include<iostream>
using namespace std;
int main()
{ int i,j; for(i=1;i<=5;i++)
{ for(j=1;j<=i;j++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (iii)
#include<iostream>
using namespace std;
int main()
{ int i,j,k; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=1;k<=i;k++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (iv)
#include<iostream>
using namespace std;
int main()
{ int i,j,k; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=1;k<2*i;k++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (v)
#include<iostream>
using namespace std;
intmain()
{ int i,j,k; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=1;k<2*i;k++)
cout<<i;
cout<<endl;
}

return0;
}

//Solution of (vi)
#include<iostream>
using namespace std;
intmain()
{ int i,j,k,l; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=i;k>=1;k--)
cout<<k; for(l=2;l<=i;l++)
cout<<l;
cout<<endl;
}

return0;
}
Write a program to compute sinx for given x. The user should supply x and a
positive integer n. We compute the sine of x using the series and the
computation should use all terms in the series up through the term involving
x
sin x = x - x3/3! + x5/5! - x7/7!+x9/9! .........

Source Code
Output

#include<iostream>
using namespace std;
int main()
{ int i,j,n,fact,sign=-1; float x, p=1,sum=0;
cout<<"Enter the value of x : ";
cin>>x;
cout<<"Enter the value of n : ";
cin>>n;
for(i=1;i<=n;i+=2)
{
fact=1; for(j=1;j<=i;j++)
{
p=p*x;
fact=fact*j;
}
sign=-1*sign;
sum+=sign*p/fact;
}
cout<<"sin "<<x<<"="<<sum;

return 0;
}
Write a program to compute the cosine of x. The user should supply x and a
positive integer n. We compute the cosine of x using the series and the
computation should use all terms in the series up through the term involving
xn
cos x = 1 - x2/2! + x4/4! - x6/6! -.....

Source Code
Output

#include<iostream>

using namespace std;

int main()

{
int i,j,n,fact,sign=-1;
float x, p=1,sum=0;

cout<<"Enter the value of x : ";

cin>>x;

cout<<"Enter the value of n : ";


cin>>n;

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

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

p=p*x;

fact=fact*j;

sum+=sign*p/fact;

sign=-1*sign;

cout<<"cos "<<x<<"="<<1+sum;

return 0;

}
Write a program which input principal, rate and time from user and calculate
compound interest.
CI = P(1+R/100)T - P

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cmath>
using namespace std;
int main()
{ float P,R,T,CI;
cout<<"Enter Principal, Rate and Time : ";
cin>>P>>R>>T;
CI=P*pow((1+R/100),T) - P;
cout<<"Compound Interest is : "<<CI;
return 0;
}
Write a program to compute area of triangle. Sides are input by user.
Area = sqrt(s*(s-a)*(s-b)*(s-c))
where s=(a+b+c)/2

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cmath>
using namespace std;
int main()
{ float a,b,c,s,Area;
cout<<"Enter three sides of triangle : ";
cin>>a>>b>>c;
s=(a+b+c)/2;
Area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"Area of triangle is : "<<Area;

return 0;
}
Write a program to check character entered is alphabet, digit or special
character.

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cctype>
using namespace std;
int main()
{ char ch;
cout<<"Enter any character :";
ch=getchar(); if(isalpha(ch))
cout<<"Alphabet"; else if(isdigit(ch))
cout<<"Number"; else
cout<<"Special Character";
return 0;
}
Write a program which display a number between 10 to 100 randomly.

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cstdlib>
#include <ctime>
using namespace std;
int main()
{ int n;
srand(time(0));
n = rand() % 91 + 10;
cout<<"The randomly selected number is :"<<n;
return 0;
}
Write a program which accept a letter and display it in uppercase letter.

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cctype>
using namespace std;
int main()
{ char ch;
cout<<"Enter any character :";
ch=getchar();
ch=toupper(ch);
cout<<ch;

return 0;
}
Write a C++ program to implement the Number Guessing Game. In this
game the computer chooses a random number between 1 and 100, and the
player tries to guess the number in as few attempts as possible. Each time
the player enters a guess, the computer tells him whether the guess is too
high, too low, or right. Once the player guesses the number, the game is
over.

ANSI C++
Pre-Standard C++
Output
#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main()

{
int num, guess, tries = 0;

srand(time(0)); //seed random number generator


num = rand() % 100 + 1; // random number between 1 and 100

cout << "Guess My Number Game\n\n";


do
{

cout << "Enter a guess between 1 and 100 : ";

cin >> guess;

tries++;
if (guess > num)

cout << "Too high!\n\n";


else if (guess < num)

cout << "Too low!\n\n";


else

cout << "\nCorrect! You got it in " << tries << "
guesses!\n";

} while (guess != num);

cin.ignore();

cin.get();
return 0;
}
Write a C++ program using function which accept two integers as an
argument and return its sum. Call this function from main( ) and print the
results in main( ).

ANSI C++
Output
#include<iostream>
using namespace std;
int sum(int, int);
int main()
{ int x,y,s;
cout<<"Enter first number : ";
cin>>x;
cout<<"Enter second number : ";
cin>>y;
s=sum(x,y);
cout<<"The sum is : "<<s;

return 0;
}
int sum(int a, int b)
{
int total;
total=a+b; return total;
}
Write a function to calculate the factorial value of any integer as an argument.
Call this function from main( ) and print the results in main( ).

ANSI C++
Output

#include<iostream>
using namespace std;
int factorial(int);
int main()
{ int x,f;
cout<<"Enter number : ";
cin>>x;
f=factorial(x);
cout<<"The factorial is :"<<f;
return 0;
}
int factorial(int a)
{ int fact=1;
while(a>=1)
{
fact=fact*a;
a--;
}
return fact;
}
Write a function that receives two numbers as an argument and display all
prime numbers between these two numbers. Call this function from main( ).

ANSI C++
Output
#include<iostream>
using namespace std;
void showprime(int, int);
int main()
{
int x,y;
cout<<"Enter first number : ";
cin>>x;

cout<<"Enter second number : ";


cin>>y;
showprime(x,y);

return 0;
}
void showprime(int a, int b)
{ bool flag;
for(int i=a+1;i<=b;i++)
{

flag=false; for(int j=2;j<i;j++)


{
if(i%j==0)
{
flag=true; break;
}
}
if(flag==false && i>1)
cout<<i<<endl;
}
}
Raising a number to a power p is the same as multiplying n by itself p times.
Write a function called power that takes two arguments, a double value for n
and an int value for p, and return the result as double value. Use default
argument of 2 for p, so that if this argument is omitted the number will be
squared. Write the main function that gets value from the user to test power
function.

ANSI C++
Output

#include<iostream>
using namespace std;
double power(double,int=2);
int main()
{ int p; double n,r;
cout << "Enter number : ";
cin >> n;
cout << "Enter exponent : ";
cin >> p;
r = power(n,p);
cout << "Result is " << r;
r = power(n);
cout << "\nResult without passing exponent is " << r;
return 0;
}
double power(double a, int b)
{ double x = 1; for(int i = 1; i <= b; i++)
x = x * a; return x;
}
Write the output of the following program :
#include <iostream>
using namespace std;

void X(int &A, int &B)


{
A = A + B;
B = A - B;
A = A - B;
}

int main()
{
int a = 4, b = 18;
X(a,b);
cout << a << ", " << b;

return 0;
}

Show the answer.


18, 4

7 Write the output of the following program:

#include <iostream>
using namespace std;

void X(int A, int &B)


{
A = A + B;
B = A - B;
A = A - B;
}
int main()
{
int a = 4, b = 18;
X(a,b);
cout << a << ", " << b;

return 0;
}

Show the answer.


4, 4

8 Write the output of the following program:


#include <iostream>
using namespace std;

void Execute(int &B, int C = 100)


{
int temp = B + C;
B += temp;
if (C == 100)
cout << temp << " " << B << " " << C << endl;
}

int main()
{
int M = 90, N = 10;
Execute(M);
cout << M << " " << N << endl;
Execute(M, N);
cout << M << " " << N << endl;

return 0;
}

Show the answer.


190 280 100
280 10
570 10

9 Give the output of the following program


#include <iostream>
using namespace std;
int global = 10;

void func(int &x, int y)


{
x = x - y;
y = x * 10;
cout << x << ", " << y << '\n';
}

int main()
{
int global = 7;
func (::global, global);
cout << global << ", " << ::global << '\n';

func(global, ::global);
cout << global << ", " << ::global << '\n';

return 0;
}

Show the answer.


3, 30
7, 3
4, 40
4, 3

10 Write the output of the following program :


#include <iostream>
using namespace std;

static int i = 100;


void abc()
{
static int i = 8;
cout << "first = " << i++ << endl;
}

int main()
{
static int i = 2;
abc();
cout << "second = " << i << endl;
abc();

return 0;
}
Show the answer.
first = 8
second = 2
first = 9

11 Write the output of the following program:


#include <iostream>
using namespace std;

int func(int &x, int y = 10)


{
if (x % y == 0)
return ++x;
else
return y--;
}

int main()
{
int p = 20, q = 23;

q = func(p, q);
cout << p << " " << " " << q << endl;

p = func (q);
cout << p << " " << " " << q << endl;

q = func (p);
cout << p << " " << " " << q << endl;

return 0;
}

Show the answer.


20 23
10 23
11 11

Write a program that lets the user perform arithmetic operations on two
numbers. Your program must be menu driven, allowing the user to select the
operation (+, -, *, or /) and input the numbers. Furthermore, your program
must consist of following functions:
1. Function showChoice: This function shows the options to the user and
explains how to enter data.
2. Function add: This function accepts two number as arguments and returns
sum.
3. Function subtract: This function accepts two number as arguments and
returns their difference.
4. Function mulitiply: This function accepts two number as arguments and
returns product.
5. Function divide: This function accepts two number as arguments and
returns quotient.

ANSI C++
Output

#include <iostream>
using namespace std;
void showChoices();float add(float, float);float
subtract(float, float);float multiply(float, float);float
divide(float, float);
int main()
{ float x, y; int choice; do {
showChoices();
cin >> choice; switch (choice)
{ case 1:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Sum " << add(x,y) <<endl; break;
case 2:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Difference " << subtract(x,y) <<endl;
break; case 3:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Product " << multiply(x,y) <<endl;
break; case 4:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Quotient " << divide(x,y) <<endl;
break; case 5: break; default:
cout << "Invalid input" << endl;
}
}while (choice != 5);
return 0;
}
void showChoices()
{
cout << "MENU" << endl;
cout << "1: Add " << endl;
cout << "2: Subtract" << endl;
cout << "3: Multiply " << endl;
cout << "4: Divide " << endl;
cout << "5: Exit " << endl;
cout << "Enter your choice :";
}
float add(float a, float b)
{ return a + b;
}
float subtract(float a, float b)
{ return a - b;
}
float multiply(float a, float b)
{ return a * b;
}
float divide(float a, float b)
{ return a / b;
}
Write a C++ program to find the sum and average of one dimensional integer
array.

ANSI C++
Output
#include<iostream>
using namespace std;
intmain()
{ int Arr[100],n,i,sum=0;
cout<<"Enter number of elements you want to insert ";
cin>>n;
for(i=0;i<n;i++)
{

cout<<"Enter element "<<i+1<<":";


cin>>Arr[i];
}
for(i=0;i<n;i++)
sum+=Arr[i];

cout<<"\nThe sum of Array is :"<<sum;


cout<<"\nThe average of Array is :"<<sum/i;
return 0;
}
Write a C++ program to swap first and last element of an integer 1-d array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,i,temp;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{

cout<<"Enter element "<<i+1<<":";


cin>>Arr[i];
}
temp=Arr[0];
Arr[0]=Arr[n-1];

Arr[n-1]=temp;

cout<<"\nArray after swapping"<<endl;


for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}
Write a C++ program to swap first and last element of an integer 1-d array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,i,temp;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{

cout<<"Enter element "<<i+1<<":";


cin>>Arr[i];
}

temp=Arr[0];
Arr[0]=Arr[n-1];

Arr[n-1]=temp;
cout<<"\nArray after swapping"<<endl;
for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}
Write a C++ program to find the largest and smallest element of an array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,i,small,large;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}

small=Arr[0];
large=Arr[0];
for(i=1;i<n;i++)
{ if(Arr[i]<small)
small=Arr[i]; if(Arr[i]>large)
large=Arr[i];
}

cout<<"\nLargest element is :"<<large;


cout<<"\nSmallest element is :"<<small;
return 0;
}
Write a C++ program to reverse the element of an integer 1-D array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,temp,i,j;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}
for(i=0,j=n-1;i<n/2;i++,j--)
{
temp=Arr[i];
Arr[i]=Arr[j];
Arr[j]=temp;
}

cout<<"\nReverse array"<<endl;
for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}
Write a menu driven C++ program with following option
a. Accept elements of an array
b. Display elements of an array
c. Sort the array using insertion sort method
d. Sort the array using selection sort method
e. Sort the array using bubble sort method
Write C++ functions for all options. The functions should have two
parameters name of the array and number of elements in the array.

ANSI C++
Output
#include<iostream>
using namespace std;
void accept(int Arr[], int s);void display(int Arr[], int
s);void isort(int Arr[], int s);void ssort(int Arr[], int
s);void bsort(int Arr[],int s);
int main()
{ int Arr[100],n,choice;
cout<<"Enter Size of Array ";
cin>>n; do {
cout<<"\n\nMENU";
cout<<"\n1. Accept elements of array";
cout<<"\n2. Display elements of array";
cout<<"\n3. Sort the array using insertion sort
method";
cout<<"\n4. Sort the array using selection sort
method";
cout<<"\n5. Sort the array using bubble sort method";
cout<<"\n6. Exit";
cout<<"\n\nEnter your choice 1-6 :";
cin>>choice;
switch(choice)
{ case 1: accept(Arr,n);
break; case 2: display(Arr,n);
break; case 3: isort(Arr,n); break;
case 4: ssort(Arr,n); break; case 5:
bsort(Arr,n); break; case 6: break;
default:cout<<"\nInvalid choice";
}
}while(choice!=6);
return 0;
}
void accept(int Arr[], int s)
{ for(int I=0;I<s;I++)
{
cout<<"Enter element "<<I+1<<":";
cin>>Arr[I];
}
}
void display(int Arr[], int s)
{
cout<<"The elements of the array are:\n"; for(int
I=0;I<s;I++)
cout<<Arr[I]<<" ";
}
void isort(int Arr[], int s)
{ int I,J,Temp; for(I=1;I<s;I++)
{
Temp=Arr[I];
J=I-1; while((Temp<Arr[J]) && (J>=0))
{
Arr[J+1]=Arr[J];
J--;
}
Arr[J+1]=Temp;
}
}
void ssort(int Arr[], int s)
{ int I,J,Temp,Small; for(I=0;I<s-1;I++)
{
Small=I; for(J=I+1;J<s;J++) //finding the
smallest element
if(Arr[J]<Arr[Small])
Small=J; if(Small!=I)
{
Temp=Arr[I]; //Swapping
Arr[I]=Arr[Small];
Arr[Small]=Temp;
}
}
}
void bsort(int Arr[],int s)
{ int I,J,Temp; for(I=0;I<s-1;I++)
{ for(J=0;J<(s-1-I);J++)
if(Arr[J]>Arr[J+1])
{
Temp=Arr[J]; //swapping
Arr[J]=Arr[J+1];
Arr[J+1]=Temp;
}
}
}
P is one-dimensional array of integers. Write a C++ function to efficiently
search for a data VAL from P. If VAL is present in the array then the function
should return value 1 and 0 otherwise.

ANSI C++
Output
#include<iostream>
using namespace std;
bool lsearch(int Arr[], int s, int VAL);
int main()
{ int Arr[100],n,val; bool found;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}

cout<<"Enter the number you want to search ";


cin>>val;

found=lsearch(Arr,n,val);
if(found)
cout<<"\nItem found"; else
cout<<"\nItem not found";

return 0;
}
bool lsearch(int Arr[], int s, int VAL)
{ for(int I=0; I<s; I++)
{ if(Arr[I]==VAL) return true;
} return false;
}
Suppose a one-dimensional array AR containing integers is arranged in
ascending order. Write a user-defined function in C++ to search for an
integer from AR with the help of Binary search method, returning an integer
0 to show absence of the number and integer 1 to show presence of the
number in the array. Function should have three parameters : (i) array AR (ii)
the number to be searched and (iii) the number of elements N in the array.

ANSI C++
Output
#include<iostream>
using namespace std;
bool bsearch(int AR[], int N, int VAL);
int main()
{ int AR[100],n,val; bool found;

cout<<"Enter number of elements you want to insert ";


cin>>n;

cout<<"Enter element in ascending order\n";


for(int i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>AR[i];
}

cout<<"\nEnter the number you want to search ";


cin>>val;

found=bsearch(AR,n,val);
if(found)
cout<<"\nItem found"; else
cout<<"\nItem not found";

return 0;
}
bool bsearch(int AR[], int N, int VAL)
{ int Mid,Lbound=0,Ubound=N-1; while(Lbound<=Ubound)
{
Mid=(Lbound+Ubound)/2; if(VAL>AR[Mid])
Lbound=Mid+1; else if(VAL<AR[Mid])
Ubound=Mid-1; else
return true;
} returnfalse;
}
Suppose A, B, C are arrays of integers of size M, N, and M + N respectively.
The numbers in array A appear in ascending order while the numbers in array
B appear in descending order. Write a user defined function in C++ to
produce third array C by merging arrays A and B in ascending order. Use A,
B and C as arguments in the function.

ANSI C++
Output
#include<iostream>
using namespace std;
void Merge(int A[], int B[],int C[], int N,int M, int &K);

int main()
{ int A[100], B[100], C[200],i,n,m,k;
cout<<"\nEnter number of elements you want to insert in
first array ";
cin>>n;

cout<<"Enter element in ascending order\n";


for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>A[i];
}

cout<<"\nEnter number of elements you want to insert in


second array ";
cin>>m;

cout<<"Enter element in descending order\n";


for(i=0;i<m;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>B[i];
}

Merge(A,B,C,n,m,k);

cout<<"\nThe Merged Array in Ascending Order"<<endl;


for(i=0;i<k;i++)
{
cout<<C[i]<<" ";
}
return 0;
}
void Merge(int A[], int B[], int C[], int N, int M, int &K)
{ int I=0, J=M-1;
K=0;
while (I<N && J>=0)
{ if (A[I]<B[J])
C[K++]=A[I++]; else if (A[I]>B[J])
C[K++]=B[J--]; else {
C[K++]=A[I++];
J--;
}
}
for (int T=I;T<N;T++)
C[K++]=A[T]; for (int T=J;T>=0;T--)
C[K++]=B[T];
}
uppose X. Y, Z are arrays of integers of size M, N, and M + N respectively. The
numbers in array X and Y appear in descending order. Write a user-defined
function in C++ to produce third array Z by merging arrays X and Y in
descending order.

ANSI C++
Output
#include<iostream>
using namespace std;
void Merge(int A[], int B[], int C[], int N, int M, int &K);

int main()
{ int A[100], B[100], C[200],i,n,m,k;

cout<<"\nEnter number of elements you want to insert in


first array ";
cin>>n;

cout<<"Enter element in descending order\n";


for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>A[i];
}

cout<<"\nEnter number of elements you want to insert in


second array ";
cin>>m;

cout<<"Enter element in descending order\n";


for(i=0;i<m;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>B[i];
}

Merge(A,B,C,n,m,k);

cout<<"\nThe Merged Array in Descending Order"<<endl;


for(i=0;i<k;i++)
{
cout<<C[i]<<" ";
}
return 0;
}

void Merge(int A[], int B[], int C[], int N, int M, int &K)
{ int I=0, J=0;
K=0;
while (I<N && J<M)
{ if (A[I]>B[J])
C[K++]=A[I++]; else if (A[I]<B[J])
C[K++]=B[J++]; else {
C[K++]=A[I++];
J++;
}
}
for (int T=I;T<N;T++)
C[K++]=A[T]; for (int T=J;T<M;T++)
C[K++]=B[T];
}
Write a program to find the length of string.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout<<"Enter string: ";


cin.getline(str, 80);
int i; //Hold length of string

for(i = 0; str[i] != '\0'; i++);

cout << "Length of string is: " << i;


return 0;
}
Assignments » String » Set1 »Solution 2

Write a program to display string from backward.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout<<"Enter string: ";


cin.getline(str, 80);
int l; //Hold length of string

//Find the length of the string


for(l = 0; str[l] != '\0'; l++);
//Display the string backwards
for(int i = l -1; i >= 0; i--)
{
cout << str[i];
}

return 0;
}
Write a program to count number of words in string.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout << "Enter a string: ";


cin.getline(str,80);
int words = 0; // Holds number of words

for(int i = 0; str[i] != '\0'; i++)


{ if (str[i] == ' ') //Checking for spaces
{
words++;
}
}

cout << "The number of words = " << words+1 << endl;
return 0;
}
Write a program to concatenate one string contents to another.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str1[80], str2[80];

cout<<"Enter first string: ";


cin.getline(str1, 80);

cout<<"Enter second string: ";


cin.getline(str2, 80);
int l = 0; //Hold length of first string

//Find length of first string.


for(l = 0; str1[l] != '\0'; l++);
//Adding second string content in first
for(int i = 0; str2[i] != '\0'; i++)
{
str1[l++] = str2[i];
}

str1[l] ='\0';

cout << "\nThe first string after adding second string


content:\n\n" << str1;

return 0;
}
Write a C++ program to compare two strings they are exact equal or not.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str1[80], str2[80];

cout<<"Enter first string: ";


gets(str1);

cout<<"Enter second string: ";


gets(str2);
int i; for (i = 0; str1[i] == str2[i] && str1[i]!= '\0'
&& str2[i] != '\0'; i++);
if(str1[i] - str2[i] == 0)
cout << "Strings are equal"; else
cout << "Strings are not equal";
return 0;
}
rite a program to check a string is palindrome or not.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout<<"Enter string: ";


cin.getline(str, 80);
int l; //Hold length of string

//finding length of string


for(l = 0; str[l] !='\0'; l++);
//Comparing first element with last element till middle
of string
int i; for(i = 0; (i < l/2) && (str[i] == str[l - i -
1]); i++);
if(i == l/2)
cout << "Palindrome"; else
cout << "Not a palindrome";
return 0;
}

Write the output of the following program. Assume that all necessary header
files are included.
void Encrypt(char T[])
{
for (int i = 0; T[i] != '\0'; i += 2)
if (T[i] == 'A' || T[i] == 'E')
T[i] = '#';
else if (islower(T[i]))
T[i] = toupper(T[i]);
else
T[i] = '@';
}

int main()
{
char text[]="SaVE EArtH";
Encrypt(text);
cout << text << endl;

return 0;
}

Show the answer.


@a@E@E#rTH

12 Write the output of the following program


int main()
{
char name[] = "CoMPutER";
for (int x = 0; x < strlen(name);
x++)
if (islower(name [x]))
name [x] =
toupper(name[x]);
else
if (isupper(name[x]))
if (x % 2 == 0)
name[x] =
tolower(name[x]);
else
name[x] =
name[x-1];

cout << name;

return 0;
}

Show the answer.


cOmmUTee

Two numbers are entered through the keyboard. Write a program to find the
value of one number raised to the power of another.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,p,r=1;
cout<<"Enter the base number and exponent ";
cin>>n>>p;
for(int i=1;i<=p;i++)
r=r*n;

cout<<"Result :"<<r;

return 0;
}

Write a program to reverse any given integer number.


Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,t,r,rev=0;
cout<<"Enter any number : ";
cin>>n;
t=n;
while(t>0)
{
r=t%10;
t=t/10;
rev=rev*10+r;
}

cout<<"Reverse of number "<<n<<" is "<<rev;

return 0;
}
Write a program to sum of digits of given integer number.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,t,r,sum=0;
cout<<"Enter any number : ";
cin>>n;
t=n;
while(t>0)
{
r=t%10;
sum+=r;
t=t/10;
}

cout<<"Sum of digits of number "<<n<<" is "<<sum;

return 0;
rite a program to check given number is prime or not.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n;
bool flag=false;
cout<<"Enter any number : ";
cin>>n;
for(int i=2;i<n;i++)
{ if(n%i==0)
{
flag=true; break;
}
}
if(flag==false && n>1)
cout<<"Number is prime"; else
cout<<"Number is not prime";

return 0;
}
Write a program to calculate HCF of Two given number.

Source Code
Output

#include<iostream>
using namespace std;
intmain()
{
int dividend, divisor, rem, hcf;
cout<<"Enter two numbers : ";
cin>>dividend>>divisor;
while(rem!=0)
{
rem=dividend%divisor; if(rem==0)
hcf=divisor; else {
dividend=divisor;
divisor=rem;
}
}

cout<<"HCF is : "<<hcf;

return 0;
}

Write a program to enter the numbers till the user wants and at the end it
should display the count of positive, negative and zeros entered.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n, sum_p=0, sum_n=0, sum_z=0; char choice;
do {
cout<<"Enter number ";
cin>>n;
if(n>0)
sum_p++; else if(n<0)
sum_n++; else
sum_z++;

cout<<"Do you want to Continue(y/n)? ";


cin>>choice;

}while(choice=='y'|| choice=='Y');

cout<<"Positive Number :"<<sum_p<<"\nNegative


Number :"<<sum_n<<"\nZero Number :"<<sum_z;

return 0;
}
Write a program to enter the numbers till the user wants and at the end it
should display the maximum and minimum number entered.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n, max=0, min=32767; char choice;
do {
cout<<"Enter number : ";
cin>>n;
if(n>max)
max=n; if(n<min)
min=n;

cout<<"Do you want to Continue(y/n)? ";


cin>>choice;

}while(choice=='y' || choice=='Y');

cout<<"Maximum Number :"<<max<<"\nMinimum


Number :"<<min;

return 0;
}
Write a program to print all Armstrong numbers between 1 and 500. If sum
of cubes of each digit of the number is equal to the number itself, then the
number is called an Armstrong number.
For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )
Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,digit1,digit2,digit3;
for(int i=1;i<=500;i++)
{
digit1=i/100;
digit2=i/10 - digit1*10;
digit3=i%10;
if(digit1*digit1*digit1 + digit2*digit2*digit2 +
digit3*digit3*digit3 == i)
cout<<i<<endl;
}

return 0;
}

Write a program to print Fibonacci series of n terms where n is input by user


0 1 1 2 3 5 8 13 24 .....

Source Code
Output

#include<iostream>
using namespace std;

int main()
{
int f=0,s=1,t,n;

cout<<"Enter Number of terms of Series : ";


cin>>n;

cout<<f<<" "<<s<<" ";


for(int i=3;i<=n;i++)
{
t=f+s;
cout<<t<<" ";
f=s;
s=t;
}

return 0;
}
Write a program to calculate the sum of following series where n is input by
user.
1 + 1/2 + 1/3 + 1/4 + 1/5 +…………1/n

Source Code
Output

#include<iostream>
using namespace std;
int main()
{ int i,n; float sum=0;

cout<<"Enter the value of n ";


cin>>n;
for(i=1;i<=n;i++)
sum += 1.0/i;
cout<<"Sum : "<<sum;

return 0;
}
Compute the natural logarithm of 2, by adding up to n terms in the series
1 - 1/2 + 1/3 - 1/4 + 1/5 -... 1/n
where n is a positive integer and input by user.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{ int i,n,sign=-1; float sum=0;
cout<<"Enter the value of n ";
cin>>n;
for(i=1;i<=n;i++)
{
sign *= -1;
sum += sign*1.0/i;
}
cout<<"log 2 : "<<sum;

return 0;
}
Write C++ program to print following pattern:
i) ii) iii)
********** * *
********** ** **
********** *** ***
********** **** ****
***** *****
iv) v) 1 vi) 1
* 222 212
*** 33333 32123
4444444 4321234
***** 555555555 543212345
*******
*********
Source Code

//Solution of (i)
#include<iostream>
using namespace std;
int main()
{ int i,j; for(i=1;i<=4;i++)
{ for(j=1;j<=10;j++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (ii)
#include<iostream>
using namespace std;
int main()
{ int i,j; for(i=1;i<=5;i++)
{ for(j=1;j<=i;j++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (iii)
#include<iostream>
using namespace std;
int main()
{ int i,j,k; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=1;k<=i;k++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (iv)
#include<iostream>
using namespace std;
int main()
{ int i,j,k; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=1;k<2*i;k++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (v)
#include<iostream>
using namespace std;
intmain()
{ int i,j,k; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=1;k<2*i;k++)
cout<<i;
cout<<endl;
}

return0;
}

//Solution of (vi)
#include<iostream>
using namespace std;
intmain()
{ int i,j,k,l; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=i;k>=1;k--)
cout<<k; for(l=2;l<=i;l++)
cout<<l;
cout<<endl;
}

return0;
}
Write a program to compute sinx for given x. The user should supply x and a
positive integer n. We compute the sine of x using the series and the
computation should use all terms in the series up through the term involving
x
sin x = x - x3/3! + x5/5! - x7/7!+x9/9! .........

Source Code
Output

#include<iostream>
using namespace std;
int main()
{ int i,j,n,fact,sign=-1; float x, p=1,sum=0;
cout<<"Enter the value of x : ";
cin>>x;
cout<<"Enter the value of n : ";
cin>>n;
for(i=1;i<=n;i+=2)
{
fact=1; for(j=1;j<=i;j++)
{
p=p*x;
fact=fact*j;
}
sign=-1*sign;
sum+=sign*p/fact;
}
cout<<"sin "<<x<<"="<<sum;

return 0;
}
Write a program to compute the cosine of x. The user should supply x and a
positive integer n. We compute the cosine of x using the series and the
computation should use all terms in the series up through the term involving
xn
cos x = 1 - x2/2! + x4/4! - x6/6! -.....

Source Code
Output

#include<iostream>

using namespace std;

int main()

{
int i,j,n,fact,sign=-1;
float x, p=1,sum=0;
cout<<"Enter the value of x : ";

cin>>x;

cout<<"Enter the value of n : ";

cin>>n;

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

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

p=p*x;

fact=fact*j;

sum+=sign*p/fact;

sign=-1*sign;

cout<<"cos "<<x<<"="<<1+sum;
return 0;

}
Write a program which input principal, rate and time from user and calculate
compound interest.
CI = P(1+R/100)T - P

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cmath>
using namespace std;
int main()
{ float P,R,T,CI;
cout<<"Enter Principal, Rate and Time : ";
cin>>P>>R>>T;
CI=P*pow((1+R/100),T) - P;
cout<<"Compound Interest is : "<<CI;
return 0;
}
Write a program to compute area of triangle. Sides are input by user.
Area = sqrt(s*(s-a)*(s-b)*(s-c))
where s=(a+b+c)/2

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cmath>
using namespace std;
int main()
{ float a,b,c,s,Area;
cout<<"Enter three sides of triangle : ";
cin>>a>>b>>c;
s=(a+b+c)/2;
Area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"Area of triangle is : "<<Area;

return 0;
}
Write a program to check character entered is alphabet, digit or special
character.

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cctype>
using namespace std;
int main()
{ char ch;
cout<<"Enter any character :";
ch=getchar(); if(isalpha(ch))
cout<<"Alphabet"; else if(isdigit(ch))
cout<<"Number"; else
cout<<"Special Character";
return 0;
}
Write a program which display a number between 10 to 100 randomly.

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cstdlib>
#include <ctime>
using namespace std;
int main()
{ int n;
srand(time(0));
n = rand() % 91 + 10;
cout<<"The randomly selected number is :"<<n;
return 0;
}
Write a program which accept a letter and display it in uppercase letter.

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cctype>
using namespace std;
int main()
{ char ch;
cout<<"Enter any character :";
ch=getchar();
ch=toupper(ch);
cout<<ch;

return 0;
}
Write a C++ program to implement the Number Guessing Game. In this
game the computer chooses a random number between 1 and 100, and the
player tries to guess the number in as few attempts as possible. Each time
the player enters a guess, the computer tells him whether the guess is too
high, too low, or right. Once the player guesses the number, the game is
over.

ANSI C++
Pre-Standard C++
Output
#include <iostream>

#include <cstdlib>

#include <ctime>
using namespace std;

int main()

{
int num, guess, tries = 0;

srand(time(0)); //seed random number generator

num = rand() % 100 + 1; // random number between 1 and 100

cout << "Guess My Number Game\n\n";


do
{

cout << "Enter a guess between 1 and 100 : ";

cin >> guess;

tries++;
if (guess > num)

cout << "Too high!\n\n";


else if (guess < num)

cout << "Too low!\n\n";


else

cout << "\nCorrect! You got it in " << tries << "
guesses!\n";

} while (guess != num);


cin.ignore();

cin.get();
return 0;

}
Write a C++ program using function which accept two integers as an
argument and return its sum. Call this function from main( ) and print the
results in main( ).

ANSI C++
Output
#include<iostream>
using namespace std;
int sum(int, int);
int main()
{ int x,y,s;
cout<<"Enter first number : ";
cin>>x;
cout<<"Enter second number : ";
cin>>y;
s=sum(x,y);
cout<<"The sum is : "<<s;

return 0;
}
int sum(int a, int b)
{
int total;
total=a+b; return total;
}
Write a function to calculate the factorial value of any integer as an argument.
Call this function from main( ) and print the results in main( ).
ANSI C++
Output

#include<iostream>
using namespace std;
int factorial(int);
int main()
{ int x,f;
cout<<"Enter number : ";
cin>>x;
f=factorial(x);
cout<<"The factorial is :"<<f;
return 0;
}
int factorial(int a)
{ int fact=1;
while(a>=1)
{
fact=fact*a;
a--;
}
return fact;
}
Write a function that receives two numbers as an argument and display all
prime numbers between these two numbers. Call this function from main( ).

ANSI C++
Output
#include<iostream>
using namespace std;
void showprime(int, int);
int main()
{
int x,y;
cout<<"Enter first number : ";
cin>>x;
cout<<"Enter second number : ";
cin>>y;
showprime(x,y);

return 0;
}
void showprime(int a, int b)
{ bool flag;
for(int i=a+1;i<=b;i++)
{

flag=false; for(int j=2;j<i;j++)


{
if(i%j==0)
{
flag=true; break;
}
}
if(flag==false && i>1)
cout<<i<<endl;
}
}
Raising a number to a power p is the same as multiplying n by itself p times.
Write a function called power that takes two arguments, a double value for n
and an int value for p, and return the result as double value. Use default
argument of 2 for p, so that if this argument is omitted the number will be
squared. Write the main function that gets value from the user to test power
function.

ANSI C++
Output

#include<iostream>
using namespace std;
double power(double,int=2);
int main()
{ int p; double n,r;
cout << "Enter number : ";
cin >> n;
cout << "Enter exponent : ";
cin >> p;
r = power(n,p);
cout << "Result is " << r;
r = power(n);
cout << "\nResult without passing exponent is " << r;
return 0;
}
double power(double a, int b)
{ double x = 1; for(int i = 1; i <= b; i++)
x = x * a; return x;
}
Write the output of the following program :
#include <iostream>
using namespace std;

void X(int &A, int &B)


{
A = A + B;
B = A - B;
A = A - B;
}

int main()
{
int a = 4, b = 18;
X(a,b);
cout << a << ", " << b;

return 0;
}

Show the answer.


18, 4
7 Write the output of the following program:

#include <iostream>
using namespace std;

void X(int A, int &B)


{
A = A + B;
B = A - B;
A = A - B;
}

int main()
{
int a = 4, b = 18;
X(a,b);
cout << a << ", " << b;

return 0;
}

Show the answer.


4, 4

8 Write the output of the following program:


#include <iostream>
using namespace std;

void Execute(int &B, int C = 100)


{
int temp = B + C;
B += temp;
if (C == 100)
cout << temp << " " << B << " " << C << endl;
}

int main()
{
int M = 90, N = 10;
Execute(M);
cout << M << " " << N << endl;
Execute(M, N);
cout << M << " " << N << endl;

return 0;
}
Show the answer.
190 280 100
280 10
570 10

9 Give the output of the following program


#include <iostream>
using namespace std;

int global = 10;

void func(int &x, int y)


{
x = x - y;
y = x * 10;
cout << x << ", " << y << '\n';
}

int main()
{
int global = 7;
func (::global, global);
cout << global << ", " << ::global << '\n';

func(global, ::global);
cout << global << ", " << ::global << '\n';

return 0;
}

Show the answer.


3, 30
7, 3
4, 40
4, 3

10 Write the output of the following program :


#include <iostream>
using namespace std;

static int i = 100;


void abc()
{
static int i = 8;
cout << "first = " << i++ << endl;
}

int main()
{
static int i = 2;
abc();
cout << "second = " << i << endl;
abc();

return 0;
}

Show the answer.


first = 8
second = 2
first = 9

11 Write the output of the following program:


#include <iostream>
using namespace std;

int func(int &x, int y = 10)


{
if (x % y == 0)
return ++x;
else
return y--;
}

int main()
{
int p = 20, q = 23;

q = func(p, q);
cout << p << " " << " " << q << endl;

p = func (q);
cout << p << " " << " " << q << endl;

q = func (p);
cout << p << " " << " " << q << endl;

return 0;
}
Show the answer.
20 23
10 23
11 11

Write a program that lets the user perform arithmetic operations on two
numbers. Your program must be menu driven, allowing the user to select the
operation (+, -, *, or /) and input the numbers. Furthermore, your program
must consist of following functions:
1. Function showChoice: This function shows the options to the user and
explains how to enter data.
2. Function add: This function accepts two number as arguments and returns
sum.
3. Function subtract: This function accepts two number as arguments and
returns their difference.
4. Function mulitiply: This function accepts two number as arguments and
returns product.
5. Function divide: This function accepts two number as arguments and
returns quotient.

ANSI C++
Output

#include <iostream>
using namespace std;
void showChoices();float add(float, float);float
subtract(float, float);float multiply(float, float);float
divide(float, float);
int main()
{ float x, y; int choice; do {
showChoices();
cin >> choice; switch (choice)
{ case 1:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Sum " << add(x,y) <<endl; break;
case 2:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Difference " << subtract(x,y) <<endl;
break; case 3:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Product " << multiply(x,y) <<endl;
break; case 4:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Quotient " << divide(x,y) <<endl;
break; case 5: break; default:
cout << "Invalid input" << endl;
}
}while (choice != 5);
return 0;
}
void showChoices()
{
cout << "MENU" << endl;
cout << "1: Add " << endl;
cout << "2: Subtract" << endl;
cout << "3: Multiply " << endl;
cout << "4: Divide " << endl;
cout << "5: Exit " << endl;
cout << "Enter your choice :";
}
float add(float a, float b)
{ return a + b;
}
float subtract(float a, float b)
{ return a - b;
}
float multiply(float a, float b)
{ return a * b;
}
float divide(float a, float b)
{ return a / b;
}
Write a C++ program to find the sum and average of one dimensional integer
array.

ANSI C++
Output
#include<iostream>
using namespace std;
intmain()
{ int Arr[100],n,i,sum=0;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{

cout<<"Enter element "<<i+1<<":";


cin>>Arr[i];
}
for(i=0;i<n;i++)
sum+=Arr[i];

cout<<"\nThe sum of Array is :"<<sum;


cout<<"\nThe average of Array is :"<<sum/i;
return 0;
}
Write a C++ program to swap first and last element of an integer 1-d array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,i,temp;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{

cout<<"Enter element "<<i+1<<":";


cin>>Arr[i];
}

temp=Arr[0];
Arr[0]=Arr[n-1];

Arr[n-1]=temp;

cout<<"\nArray after swapping"<<endl;


for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}
Write a C++ program to swap first and last element of an integer 1-d array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,i,temp;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{

cout<<"Enter element "<<i+1<<":";


cin>>Arr[i];
}

temp=Arr[0];
Arr[0]=Arr[n-1];

Arr[n-1]=temp;

cout<<"\nArray after swapping"<<endl;


for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}
Write a C++ program to find the largest and smallest element of an array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,i,small,large;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}

small=Arr[0];
large=Arr[0];
for(i=1;i<n;i++)
{ if(Arr[i]<small)
small=Arr[i]; if(Arr[i]>large)
large=Arr[i];
}

cout<<"\nLargest element is :"<<large;


cout<<"\nSmallest element is :"<<small;
return 0;
}
Write a C++ program to reverse the element of an integer 1-D array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,temp,i,j;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}
for(i=0,j=n-1;i<n/2;i++,j--)
{
temp=Arr[i];
Arr[i]=Arr[j];
Arr[j]=temp;
}

cout<<"\nReverse array"<<endl;
for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}
Write a menu driven C++ program with following option
a. Accept elements of an array
b. Display elements of an array
c. Sort the array using insertion sort method
d. Sort the array using selection sort method
e. Sort the array using bubble sort method
Write C++ functions for all options. The functions should have two
parameters name of the array and number of elements in the array.

ANSI C++
Output
#include<iostream>
using namespace std;
void accept(int Arr[], int s);void display(int Arr[], int
s);void isort(int Arr[], int s);void ssort(int Arr[], int
s);void bsort(int Arr[],int s);
int main()
{ int Arr[100],n,choice;
cout<<"Enter Size of Array ";
cin>>n; do {
cout<<"\n\nMENU";
cout<<"\n1. Accept elements of array";
cout<<"\n2. Display elements of array";
cout<<"\n3. Sort the array using insertion sort
method";
cout<<"\n4. Sort the array using selection sort
method";
cout<<"\n5. Sort the array using bubble sort method";
cout<<"\n6. Exit";
cout<<"\n\nEnter your choice 1-6 :";
cin>>choice;
switch(choice)
{ case 1: accept(Arr,n);
break; case 2: display(Arr,n);
break; case 3: isort(Arr,n); break;
case 4: ssort(Arr,n); break; case 5:
bsort(Arr,n); break; case 6: break;
default:cout<<"\nInvalid choice";
}
}while(choice!=6);
return 0;
}
void accept(int Arr[], int s)
{ for(int I=0;I<s;I++)
{
cout<<"Enter element "<<I+1<<":";
cin>>Arr[I];
}
}
void display(int Arr[], int s)
{
cout<<"The elements of the array are:\n"; for(int
I=0;I<s;I++)
cout<<Arr[I]<<" ";
}
void isort(int Arr[], int s)
{ int I,J,Temp; for(I=1;I<s;I++)
{
Temp=Arr[I];
J=I-1; while((Temp<Arr[J]) && (J>=0))
{
Arr[J+1]=Arr[J];
J--;
}
Arr[J+1]=Temp;
}
}
void ssort(int Arr[], int s)
{ int I,J,Temp,Small; for(I=0;I<s-1;I++)
{
Small=I; for(J=I+1;J<s;J++) //finding the
smallest element
if(Arr[J]<Arr[Small])
Small=J; if(Small!=I)
{
Temp=Arr[I]; //Swapping
Arr[I]=Arr[Small];
Arr[Small]=Temp;
}
}
}
void bsort(int Arr[],int s)
{ int I,J,Temp; for(I=0;I<s-1;I++)
{ for(J=0;J<(s-1-I);J++)
if(Arr[J]>Arr[J+1])
{
Temp=Arr[J]; //swapping
Arr[J]=Arr[J+1];
Arr[J+1]=Temp;
}
}
}
P is one-dimensional array of integers. Write a C++ function to efficiently
search for a data VAL from P. If VAL is present in the array then the function
should return value 1 and 0 otherwise.

ANSI C++
Output
#include<iostream>
using namespace std;
bool lsearch(int Arr[], int s, int VAL);
int main()
{ int Arr[100],n,val; bool found;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}

cout<<"Enter the number you want to search ";


cin>>val;

found=lsearch(Arr,n,val);
if(found)
cout<<"\nItem found"; else
cout<<"\nItem not found";

return 0;
}
bool lsearch(int Arr[], int s, int VAL)
{ for(int I=0; I<s; I++)
{ if(Arr[I]==VAL) return true;
} return false;
}
Suppose a one-dimensional array AR containing integers is arranged in
ascending order. Write a user-defined function in C++ to search for an
integer from AR with the help of Binary search method, returning an integer
0 to show absence of the number and integer 1 to show presence of the
number in the array. Function should have three parameters : (i) array AR (ii)
the number to be searched and (iii) the number of elements N in the array.

ANSI C++
Output
#include<iostream>
using namespace std;
bool bsearch(int AR[], int N, int VAL);
int main()
{ int AR[100],n,val; bool found;

cout<<"Enter number of elements you want to insert ";


cin>>n;

cout<<"Enter element in ascending order\n";


for(int i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>AR[i];
}

cout<<"\nEnter the number you want to search ";


cin>>val;

found=bsearch(AR,n,val);
if(found)
cout<<"\nItem found"; else
cout<<"\nItem not found";

return 0;
}
bool bsearch(int AR[], int N, int VAL)
{ int Mid,Lbound=0,Ubound=N-1; while(Lbound<=Ubound)
{
Mid=(Lbound+Ubound)/2; if(VAL>AR[Mid])
Lbound=Mid+1; else if(VAL<AR[Mid])
Ubound=Mid-1; else
return true;
} returnfalse;
}
Suppose A, B, C are arrays of integers of size M, N, and M + N respectively.
The numbers in array A appear in ascending order while the numbers in array
B appear in descending order. Write a user defined function in C++ to
produce third array C by merging arrays A and B in ascending order. Use A,
B and C as arguments in the function.

ANSI C++
Output
#include<iostream>
using namespace std;
void Merge(int A[], int B[],int C[], int N,int M, int &K);

int main()
{ int A[100], B[100], C[200],i,n,m,k;

cout<<"\nEnter number of elements you want to insert in


first array ";
cin>>n;

cout<<"Enter element in ascending order\n";


for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>A[i];
}

cout<<"\nEnter number of elements you want to insert in


second array ";
cin>>m;

cout<<"Enter element in descending order\n";


for(i=0;i<m;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>B[i];
}

Merge(A,B,C,n,m,k);

cout<<"\nThe Merged Array in Ascending Order"<<endl;


for(i=0;i<k;i++)
{
cout<<C[i]<<" ";
}
return 0;
}

void Merge(int A[], int B[], int C[], int N, int M, int &K)
{ int I=0, J=M-1;
K=0;
while (I<N && J>=0)
{ if (A[I]<B[J])
C[K++]=A[I++]; else if (A[I]>B[J])
C[K++]=B[J--]; else {
C[K++]=A[I++];
J--;
}
}
for (int T=I;T<N;T++)
C[K++]=A[T]; for (int T=J;T>=0;T--)
C[K++]=B[T];
}
uppose X. Y, Z are arrays of integers of size M, N, and M + N respectively. The
numbers in array X and Y appear in descending order. Write a user-defined
function in C++ to produce third array Z by merging arrays X and Y in
descending order.

ANSI C++
Output
#include<iostream>
using namespace std;
void Merge(int A[], int B[], int C[], int N, int M, int &K);

int main()
{ int A[100], B[100], C[200],i,n,m,k;
cout<<"\nEnter number of elements you want to insert in
first array ";
cin>>n;

cout<<"Enter element in descending order\n";


for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>A[i];
}

cout<<"\nEnter number of elements you want to insert in


second array ";
cin>>m;

cout<<"Enter element in descending order\n";


for(i=0;i<m;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>B[i];
}

Merge(A,B,C,n,m,k);

cout<<"\nThe Merged Array in Descending Order"<<endl;


for(i=0;i<k;i++)
{
cout<<C[i]<<" ";
}
return 0;
}
void Merge(int A[], int B[], int C[], int N, int M, int &K)
{ int I=0, J=0;
K=0;
while (I<N && J<M)
{ if (A[I]>B[J])
C[K++]=A[I++]; else if (A[I]<B[J])
C[K++]=B[J++]; else {
C[K++]=A[I++];
J++;
}
}
for (int T=I;T<N;T++)
C[K++]=A[T]; for (int T=J;T<M;T++)
C[K++]=B[T];
}
Write a program to find the length of string.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout<<"Enter string: ";


cin.getline(str, 80);
int i; //Hold length of string

for(i = 0; str[i] != '\0'; i++);

cout << "Length of string is: " << i;


return 0;
}
Assignments » String » Set1 »Solution 2

Write a program to display string from backward.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout<<"Enter string: ";


cin.getline(str, 80);
int l; //Hold length of string

//Find the length of the string


for(l = 0; str[l] != '\0'; l++);
//Display the string backwards
for(int i = l -1; i >= 0; i--)
{
cout << str[i];
}

return 0;
}
Write a program to count number of words in string.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout << "Enter a string: ";


cin.getline(str,80);
int words = 0; // Holds number of words

for(int i = 0; str[i] != '\0'; i++)


{ if (str[i] == ' ') //Checking for spaces
{
words++;
}
}

cout << "The number of words = " << words+1 << endl;
return 0;
}
Write a program to concatenate one string contents to another.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str1[80], str2[80];

cout<<"Enter first string: ";


cin.getline(str1, 80);

cout<<"Enter second string: ";


cin.getline(str2, 80);
int l = 0; //Hold length of first string

//Find length of first string.


for(l = 0; str1[l] != '\0'; l++);
//Adding second string content in first
for(int i = 0; str2[i] != '\0'; i++)
{
str1[l++] = str2[i];
}

str1[l] ='\0';
cout << "\nThe first string after adding second string
content:\n\n" << str1;

return 0;
}
Write a C++ program to compare two strings they are exact equal or not.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str1[80], str2[80];

cout<<"Enter first string: ";


gets(str1);

cout<<"Enter second string: ";


gets(str2);
int i; for (i = 0; str1[i] == str2[i] && str1[i]!= '\0'
&& str2[i] != '\0'; i++);
if(str1[i] - str2[i] == 0)
cout << "Strings are equal"; else
cout << "Strings are not equal";
return 0;
}
rite a program to check a string is palindrome or not.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout<<"Enter string: ";


cin.getline(str, 80);
int l; //Hold length of string

//finding length of string


for(l = 0; str[l] !='\0'; l++);
//Comparing first element with last element till middle
of string
int i; for(i = 0; (i < l/2) && (str[i] == str[l - i -
1]); i++);
if(i == l/2)
cout << "Palindrome"; else
cout << "Not a palindrome";
return 0;
}

Write the output of the following program. Assume that all necessary header
files are included.
void Encrypt(char T[])
{
for (int i = 0; T[i] != '\0'; i += 2)
if (T[i] == 'A' || T[i] == 'E')
T[i] = '#';
else if (islower(T[i]))
T[i] = toupper(T[i]);
else
T[i] = '@';
}

int main()
{
char text[]="SaVE EArtH";
Encrypt(text);
cout << text << endl;

return 0;
}

Show the answer.


@a@E@E#rTH

12 Write the output of the following program


int main()
{
char name[] = "CoMPutER";
for (int x = 0; x < strlen(name);
x++)
if (islower(name [x]))
name [x] =
toupper(name[x]);
else
if (isupper(name[x]))
if (x % 2 == 0)
name[x] =
tolower(name[x]);
else
name[x] =
name[x-1];

cout << name;

return 0;
}

Show the answer.


cOmmUTee

Two numbers are entered through the keyboard. Write a program to find the
value of one number raised to the power of another.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,p,r=1;
cout<<"Enter the base number and exponent ";
cin>>n>>p;
for(int i=1;i<=p;i++)
r=r*n;
cout<<"Result :"<<r;

return 0;
}

Write a program to reverse any given integer number.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,t,r,rev=0;
cout<<"Enter any number : ";
cin>>n;
t=n;
while(t>0)
{
r=t%10;
t=t/10;
rev=rev*10+r;
}

cout<<"Reverse of number "<<n<<" is "<<rev;

return 0;
}
Write a program to sum of digits of given integer number.

Source Code
Output
#include<iostream>
using namespace std;
int main()
{
int n,t,r,sum=0;
cout<<"Enter any number : ";
cin>>n;
t=n;
while(t>0)
{
r=t%10;
sum+=r;
t=t/10;
}

cout<<"Sum of digits of number "<<n<<" is "<<sum;

return 0;
rite a program to check given number is prime or not.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n;
bool flag=false;
cout<<"Enter any number : ";
cin>>n;
for(int i=2;i<n;i++)
{ if(n%i==0)
{
flag=true; break;
}
}
if(flag==false && n>1)
cout<<"Number is prime"; else
cout<<"Number is not prime";

return 0;
}
Write a program to calculate HCF of Two given number.

Source Code
Output

#include<iostream>
using namespace std;
intmain()
{
int dividend, divisor, rem, hcf;
cout<<"Enter two numbers : ";
cin>>dividend>>divisor;
while(rem!=0)
{
rem=dividend%divisor; if(rem==0)
hcf=divisor; else {
dividend=divisor;
divisor=rem;
}
}
cout<<"HCF is : "<<hcf;

return 0;
}

Write a program to enter the numbers till the user wants and at the end it
should display the count of positive, negative and zeros entered.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n, sum_p=0, sum_n=0, sum_z=0; char choice;
do {
cout<<"Enter number ";
cin>>n;
if(n>0)
sum_p++; else if(n<0)
sum_n++; else
sum_z++;

cout<<"Do you want to Continue(y/n)? ";


cin>>choice;

}while(choice=='y'|| choice=='Y');

cout<<"Positive Number :"<<sum_p<<"\nNegative


Number :"<<sum_n<<"\nZero Number :"<<sum_z;
return 0;
}

Write a program to enter the numbers till the user wants and at the end it
should display the maximum and minimum number entered.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n, max=0, min=32767; char choice;
do {
cout<<"Enter number : ";
cin>>n;
if(n>max)
max=n; if(n<min)
min=n;

cout<<"Do you want to Continue(y/n)? ";


cin>>choice;

}while(choice=='y' || choice=='Y');

cout<<"Maximum Number :"<<max<<"\nMinimum


Number :"<<min;
return 0;
}
Write a program to print all Armstrong numbers between 1 and 500. If sum
of cubes of each digit of the number is equal to the number itself, then the
number is called an Armstrong number.
For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,digit1,digit2,digit3;
for(int i=1;i<=500;i++)
{
digit1=i/100;
digit2=i/10 - digit1*10;
digit3=i%10;
if(digit1*digit1*digit1 + digit2*digit2*digit2 +
digit3*digit3*digit3 == i)
cout<<i<<endl;
}

return 0;
}

Write a program to print Fibonacci series of n terms where n is input by user


0 1 1 2 3 5 8 13 24 .....

Source Code
Output
#include<iostream>
using namespace std;

int main()
{
int f=0,s=1,t,n;

cout<<"Enter Number of terms of Series : ";


cin>>n;

cout<<f<<" "<<s<<" ";


for(int i=3;i<=n;i++)
{
t=f+s;
cout<<t<<" ";
f=s;
s=t;
}

return 0;
}
Write a program to calculate the sum of following series where n is input by
user.
1 + 1/2 + 1/3 + 1/4 + 1/5 +…………1/n

Source Code
Output

#include<iostream>
using namespace std;
int main()
{ int i,n; float sum=0;
cout<<"Enter the value of n ";
cin>>n;
for(i=1;i<=n;i++)
sum += 1.0/i;

cout<<"Sum : "<<sum;

return 0;
}
Compute the natural logarithm of 2, by adding up to n terms in the series
1 - 1/2 + 1/3 - 1/4 + 1/5 -... 1/n
where n is a positive integer and input by user.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{ int i,n,sign=-1; float sum=0;
cout<<"Enter the value of n ";
cin>>n;
for(i=1;i<=n;i++)
{
sign *= -1;
sum += sign*1.0/i;
}
cout<<"log 2 : "<<sum;

return 0;
}
Write C++ program to print following pattern:
i) ii) iii)
********** * *
********** ** **
********** *** ***
********** **** ****
***** *****
iv) v) 1 vi) 1
* 222 212
*** 33333 32123
4444444 4321234
***** 555555555 543212345
*******
*********
Source Code

//Solution of (i)
#include<iostream>
using namespace std;
int main()
{ int i,j; for(i=1;i<=4;i++)
{ for(j=1;j<=10;j++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (ii)
#include<iostream>
using namespace std;
int main()
{ int i,j; for(i=1;i<=5;i++)
{ for(j=1;j<=i;j++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (iii)
#include<iostream>
using namespace std;
int main()
{ int i,j,k; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=1;k<=i;k++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (iv)
#include<iostream>
using namespace std;
int main()
{ int i,j,k; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=1;k<2*i;k++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (v)
#include<iostream>
using namespace std;
intmain()
{ int i,j,k; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=1;k<2*i;k++)
cout<<i;
cout<<endl;
}

return0;
}

//Solution of (vi)
#include<iostream>
using namespace std;
intmain()
{ int i,j,k,l; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=i;k>=1;k--)
cout<<k; for(l=2;l<=i;l++)
cout<<l;
cout<<endl;
}

return0;
}
Write a program to compute sinx for given x. The user should supply x and a
positive integer n. We compute the sine of x using the series and the
computation should use all terms in the series up through the term involving
x
sin x = x - x3/3! + x5/5! - x7/7!+x9/9! .........

Source Code
Output

#include<iostream>
using namespace std;
int main()
{ int i,j,n,fact,sign=-1; float x, p=1,sum=0;
cout<<"Enter the value of x : ";
cin>>x;
cout<<"Enter the value of n : ";
cin>>n;
for(i=1;i<=n;i+=2)
{
fact=1; for(j=1;j<=i;j++)
{
p=p*x;
fact=fact*j;
}
sign=-1*sign;
sum+=sign*p/fact;
}
cout<<"sin "<<x<<"="<<sum;

return 0;
}
Write a program to compute the cosine of x. The user should supply x and a
positive integer n. We compute the cosine of x using the series and the
computation should use all terms in the series up through the term involving
xn
cos x = 1 - x2/2! + x4/4! - x6/6! -.....

Source Code
Output

#include<iostream>

using namespace std;


int main()

{
int i,j,n,fact,sign=-1;
float x, p=1,sum=0;

cout<<"Enter the value of x : ";

cin>>x;

cout<<"Enter the value of n : ";

cin>>n;

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

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

p=p*x;

fact=fact*j;

sum+=sign*p/fact;

sign=-1*sign;
}

cout<<"cos "<<x<<"="<<1+sum;

return 0;

}
Write a program which input principal, rate and time from user and calculate
compound interest.
CI = P(1+R/100)T - P

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cmath>
using namespace std;
int main()
{ float P,R,T,CI;
cout<<"Enter Principal, Rate and Time : ";
cin>>P>>R>>T;
CI=P*pow((1+R/100),T) - P;
cout<<"Compound Interest is : "<<CI;
return 0;
}
Write a program to compute area of triangle. Sides are input by user.
Area = sqrt(s*(s-a)*(s-b)*(s-c))
where s=(a+b+c)/2

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cmath>
using namespace std;
int main()
{ float a,b,c,s,Area;
cout<<"Enter three sides of triangle : ";
cin>>a>>b>>c;
s=(a+b+c)/2;
Area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"Area of triangle is : "<<Area;

return 0;
}
Write a program to check character entered is alphabet, digit or special
character.

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cctype>
using namespace std;
int main()
{ char ch;
cout<<"Enter any character :";
ch=getchar(); if(isalpha(ch))
cout<<"Alphabet"; else if(isdigit(ch))
cout<<"Number"; else
cout<<"Special Character";
return 0;
}
Write a program which display a number between 10 to 100 randomly.

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cstdlib>
#include <ctime>
using namespace std;
int main()
{ int n;
srand(time(0));
n = rand() % 91 + 10;
cout<<"The randomly selected number is :"<<n;
return 0;
}
Write a program which accept a letter and display it in uppercase letter.

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cctype>
using namespace std;
int main()
{ char ch;
cout<<"Enter any character :";
ch=getchar();
ch=toupper(ch);
cout<<ch;

return 0;
}
Write a C++ program to implement the Number Guessing Game. In this
game the computer chooses a random number between 1 and 100, and the
player tries to guess the number in as few attempts as possible. Each time
the player enters a guess, the computer tells him whether the guess is too
high, too low, or right. Once the player guesses the number, the game is
over.

ANSI C++
Pre-Standard C++
Output
#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main()

{
int num, guess, tries = 0;

srand(time(0)); //seed random number generator

num = rand() % 100 + 1; // random number between 1 and 100

cout << "Guess My Number Game\n\n";


do
{

cout << "Enter a guess between 1 and 100 : ";

cin >> guess;

tries++;
if (guess > num)

cout << "Too high!\n\n";


else if (guess < num)

cout << "Too low!\n\n";


else
cout << "\nCorrect! You got it in " << tries << "
guesses!\n";

} while (guess != num);

cin.ignore();

cin.get();
return 0;

}
Write a C++ program using function which accept two integers as an
argument and return its sum. Call this function from main( ) and print the
results in main( ).

ANSI C++
Output
#include<iostream>
using namespace std;
int sum(int, int);
int main()
{ int x,y,s;
cout<<"Enter first number : ";
cin>>x;
cout<<"Enter second number : ";
cin>>y;
s=sum(x,y);
cout<<"The sum is : "<<s;

return 0;
}
int sum(int a, int b)
{
int total;
total=a+b; return total;
}
Write a function to calculate the factorial value of any integer as an argument.
Call this function from main( ) and print the results in main( ).

ANSI C++
Output

#include<iostream>
using namespace std;
int factorial(int);
int main()
{ int x,f;
cout<<"Enter number : ";
cin>>x;
f=factorial(x);
cout<<"The factorial is :"<<f;
return 0;
}
int factorial(int a)
{ int fact=1;
while(a>=1)
{
fact=fact*a;
a--;
}
return fact;
}
Write a function that receives two numbers as an argument and display all
prime numbers between these two numbers. Call this function from main( ).

ANSI C++
Output
#include<iostream>
using namespace std;
void showprime(int, int);
int main()
{
int x,y;
cout<<"Enter first number : ";
cin>>x;

cout<<"Enter second number : ";


cin>>y;
showprime(x,y);

return 0;
}
void showprime(int a, int b)
{ bool flag;
for(int i=a+1;i<=b;i++)
{

flag=false; for(int j=2;j<i;j++)


{
if(i%j==0)
{
flag=true; break;
}
}
if(flag==false && i>1)
cout<<i<<endl;
}
}
Raising a number to a power p is the same as multiplying n by itself p times.
Write a function called power that takes two arguments, a double value for n
and an int value for p, and return the result as double value. Use default
argument of 2 for p, so that if this argument is omitted the number will be
squared. Write the main function that gets value from the user to test power
function.
ANSI C++
Output

#include<iostream>
using namespace std;
double power(double,int=2);
int main()
{ int p; double n,r;
cout << "Enter number : ";
cin >> n;
cout << "Enter exponent : ";
cin >> p;
r = power(n,p);
cout << "Result is " << r;
r = power(n);
cout << "\nResult without passing exponent is " << r;
return 0;
}
double power(double a, int b)
{ double x = 1; for(int i = 1; i <= b; i++)
x = x * a; return x;
}
Write the output of the following program :
#include <iostream>
using namespace std;

void X(int &A, int &B)


{
A = A + B;
B = A - B;
A = A - B;
}

int main()
{
int a = 4, b = 18;
X(a,b);
cout << a << ", " << b;

return 0;
}

Show the answer.


18, 4

7 Write the output of the following program:

#include <iostream>
using namespace std;

void X(int A, int &B)


{
A = A + B;
B = A - B;
A = A - B;
}

int main()
{
int a = 4, b = 18;
X(a,b);
cout << a << ", " << b;

return 0;
}

Show the answer.


4, 4

8 Write the output of the following program:


#include <iostream>
using namespace std;

void Execute(int &B, int C = 100)


{
int temp = B + C;
B += temp;
if (C == 100)
cout << temp << " " << B << " " << C << endl;
}

int main()
{
int M = 90, N = 10;
Execute(M);
cout << M << " " << N << endl;
Execute(M, N);
cout << M << " " << N << endl;

return 0;
}

Show the answer.


190 280 100
280 10
570 10

9 Give the output of the following program


#include <iostream>
using namespace std;

int global = 10;

void func(int &x, int y)


{
x = x - y;
y = x * 10;
cout << x << ", " << y << '\n';
}

int main()
{
int global = 7;
func (::global, global);
cout << global << ", " << ::global << '\n';

func(global, ::global);
cout << global << ", " << ::global << '\n';

return 0;
}

Show the answer.


3, 30
7, 3
4, 40
4, 3
10 Write the output of the following program :
#include <iostream>
using namespace std;

static int i = 100;


void abc()
{
static int i = 8;
cout << "first = " << i++ << endl;
}

int main()
{
static int i = 2;
abc();
cout << "second = " << i << endl;
abc();

return 0;
}

Show the answer.


first = 8
second = 2
first = 9

11 Write the output of the following program:


#include <iostream>
using namespace std;

int func(int &x, int y = 10)


{
if (x % y == 0)
return ++x;
else
return y--;
}

int main()
{
int p = 20, q = 23;

q = func(p, q);
cout << p << " " << " " << q << endl;

p = func (q);
cout << p << " " << " " << q << endl;
q = func (p);
cout << p << " " << " " << q << endl;

return 0;
}

Show the answer.


20 23
10 23
11 11

Write a program that lets the user perform arithmetic operations on two
numbers. Your program must be menu driven, allowing the user to select the
operation (+, -, *, or /) and input the numbers. Furthermore, your program
must consist of following functions:
1. Function showChoice: This function shows the options to the user and
explains how to enter data.
2. Function add: This function accepts two number as arguments and returns
sum.
3. Function subtract: This function accepts two number as arguments and
returns their difference.
4. Function mulitiply: This function accepts two number as arguments and
returns product.
5. Function divide: This function accepts two number as arguments and
returns quotient.

ANSI C++
Output

#include <iostream>
using namespace std;
void showChoices();float add(float, float);float
subtract(float, float);float multiply(float, float);float
divide(float, float);
int main()
{ float x, y; int choice; do {
showChoices();
cin >> choice; switch (choice)
{ case 1:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Sum " << add(x,y) <<endl; break;
case 2:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Difference " << subtract(x,y) <<endl;
break; case 3:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Product " << multiply(x,y) <<endl;
break; case 4:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Quotient " << divide(x,y) <<endl;
break; case 5: break; default:
cout << "Invalid input" << endl;
}
}while (choice != 5);
return 0;
}
void showChoices()
{
cout << "MENU" << endl;
cout << "1: Add " << endl;
cout << "2: Subtract" << endl;
cout << "3: Multiply " << endl;
cout << "4: Divide " << endl;
cout << "5: Exit " << endl;
cout << "Enter your choice :";
}
float add(float a, float b)
{ return a + b;
}
float subtract(float a, float b)
{ return a - b;
}
float multiply(float a, float b)
{ return a * b;
}
float divide(float a, float b)
{ return a / b;
}
Write a C++ program to find the sum and average of one dimensional integer
array.

ANSI C++
Output
#include<iostream>
using namespace std;
intmain()
{ int Arr[100],n,i,sum=0;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{

cout<<"Enter element "<<i+1<<":";


cin>>Arr[i];
}
for(i=0;i<n;i++)
sum+=Arr[i];

cout<<"\nThe sum of Array is :"<<sum;


cout<<"\nThe average of Array is :"<<sum/i;
return 0;
}
Write a C++ program to swap first and last element of an integer 1-d array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,i,temp;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{

cout<<"Enter element "<<i+1<<":";


cin>>Arr[i];
}

temp=Arr[0];
Arr[0]=Arr[n-1];

Arr[n-1]=temp;

cout<<"\nArray after swapping"<<endl;


for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}
Write a C++ program to swap first and last element of an integer 1-d array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,i,temp;
cout<<"Enter number of elements you want to insert ";
cin>>n;
for(i=0;i<n;i++)
{

cout<<"Enter element "<<i+1<<":";


cin>>Arr[i];
}

temp=Arr[0];
Arr[0]=Arr[n-1];

Arr[n-1]=temp;

cout<<"\nArray after swapping"<<endl;


for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}
Write a C++ program to find the largest and smallest element of an array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,i,small,large;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}

small=Arr[0];
large=Arr[0];
for(i=1;i<n;i++)
{ if(Arr[i]<small)
small=Arr[i]; if(Arr[i]>large)
large=Arr[i];
}

cout<<"\nLargest element is :"<<large;


cout<<"\nSmallest element is :"<<small;
return 0;
}
Write a C++ program to reverse the element of an integer 1-D array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,temp,i,j;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}
for(i=0,j=n-1;i<n/2;i++,j--)
{
temp=Arr[i];
Arr[i]=Arr[j];
Arr[j]=temp;
}

cout<<"\nReverse array"<<endl;
for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}
Write a menu driven C++ program with following option
a. Accept elements of an array
b. Display elements of an array
c. Sort the array using insertion sort method
d. Sort the array using selection sort method
e. Sort the array using bubble sort method
Write C++ functions for all options. The functions should have two
parameters name of the array and number of elements in the array.

ANSI C++
Output
#include<iostream>
using namespace std;
void accept(int Arr[], int s);void display(int Arr[], int
s);void isort(int Arr[], int s);void ssort(int Arr[], int
s);void bsort(int Arr[],int s);
int main()
{ int Arr[100],n,choice;
cout<<"Enter Size of Array ";
cin>>n; do {
cout<<"\n\nMENU";
cout<<"\n1. Accept elements of array";
cout<<"\n2. Display elements of array";
cout<<"\n3. Sort the array using insertion sort
method";
cout<<"\n4. Sort the array using selection sort
method";
cout<<"\n5. Sort the array using bubble sort method";
cout<<"\n6. Exit";
cout<<"\n\nEnter your choice 1-6 :";
cin>>choice;
switch(choice)
{ case 1: accept(Arr,n);
break; case 2: display(Arr,n);
break; case 3: isort(Arr,n); break;
case 4: ssort(Arr,n); break; case 5:
bsort(Arr,n); break; case 6: break;
default:cout<<"\nInvalid choice";
}
}while(choice!=6);
return 0;
}
void accept(int Arr[], int s)
{ for(int I=0;I<s;I++)
{
cout<<"Enter element "<<I+1<<":";
cin>>Arr[I];
}
}
void display(int Arr[], int s)
{
cout<<"The elements of the array are:\n"; for(int
I=0;I<s;I++)
cout<<Arr[I]<<" ";
}
void isort(int Arr[], int s)
{ int I,J,Temp; for(I=1;I<s;I++)
{
Temp=Arr[I];
J=I-1; while((Temp<Arr[J]) && (J>=0))
{
Arr[J+1]=Arr[J];
J--;
}
Arr[J+1]=Temp;
}
}
void ssort(int Arr[], int s)
{ int I,J,Temp,Small; for(I=0;I<s-1;I++)
{
Small=I; for(J=I+1;J<s;J++) //finding the
smallest element
if(Arr[J]<Arr[Small])
Small=J; if(Small!=I)
{
Temp=Arr[I]; //Swapping
Arr[I]=Arr[Small];
Arr[Small]=Temp;
}
}
}
void bsort(int Arr[],int s)
{ int I,J,Temp; for(I=0;I<s-1;I++)
{ for(J=0;J<(s-1-I);J++)
if(Arr[J]>Arr[J+1])
{
Temp=Arr[J]; //swapping
Arr[J]=Arr[J+1];
Arr[J+1]=Temp;
}
}
}
P is one-dimensional array of integers. Write a C++ function to efficiently
search for a data VAL from P. If VAL is present in the array then the function
should return value 1 and 0 otherwise.

ANSI C++
Output
#include<iostream>
using namespace std;
bool lsearch(int Arr[], int s, int VAL);
int main()
{ int Arr[100],n,val; bool found;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}

cout<<"Enter the number you want to search ";


cin>>val;

found=lsearch(Arr,n,val);
if(found)
cout<<"\nItem found"; else
cout<<"\nItem not found";

return 0;
}
bool lsearch(int Arr[], int s, int VAL)
{ for(int I=0; I<s; I++)
{ if(Arr[I]==VAL) return true;
} return false;
}
Suppose a one-dimensional array AR containing integers is arranged in
ascending order. Write a user-defined function in C++ to search for an
integer from AR with the help of Binary search method, returning an integer
0 to show absence of the number and integer 1 to show presence of the
number in the array. Function should have three parameters : (i) array AR (ii)
the number to be searched and (iii) the number of elements N in the array.

ANSI C++
Output
#include<iostream>
using namespace std;
bool bsearch(int AR[], int N, int VAL);
int main()
{ int AR[100],n,val; bool found;

cout<<"Enter number of elements you want to insert ";


cin>>n;

cout<<"Enter element in ascending order\n";


for(int i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>AR[i];
}

cout<<"\nEnter the number you want to search ";


cin>>val;

found=bsearch(AR,n,val);
if(found)
cout<<"\nItem found"; else
cout<<"\nItem not found";

return 0;
}
bool bsearch(int AR[], int N, int VAL)
{ int Mid,Lbound=0,Ubound=N-1; while(Lbound<=Ubound)
{
Mid=(Lbound+Ubound)/2; if(VAL>AR[Mid])
Lbound=Mid+1; else if(VAL<AR[Mid])
Ubound=Mid-1; else
return true;
} returnfalse;
}
Suppose A, B, C are arrays of integers of size M, N, and M + N respectively.
The numbers in array A appear in ascending order while the numbers in array
B appear in descending order. Write a user defined function in C++ to
produce third array C by merging arrays A and B in ascending order. Use A,
B and C as arguments in the function.

ANSI C++
Output
#include<iostream>
using namespace std;
void Merge(int A[], int B[],int C[], int N,int M, int &K);

int main()
{ int A[100], B[100], C[200],i,n,m,k;

cout<<"\nEnter number of elements you want to insert in


first array ";
cin>>n;

cout<<"Enter element in ascending order\n";


for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>A[i];
}

cout<<"\nEnter number of elements you want to insert in


second array ";
cin>>m;

cout<<"Enter element in descending order\n";


for(i=0;i<m;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>B[i];
}

Merge(A,B,C,n,m,k);

cout<<"\nThe Merged Array in Ascending Order"<<endl;


for(i=0;i<k;i++)
{
cout<<C[i]<<" ";
}
return 0;
}

void Merge(int A[], int B[], int C[], int N, int M, int &K)
{ int I=0, J=M-1;
K=0;
while (I<N && J>=0)
{ if (A[I]<B[J])
C[K++]=A[I++]; else if (A[I]>B[J])
C[K++]=B[J--]; else {
C[K++]=A[I++];
J--;
}
}
for (int T=I;T<N;T++)
C[K++]=A[T]; for (int T=J;T>=0;T--)
C[K++]=B[T];
}
uppose X. Y, Z are arrays of integers of size M, N, and M + N respectively. The
numbers in array X and Y appear in descending order. Write a user-defined
function in C++ to produce third array Z by merging arrays X and Y in
descending order.

ANSI C++
Output
#include<iostream>
using namespace std;
void Merge(int A[], int B[], int C[], int N, int M, int &K);

int main()
{ int A[100], B[100], C[200],i,n,m,k;

cout<<"\nEnter number of elements you want to insert in


first array ";
cin>>n;

cout<<"Enter element in descending order\n";


for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>A[i];
}

cout<<"\nEnter number of elements you want to insert in


second array ";
cin>>m;

cout<<"Enter element in descending order\n";


for(i=0;i<m;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>B[i];
}

Merge(A,B,C,n,m,k);

cout<<"\nThe Merged Array in Descending Order"<<endl;


for(i=0;i<k;i++)
{
cout<<C[i]<<" ";
}
return 0;
}

void Merge(int A[], int B[], int C[], int N, int M, int &K)
{ int I=0, J=0;
K=0;
while (I<N && J<M)
{ if (A[I]>B[J])
C[K++]=A[I++]; else if (A[I]<B[J])
C[K++]=B[J++]; else {
C[K++]=A[I++];
J++;
}
}
for (int T=I;T<N;T++)
C[K++]=A[T]; for (int T=J;T<M;T++)
C[K++]=B[T];
}
Write a program to find the length of string.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout<<"Enter string: ";


cin.getline(str, 80);
int i; //Hold length of string
for(i = 0; str[i] != '\0'; i++);

cout << "Length of string is: " << i;


return 0;
}
Assignments » String » Set1 »Solution 2

Write a program to display string from backward.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout<<"Enter string: ";


cin.getline(str, 80);
int l; //Hold length of string

//Find the length of the string


for(l = 0; str[l] != '\0'; l++);
//Display the string backwards
for(int i = l -1; i >= 0; i--)
{
cout << str[i];
}

return 0;
}
Write a program to count number of words in string.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout << "Enter a string: ";


cin.getline(str,80);
int words = 0; // Holds number of words

for(int i = 0; str[i] != '\0'; i++)


{ if (str[i] == ' ') //Checking for spaces
{
words++;
}
}

cout << "The number of words = " << words+1 << endl;
return 0;
}
Write a program to concatenate one string contents to another.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str1[80], str2[80];

cout<<"Enter first string: ";


cin.getline(str1, 80);

cout<<"Enter second string: ";


cin.getline(str2, 80);
int l = 0; //Hold length of first string

//Find length of first string.


for(l = 0; str1[l] != '\0'; l++);
//Adding second string content in first
for(int i = 0; str2[i] != '\0'; i++)
{
str1[l++] = str2[i];
}

str1[l] ='\0';

cout << "\nThe first string after adding second string


content:\n\n" << str1;

return 0;
}
Write a C++ program to compare two strings they are exact equal or not.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str1[80], str2[80];

cout<<"Enter first string: ";


gets(str1);

cout<<"Enter second string: ";


gets(str2);
int i; for (i = 0; str1[i] == str2[i] && str1[i]!= '\0'
&& str2[i] != '\0'; i++);
if(str1[i] - str2[i] == 0)
cout << "Strings are equal"; else
cout << "Strings are not equal";
return 0;
}
rite a program to check a string is palindrome or not.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout<<"Enter string: ";


cin.getline(str, 80);
int l; //Hold length of string

//finding length of string


for(l = 0; str[l] !='\0'; l++);
//Comparing first element with last element till middle
of string
int i; for(i = 0; (i < l/2) && (str[i] == str[l - i -
1]); i++);
if(i == l/2)
cout << "Palindrome"; else
cout << "Not a palindrome";
return 0;
}

Write the output of the following program. Assume that all necessary header
files are included.
void Encrypt(char T[])
{
for (int i = 0; T[i] != '\0'; i += 2)
if (T[i] == 'A' || T[i] == 'E')
T[i] = '#';
else if (islower(T[i]))
T[i] = toupper(T[i]);
else
T[i] = '@';
}

int main()
{
char text[]="SaVE EArtH";
Encrypt(text);
cout << text << endl;
return 0;
}

Show the answer.


@a@E@E#rTH

12 Write the output of the following program


int main()
{
char name[] = "CoMPutER";
for (int x = 0; x < strlen(name);
x++)
if (islower(name [x]))
name [x] =
toupper(name[x]);
else
if (isupper(name[x]))
if (x % 2 == 0)
name[x] =
tolower(name[x]);
else
name[x] =
name[x-1];

cout << name;

return 0;
}

Show the answer.


cOmmUTee

Two numbers are entered through the keyboard. Write a program to find the
value of one number raised to the power of another.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,p,r=1;
cout<<"Enter the base number and exponent ";
cin>>n>>p;
for(int i=1;i<=p;i++)
r=r*n;

cout<<"Result :"<<r;

return 0;
}

Write a program to reverse any given integer number.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,t,r,rev=0;
cout<<"Enter any number : ";
cin>>n;
t=n;
while(t>0)
{
r=t%10;
t=t/10;
rev=rev*10+r;
}

cout<<"Reverse of number "<<n<<" is "<<rev;


return 0;
}
Write a program to sum of digits of given integer number.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,t,r,sum=0;
cout<<"Enter any number : ";
cin>>n;
t=n;
while(t>0)
{
r=t%10;
sum+=r;
t=t/10;
}

cout<<"Sum of digits of number "<<n<<" is "<<sum;

return 0;
rite a program to check given number is prime or not.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n;
bool flag=false;
cout<<"Enter any number : ";
cin>>n;
for(int i=2;i<n;i++)
{ if(n%i==0)
{
flag=true; break;
}
}
if(flag==false && n>1)
cout<<"Number is prime"; else
cout<<"Number is not prime";

return 0;
}
Write a program to calculate HCF of Two given number.

Source Code
Output

#include<iostream>
using namespace std;
intmain()
{
int dividend, divisor, rem, hcf;
cout<<"Enter two numbers : ";
cin>>dividend>>divisor;
while(rem!=0)
{
rem=dividend%divisor; if(rem==0)
hcf=divisor; else {
dividend=divisor;
divisor=rem;
}
}

cout<<"HCF is : "<<hcf;

return 0;
}

Write a program to enter the numbers till the user wants and at the end it
should display the count of positive, negative and zeros entered.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n, sum_p=0, sum_n=0, sum_z=0; char choice;
do {
cout<<"Enter number ";
cin>>n;
if(n>0)
sum_p++; else if(n<0)
sum_n++; else
sum_z++;

cout<<"Do you want to Continue(y/n)? ";


cin>>choice;
}while(choice=='y'|| choice=='Y');

cout<<"Positive Number :"<<sum_p<<"\nNegative


Number :"<<sum_n<<"\nZero Number :"<<sum_z;

return 0;
}

Write a program to enter the numbers till the user wants and at the end it
should display the maximum and minimum number entered.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n, max=0, min=32767; char choice;
do {
cout<<"Enter number : ";
cin>>n;
if(n>max)
max=n; if(n<min)
min=n;

cout<<"Do you want to Continue(y/n)? ";


cin>>choice;
}while(choice=='y' || choice=='Y');

cout<<"Maximum Number :"<<max<<"\nMinimum


Number :"<<min;

return 0;
}
Write a program to print all Armstrong numbers between 1 and 500. If sum
of cubes of each digit of the number is equal to the number itself, then the
number is called an Armstrong number.
For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,digit1,digit2,digit3;
for(int i=1;i<=500;i++)
{
digit1=i/100;
digit2=i/10 - digit1*10;
digit3=i%10;
if(digit1*digit1*digit1 + digit2*digit2*digit2 +
digit3*digit3*digit3 == i)
cout<<i<<endl;
}

return 0;
}

Write a program to print Fibonacci series of n terms where n is input by user


0 1 1 2 3 5 8 13 24 .....

Source Code
Output

#include<iostream>
using namespace std;

int main()
{
int f=0,s=1,t,n;

cout<<"Enter Number of terms of Series : ";


cin>>n;

cout<<f<<" "<<s<<" ";


for(int i=3;i<=n;i++)
{
t=f+s;
cout<<t<<" ";
f=s;
s=t;
}

return 0;
}
Write a program to calculate the sum of following series where n is input by
user.
1 + 1/2 + 1/3 + 1/4 + 1/5 +…………1/n
Source Code
Output

#include<iostream>
using namespace std;
int main()
{ int i,n; float sum=0;

cout<<"Enter the value of n ";


cin>>n;
for(i=1;i<=n;i++)
sum += 1.0/i;

cout<<"Sum : "<<sum;

return 0;
}
Compute the natural logarithm of 2, by adding up to n terms in the series
1 - 1/2 + 1/3 - 1/4 + 1/5 -... 1/n
where n is a positive integer and input by user.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{ int i,n,sign=-1; float sum=0;
cout<<"Enter the value of n ";
cin>>n;
for(i=1;i<=n;i++)
{
sign *= -1;
sum += sign*1.0/i;
}
cout<<"log 2 : "<<sum;

return 0;
}
Write C++ program to print following pattern:
i) ii) iii)
********** * *
********** ** **
********** *** ***
********** **** ****
***** *****
iv) v) 1 vi) 1
* 222 212
*** 33333 32123
4444444 4321234
***** 555555555 543212345
*******
*********
Source Code

//Solution of (i)
#include<iostream>
using namespace std;
int main()
{ int i,j; for(i=1;i<=4;i++)
{ for(j=1;j<=10;j++)
cout<<'*';
cout<<endl;
}

return 0;
}
//Solution of (ii)
#include<iostream>
using namespace std;
int main()
{ int i,j; for(i=1;i<=5;i++)
{ for(j=1;j<=i;j++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (iii)
#include<iostream>
using namespace std;
int main()
{ int i,j,k; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=1;k<=i;k++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (iv)
#include<iostream>
using namespace std;
int main()
{ int i,j,k; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=1;k<2*i;k++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (v)
#include<iostream>
using namespace std;
intmain()
{ int i,j,k; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=1;k<2*i;k++)
cout<<i;
cout<<endl;
}

return0;
}

//Solution of (vi)
#include<iostream>
using namespace std;
intmain()
{ int i,j,k,l; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=i;k>=1;k--)
cout<<k; for(l=2;l<=i;l++)
cout<<l;
cout<<endl;
}

return0;
}
Write a program to compute sinx for given x. The user should supply x and a
positive integer n. We compute the sine of x using the series and the
computation should use all terms in the series up through the term involving
x
sin x = x - x3/3! + x5/5! - x7/7!+x9/9! .........

Source Code
Output

#include<iostream>
using namespace std;
int main()
{ int i,j,n,fact,sign=-1; float x, p=1,sum=0;
cout<<"Enter the value of x : ";
cin>>x;
cout<<"Enter the value of n : ";
cin>>n;
for(i=1;i<=n;i+=2)
{
fact=1; for(j=1;j<=i;j++)
{
p=p*x;
fact=fact*j;
}
sign=-1*sign;
sum+=sign*p/fact;
}
cout<<"sin "<<x<<"="<<sum;

return 0;
}
Write a program to compute the cosine of x. The user should supply x and a
positive integer n. We compute the cosine of x using the series and the
computation should use all terms in the series up through the term involving
xn
cos x = 1 - x2/2! + x4/4! - x6/6! -.....
Source Code
Output

#include<iostream>

using namespace std;

int main()

{
int i,j,n,fact,sign=-1;
float x, p=1,sum=0;

cout<<"Enter the value of x : ";

cin>>x;

cout<<"Enter the value of n : ";

cin>>n;

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

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

p=p*x;

fact=fact*j;
}

sum+=sign*p/fact;

sign=-1*sign;

cout<<"cos "<<x<<"="<<1+sum;

return 0;

}
Write a program which input principal, rate and time from user and calculate
compound interest.
CI = P(1+R/100)T - P

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cmath>
using namespace std;
int main()
{ float P,R,T,CI;
cout<<"Enter Principal, Rate and Time : ";
cin>>P>>R>>T;
CI=P*pow((1+R/100),T) - P;
cout<<"Compound Interest is : "<<CI;
return 0;
}
Write a program to compute area of triangle. Sides are input by user.
Area = sqrt(s*(s-a)*(s-b)*(s-c))
where s=(a+b+c)/2

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cmath>
using namespace std;
int main()
{ float a,b,c,s,Area;
cout<<"Enter three sides of triangle : ";
cin>>a>>b>>c;
s=(a+b+c)/2;
Area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"Area of triangle is : "<<Area;

return 0;
}
Write a program to check character entered is alphabet, digit or special
character.

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cctype>
using namespace std;
int main()
{ char ch;
cout<<"Enter any character :";
ch=getchar(); if(isalpha(ch))
cout<<"Alphabet"; else if(isdigit(ch))
cout<<"Number"; else
cout<<"Special Character";
return 0;
}
Write a program which display a number between 10 to 100 randomly.

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cstdlib>
#include <ctime>
using namespace std;
int main()
{ int n;
srand(time(0));
n = rand() % 91 + 10;
cout<<"The randomly selected number is :"<<n;
return 0;
}
Write a program which accept a letter and display it in uppercase letter.

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cctype>
using namespace std;
int main()
{ char ch;
cout<<"Enter any character :";
ch=getchar();
ch=toupper(ch);
cout<<ch;

return 0;
}
Write a C++ program to implement the Number Guessing Game. In this
game the computer chooses a random number between 1 and 100, and the
player tries to guess the number in as few attempts as possible. Each time
the player enters a guess, the computer tells him whether the guess is too
high, too low, or right. Once the player guesses the number, the game is
over.

ANSI C++
Pre-Standard C++
Output
#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main()

{
int num, guess, tries = 0;

srand(time(0)); //seed random number generator

num = rand() % 100 + 1; // random number between 1 and 100

cout << "Guess My Number Game\n\n";


do
{

cout << "Enter a guess between 1 and 100 : ";

cin >> guess;

tries++;
if (guess > num)
cout << "Too high!\n\n";
else if (guess < num)

cout << "Too low!\n\n";


else

cout << "\nCorrect! You got it in " << tries << "
guesses!\n";

} while (guess != num);

cin.ignore();

cin.get();
return 0;

}
Write a C++ program using function which accept two integers as an
argument and return its sum. Call this function from main( ) and print the
results in main( ).

ANSI C++
Output
#include<iostream>
using namespace std;
int sum(int, int);
int main()
{ int x,y,s;
cout<<"Enter first number : ";
cin>>x;
cout<<"Enter second number : ";
cin>>y;
s=sum(x,y);
cout<<"The sum is : "<<s;

return 0;
}
int sum(int a, int b)
{
int total;
total=a+b; return total;
}
Write a function to calculate the factorial value of any integer as an argument.
Call this function from main( ) and print the results in main( ).

ANSI C++
Output

#include<iostream>
using namespace std;
int factorial(int);
int main()
{ int x,f;
cout<<"Enter number : ";
cin>>x;
f=factorial(x);
cout<<"The factorial is :"<<f;
return 0;
}
int factorial(int a)
{ int fact=1;
while(a>=1)
{
fact=fact*a;
a--;
}
return fact;
}
Write a function that receives two numbers as an argument and display all
prime numbers between these two numbers. Call this function from main( ).

ANSI C++
Output
#include<iostream>
using namespace std;
void showprime(int, int);
int main()
{
int x,y;
cout<<"Enter first number : ";
cin>>x;

cout<<"Enter second number : ";


cin>>y;
showprime(x,y);

return 0;
}
void showprime(int a, int b)
{ bool flag;
for(int i=a+1;i<=b;i++)
{

flag=false; for(int j=2;j<i;j++)


{
if(i%j==0)
{
flag=true; break;
}
}
if(flag==false && i>1)
cout<<i<<endl;
}
}
Raising a number to a power p is the same as multiplying n by itself p times.
Write a function called power that takes two arguments, a double value for n
and an int value for p, and return the result as double value. Use default
argument of 2 for p, so that if this argument is omitted the number will be
squared. Write the main function that gets value from the user to test power
function.

ANSI C++
Output

#include<iostream>
using namespace std;
double power(double,int=2);
int main()
{ int p; double n,r;
cout << "Enter number : ";
cin >> n;
cout << "Enter exponent : ";
cin >> p;
r = power(n,p);
cout << "Result is " << r;
r = power(n);
cout << "\nResult without passing exponent is " << r;
return 0;
}
double power(double a, int b)
{ double x = 1; for(int i = 1; i <= b; i++)
x = x * a; return x;
}
Write the output of the following program :
#include <iostream>
using namespace std;

void X(int &A, int &B)


{
A = A + B;
B = A - B;
A = A - B;
}

int main()
{
int a = 4, b = 18;
X(a,b);
cout << a << ", " << b;

return 0;
}

Show the answer.


18, 4

7 Write the output of the following program:

#include <iostream>
using namespace std;

void X(int A, int &B)


{
A = A + B;
B = A - B;
A = A - B;
}

int main()
{
int a = 4, b = 18;
X(a,b);
cout << a << ", " << b;

return 0;
}

Show the answer.


4, 4

8 Write the output of the following program:


#include <iostream>
using namespace std;
void Execute(int &B, int C = 100)
{
int temp = B + C;
B += temp;
if (C == 100)
cout << temp << " " << B << " " << C << endl;
}

int main()
{
int M = 90, N = 10;
Execute(M);
cout << M << " " << N << endl;
Execute(M, N);
cout << M << " " << N << endl;

return 0;
}

Show the answer.


190 280 100
280 10
570 10

9 Give the output of the following program


#include <iostream>
using namespace std;

int global = 10;

void func(int &x, int y)


{
x = x - y;
y = x * 10;
cout << x << ", " << y << '\n';
}

int main()
{
int global = 7;
func (::global, global);
cout << global << ", " << ::global << '\n';

func(global, ::global);
cout << global << ", " << ::global << '\n';

return 0;
}

Show the answer.


3, 30
7, 3
4, 40
4, 3

10 Write the output of the following program :


#include <iostream>
using namespace std;

static int i = 100;


void abc()
{
static int i = 8;
cout << "first = " << i++ << endl;
}

int main()
{
static int i = 2;
abc();
cout << "second = " << i << endl;
abc();

return 0;
}

Show the answer.


first = 8
second = 2
first = 9

11 Write the output of the following program:


#include <iostream>
using namespace std;

int func(int &x, int y = 10)


{
if (x % y == 0)
return ++x;
else
return y--;
}
int main()
{
int p = 20, q = 23;

q = func(p, q);
cout << p << " " << " " << q << endl;

p = func (q);
cout << p << " " << " " << q << endl;

q = func (p);
cout << p << " " << " " << q << endl;

return 0;
}

Show the answer.


20 23
10 23
11 11

Write a program that lets the user perform arithmetic operations on two
numbers. Your program must be menu driven, allowing the user to select the
operation (+, -, *, or /) and input the numbers. Furthermore, your program
must consist of following functions:
1. Function showChoice: This function shows the options to the user and
explains how to enter data.
2. Function add: This function accepts two number as arguments and returns
sum.
3. Function subtract: This function accepts two number as arguments and
returns their difference.
4. Function mulitiply: This function accepts two number as arguments and
returns product.
5. Function divide: This function accepts two number as arguments and
returns quotient.

ANSI C++
Output

#include <iostream>
using namespace std;
void showChoices();float add(float, float);float
subtract(float, float);float multiply(float, float);float
divide(float, float);
int main()
{ float x, y; int choice; do {
showChoices();
cin >> choice; switch (choice)
{ case 1:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Sum " << add(x,y) <<endl; break;
case 2:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Difference " << subtract(x,y) <<endl;
break; case 3:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Product " << multiply(x,y) <<endl;
break; case 4:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Quotient " << divide(x,y) <<endl;
break; case 5: break; default:
cout << "Invalid input" << endl;
}
}while (choice != 5);
return 0;
}
void showChoices()
{
cout << "MENU" << endl;
cout << "1: Add " << endl;
cout << "2: Subtract" << endl;
cout << "3: Multiply " << endl;
cout << "4: Divide " << endl;
cout << "5: Exit " << endl;
cout << "Enter your choice :";
}
float add(float a, float b)
{ return a + b;
}
float subtract(float a, float b)
{ return a - b;
}
float multiply(float a, float b)
{ return a * b;
}
float divide(float a, float b)
{ return a / b;
}
Write a C++ program to find the sum and average of one dimensional integer
array.

ANSI C++
Output
#include<iostream>
using namespace std;
intmain()
{ int Arr[100],n,i,sum=0;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{

cout<<"Enter element "<<i+1<<":";


cin>>Arr[i];
}
for(i=0;i<n;i++)
sum+=Arr[i];

cout<<"\nThe sum of Array is :"<<sum;


cout<<"\nThe average of Array is :"<<sum/i;
return 0;
}
Write a C++ program to swap first and last element of an integer 1-d array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,i,temp;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{

cout<<"Enter element "<<i+1<<":";


cin>>Arr[i];
}

temp=Arr[0];
Arr[0]=Arr[n-1];

Arr[n-1]=temp;

cout<<"\nArray after swapping"<<endl;


for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}
Write a C++ program to swap first and last element of an integer 1-d array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,i,temp;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{

cout<<"Enter element "<<i+1<<":";


cin>>Arr[i];
}

temp=Arr[0];
Arr[0]=Arr[n-1];

Arr[n-1]=temp;

cout<<"\nArray after swapping"<<endl;


for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}
Write a C++ program to find the largest and smallest element of an array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,i,small,large;
cout<<"Enter number of elements you want to insert ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}

small=Arr[0];
large=Arr[0];
for(i=1;i<n;i++)
{ if(Arr[i]<small)
small=Arr[i]; if(Arr[i]>large)
large=Arr[i];
}

cout<<"\nLargest element is :"<<large;


cout<<"\nSmallest element is :"<<small;
return 0;
}
Write a C++ program to reverse the element of an integer 1-D array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,temp,i,j;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}
for(i=0,j=n-1;i<n/2;i++,j--)
{
temp=Arr[i];
Arr[i]=Arr[j];
Arr[j]=temp;
}

cout<<"\nReverse array"<<endl;
for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}
Write a menu driven C++ program with following option
a. Accept elements of an array
b. Display elements of an array
c. Sort the array using insertion sort method
d. Sort the array using selection sort method
e. Sort the array using bubble sort method
Write C++ functions for all options. The functions should have two
parameters name of the array and number of elements in the array.

ANSI C++
Output
#include<iostream>
using namespace std;
void accept(int Arr[], int s);void display(int Arr[], int
s);void isort(int Arr[], int s);void ssort(int Arr[], int
s);void bsort(int Arr[],int s);
int main()
{ int Arr[100],n,choice;
cout<<"Enter Size of Array ";
cin>>n; do {
cout<<"\n\nMENU";
cout<<"\n1. Accept elements of array";
cout<<"\n2. Display elements of array";
cout<<"\n3. Sort the array using insertion sort
method";
cout<<"\n4. Sort the array using selection sort
method";
cout<<"\n5. Sort the array using bubble sort method";
cout<<"\n6. Exit";
cout<<"\n\nEnter your choice 1-6 :";
cin>>choice;
switch(choice)
{ case 1: accept(Arr,n);
break; case 2: display(Arr,n);
break; case 3: isort(Arr,n); break;
case 4: ssort(Arr,n); break; case 5:
bsort(Arr,n); break; case 6: break;
default:cout<<"\nInvalid choice";
}
}while(choice!=6);
return 0;
}
void accept(int Arr[], int s)
{ for(int I=0;I<s;I++)
{
cout<<"Enter element "<<I+1<<":";
cin>>Arr[I];
}
}
void display(int Arr[], int s)
{
cout<<"The elements of the array are:\n"; for(int
I=0;I<s;I++)
cout<<Arr[I]<<" ";
}
void isort(int Arr[], int s)
{ int I,J,Temp; for(I=1;I<s;I++)
{
Temp=Arr[I];
J=I-1; while((Temp<Arr[J]) && (J>=0))
{
Arr[J+1]=Arr[J];
J--;
}
Arr[J+1]=Temp;
}
}
void ssort(int Arr[], int s)
{ int I,J,Temp,Small; for(I=0;I<s-1;I++)
{
Small=I; for(J=I+1;J<s;J++) //finding the
smallest element
if(Arr[J]<Arr[Small])
Small=J; if(Small!=I)
{
Temp=Arr[I]; //Swapping
Arr[I]=Arr[Small];
Arr[Small]=Temp;
}
}
}
void bsort(int Arr[],int s)
{ int I,J,Temp; for(I=0;I<s-1;I++)
{ for(J=0;J<(s-1-I);J++)
if(Arr[J]>Arr[J+1])
{
Temp=Arr[J]; //swapping
Arr[J]=Arr[J+1];
Arr[J+1]=Temp;
}
}
}
P is one-dimensional array of integers. Write a C++ function to efficiently
search for a data VAL from P. If VAL is present in the array then the function
should return value 1 and 0 otherwise.

ANSI C++
Output
#include<iostream>
using namespace std;
bool lsearch(int Arr[], int s, int VAL);
int main()
{ int Arr[100],n,val; bool found;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}

cout<<"Enter the number you want to search ";


cin>>val;

found=lsearch(Arr,n,val);
if(found)
cout<<"\nItem found"; else
cout<<"\nItem not found";

return 0;
}
bool lsearch(int Arr[], int s, int VAL)
{ for(int I=0; I<s; I++)
{ if(Arr[I]==VAL) return true;
} return false;
}
Suppose a one-dimensional array AR containing integers is arranged in
ascending order. Write a user-defined function in C++ to search for an
integer from AR with the help of Binary search method, returning an integer
0 to show absence of the number and integer 1 to show presence of the
number in the array. Function should have three parameters : (i) array AR (ii)
the number to be searched and (iii) the number of elements N in the array.

ANSI C++
Output
#include<iostream>
using namespace std;
bool bsearch(int AR[], int N, int VAL);
int main()
{ int AR[100],n,val; bool found;

cout<<"Enter number of elements you want to insert ";


cin>>n;

cout<<"Enter element in ascending order\n";


for(int i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>AR[i];
}

cout<<"\nEnter the number you want to search ";


cin>>val;

found=bsearch(AR,n,val);
if(found)
cout<<"\nItem found"; else
cout<<"\nItem not found";

return 0;
}
bool bsearch(int AR[], int N, int VAL)
{ int Mid,Lbound=0,Ubound=N-1; while(Lbound<=Ubound)
{
Mid=(Lbound+Ubound)/2; if(VAL>AR[Mid])
Lbound=Mid+1; else if(VAL<AR[Mid])
Ubound=Mid-1; else
return true;
} returnfalse;
}
Suppose A, B, C are arrays of integers of size M, N, and M + N respectively.
The numbers in array A appear in ascending order while the numbers in array
B appear in descending order. Write a user defined function in C++ to
produce third array C by merging arrays A and B in ascending order. Use A,
B and C as arguments in the function.

ANSI C++
Output
#include<iostream>
using namespace std;
void Merge(int A[], int B[],int C[], int N,int M, int &K);

int main()
{ int A[100], B[100], C[200],i,n,m,k;

cout<<"\nEnter number of elements you want to insert in


first array ";
cin>>n;

cout<<"Enter element in ascending order\n";


for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>A[i];
}

cout<<"\nEnter number of elements you want to insert in


second array ";
cin>>m;

cout<<"Enter element in descending order\n";


for(i=0;i<m;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>B[i];
}

Merge(A,B,C,n,m,k);

cout<<"\nThe Merged Array in Ascending Order"<<endl;


for(i=0;i<k;i++)
{
cout<<C[i]<<" ";
}
return 0;
}

void Merge(int A[], int B[], int C[], int N, int M, int &K)
{ int I=0, J=M-1;
K=0;
while (I<N && J>=0)
{ if (A[I]<B[J])
C[K++]=A[I++]; else if (A[I]>B[J])
C[K++]=B[J--]; else {
C[K++]=A[I++];
J--;
}
}
for (int T=I;T<N;T++)
C[K++]=A[T]; for (int T=J;T>=0;T--)
C[K++]=B[T];
}
uppose X. Y, Z are arrays of integers of size M, N, and M + N respectively. The
numbers in array X and Y appear in descending order. Write a user-defined
function in C++ to produce third array Z by merging arrays X and Y in
descending order.

ANSI C++
Output
#include<iostream>
using namespace std;
void Merge(int A[], int B[], int C[], int N, int M, int &K);

int main()
{ int A[100], B[100], C[200],i,n,m,k;

cout<<"\nEnter number of elements you want to insert in


first array ";
cin>>n;

cout<<"Enter element in descending order\n";


for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>A[i];
}

cout<<"\nEnter number of elements you want to insert in


second array ";
cin>>m;

cout<<"Enter element in descending order\n";


for(i=0;i<m;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>B[i];
}

Merge(A,B,C,n,m,k);

cout<<"\nThe Merged Array in Descending Order"<<endl;


for(i=0;i<k;i++)
{
cout<<C[i]<<" ";
}
return 0;
}

void Merge(int A[], int B[], int C[], int N, int M, int &K)
{ int I=0, J=0;
K=0;
while (I<N && J<M)
{ if (A[I]>B[J])
C[K++]=A[I++]; else if (A[I]<B[J])
C[K++]=B[J++]; else {
C[K++]=A[I++];
J++;
}
}
for (int T=I;T<N;T++)
C[K++]=A[T]; for (int T=J;T<M;T++)
C[K++]=B[T];
}
Write a program to find the length of string.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout<<"Enter string: ";


cin.getline(str, 80);
int i; //Hold length of string

for(i = 0; str[i] != '\0'; i++);

cout << "Length of string is: " << i;


return 0;
}
Assignments » String » Set1 »Solution 2

Write a program to display string from backward.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout<<"Enter string: ";


cin.getline(str, 80);
int l; //Hold length of string

//Find the length of the string


for(l = 0; str[l] != '\0'; l++);
//Display the string backwards
for(int i = l -1; i >= 0; i--)
{
cout << str[i];
}

return 0;
}
Write a program to count number of words in string.
ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout << "Enter a string: ";


cin.getline(str,80);
int words = 0; // Holds number of words

for(int i = 0; str[i] != '\0'; i++)


{ if (str[i] == ' ') //Checking for spaces
{
words++;
}
}

cout << "The number of words = " << words+1 << endl;
return 0;
}
Write a program to concatenate one string contents to another.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str1[80], str2[80];

cout<<"Enter first string: ";


cin.getline(str1, 80);

cout<<"Enter second string: ";


cin.getline(str2, 80);
int l = 0; //Hold length of first string

//Find length of first string.


for(l = 0; str1[l] != '\0'; l++);
//Adding second string content in first
for(int i = 0; str2[i] != '\0'; i++)
{
str1[l++] = str2[i];
}

str1[l] ='\0';

cout << "\nThe first string after adding second string


content:\n\n" << str1;

return 0;
}
Write a C++ program to compare two strings they are exact equal or not.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str1[80], str2[80];

cout<<"Enter first string: ";


gets(str1);

cout<<"Enter second string: ";


gets(str2);
int i; for (i = 0; str1[i] == str2[i] && str1[i]!= '\0'
&& str2[i] != '\0'; i++);
if(str1[i] - str2[i] == 0)
cout << "Strings are equal"; else
cout << "Strings are not equal";
return 0;
}
rite a program to check a string is palindrome or not.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout<<"Enter string: ";


cin.getline(str, 80);
int l; //Hold length of string

//finding length of string


for(l = 0; str[l] !='\0'; l++);
//Comparing first element with last element till middle
of string
int i; for(i = 0; (i < l/2) && (str[i] == str[l - i -
1]); i++);
if(i == l/2)
cout << "Palindrome"; else
cout << "Not a palindrome";
return 0;
}

Write the output of the following program. Assume that all necessary header
files are included.
void Encrypt(char T[])
{
for (int i = 0; T[i] != '\0'; i += 2)
if (T[i] == 'A' || T[i] == 'E')
T[i] = '#';
else if (islower(T[i]))
T[i] = toupper(T[i]);
else
T[i] = '@';
}

int main()
{
char text[]="SaVE EArtH";
Encrypt(text);
cout << text << endl;

return 0;
}

Show the answer.


@a@E@E#rTH

12 Write the output of the following program


int main()
{
char name[] = "CoMPutER";
for (int x = 0; x < strlen(name);
x++)
if (islower(name [x]))
name [x] =
toupper(name[x]);
else
if (isupper(name[x]))
if (x % 2 == 0)
name[x] =
tolower(name[x]);
else
name[x] =
name[x-1];

cout << name;

return 0;
}

Show the answer.


cOmmUTee

Two numbers are entered through the keyboard. Write a program to find the
value of one number raised to the power of another.

Source Code
Output
#include<iostream>
using namespace std;
int main()
{
int n,p,r=1;
cout<<"Enter the base number and exponent ";
cin>>n>>p;
for(int i=1;i<=p;i++)
r=r*n;

cout<<"Result :"<<r;

return 0;
}

Write a program to reverse any given integer number.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,t,r,rev=0;
cout<<"Enter any number : ";
cin>>n;
t=n;
while(t>0)
{
r=t%10;
t=t/10;
rev=rev*10+r;
}

cout<<"Reverse of number "<<n<<" is "<<rev;

return 0;
}
Write a program to sum of digits of given integer number.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,t,r,sum=0;
cout<<"Enter any number : ";
cin>>n;
t=n;
while(t>0)
{
r=t%10;
sum+=r;
t=t/10;
}

cout<<"Sum of digits of number "<<n<<" is "<<sum;

return 0;
rite a program to check given number is prime or not.
Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n;
bool flag=false;
cout<<"Enter any number : ";
cin>>n;
for(int i=2;i<n;i++)
{ if(n%i==0)
{
flag=true; break;
}
}
if(flag==false && n>1)
cout<<"Number is prime"; else
cout<<"Number is not prime";

return 0;
}
Write a program to calculate HCF of Two given number.

Source Code
Output

#include<iostream>
using namespace std;
intmain()
{
int dividend, divisor, rem, hcf;
cout<<"Enter two numbers : ";
cin>>dividend>>divisor;
while(rem!=0)
{
rem=dividend%divisor; if(rem==0)
hcf=divisor; else {
dividend=divisor;
divisor=rem;
}
}

cout<<"HCF is : "<<hcf;

return 0;
}

Write a program to enter the numbers till the user wants and at the end it
should display the count of positive, negative and zeros entered.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n, sum_p=0, sum_n=0, sum_z=0; char choice;
do {
cout<<"Enter number ";
cin>>n;
if(n>0)
sum_p++; else if(n<0)
sum_n++; else
sum_z++;

cout<<"Do you want to Continue(y/n)? ";


cin>>choice;

}while(choice=='y'|| choice=='Y');

cout<<"Positive Number :"<<sum_p<<"\nNegative


Number :"<<sum_n<<"\nZero Number :"<<sum_z;

return 0;
}

Write a program to enter the numbers till the user wants and at the end it
should display the maximum and minimum number entered.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n, max=0, min=32767; char choice;
do {
cout<<"Enter number : ";
cin>>n;
if(n>max)
max=n; if(n<min)
min=n;

cout<<"Do you want to Continue(y/n)? ";


cin>>choice;

}while(choice=='y' || choice=='Y');

cout<<"Maximum Number :"<<max<<"\nMinimum


Number :"<<min;

return 0;
}
Write a program to print all Armstrong numbers between 1 and 500. If sum
of cubes of each digit of the number is equal to the number itself, then the
number is called an Armstrong number.
For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )

Source Code
Output

#include<iostream>
using namespace std;
int main()
{
int n,digit1,digit2,digit3;
for(int i=1;i<=500;i++)
{
digit1=i/100;
digit2=i/10 - digit1*10;
digit3=i%10;
if(digit1*digit1*digit1 + digit2*digit2*digit2 +
digit3*digit3*digit3 == i)
cout<<i<<endl;
}

return 0;
}

Write a program to print Fibonacci series of n terms where n is input by user


0 1 1 2 3 5 8 13 24 .....

Source Code
Output

#include<iostream>
using namespace std;

int main()
{
int f=0,s=1,t,n;

cout<<"Enter Number of terms of Series : ";


cin>>n;

cout<<f<<" "<<s<<" ";


for(int i=3;i<=n;i++)
{
t=f+s;
cout<<t<<" ";
f=s;
s=t;
}
return 0;
}
Write a program to calculate the sum of following series where n is input by
user.
1 + 1/2 + 1/3 + 1/4 + 1/5 +…………1/n

Source Code
Output

#include<iostream>
using namespace std;
int main()
{ int i,n; float sum=0;

cout<<"Enter the value of n ";


cin>>n;
for(i=1;i<=n;i++)
sum += 1.0/i;

cout<<"Sum : "<<sum;

return 0;
}
Compute the natural logarithm of 2, by adding up to n terms in the series
1 - 1/2 + 1/3 - 1/4 + 1/5 -... 1/n
where n is a positive integer and input by user.

Source Code
Output

#include<iostream>
using namespace std;
int main()
{ int i,n,sign=-1; float sum=0;
cout<<"Enter the value of n ";
cin>>n;
for(i=1;i<=n;i++)
{
sign *= -1;
sum += sign*1.0/i;
}
cout<<"log 2 : "<<sum;

return 0;
}
Write C++ program to print following pattern:
i) ii) iii)
********** * *
********** ** **
********** *** ***
********** **** ****
***** *****
iv) v) 1 vi) 1
* 222 212
*** 33333 32123
4444444 4321234
***** 555555555 543212345
*******
*********
Source Code

//Solution of (i)
#include<iostream>
using namespace std;
int main()
{ int i,j; for(i=1;i<=4;i++)
{ for(j=1;j<=10;j++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (ii)
#include<iostream>
using namespace std;
int main()
{ int i,j; for(i=1;i<=5;i++)
{ for(j=1;j<=i;j++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (iii)
#include<iostream>
using namespace std;
int main()
{ int i,j,k; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=1;k<=i;k++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (iv)
#include<iostream>
using namespace std;
int main()
{ int i,j,k; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=1;k<2*i;k++)
cout<<'*';
cout<<endl;
}

return 0;
}

//Solution of (v)
#include<iostream>
using namespace std;
intmain()
{ int i,j,k; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=1;k<2*i;k++)
cout<<i;
cout<<endl;
}

return0;
}

//Solution of (vi)
#include<iostream>
using namespace std;
intmain()
{ int i,j,k,l; for(i=1;i<=5;i++)
{ for(j=5;j>i;j--)
cout<<' '; for(k=i;k>=1;k--)
cout<<k; for(l=2;l<=i;l++)
cout<<l;
cout<<endl;
}
return0;
}
Write a program to compute sinx for given x. The user should supply x and a
positive integer n. We compute the sine of x using the series and the
computation should use all terms in the series up through the term involving
x
sin x = x - x3/3! + x5/5! - x7/7!+x9/9! .........

Source Code
Output

#include<iostream>
using namespace std;
int main()
{ int i,j,n,fact,sign=-1; float x, p=1,sum=0;
cout<<"Enter the value of x : ";
cin>>x;
cout<<"Enter the value of n : ";
cin>>n;
for(i=1;i<=n;i+=2)
{
fact=1; for(j=1;j<=i;j++)
{
p=p*x;
fact=fact*j;
}
sign=-1*sign;
sum+=sign*p/fact;
}
cout<<"sin "<<x<<"="<<sum;

return 0;
}
Write a program to compute the cosine of x. The user should supply x and a
positive integer n. We compute the cosine of x using the series and the
computation should use all terms in the series up through the term involving
xn
cos x = 1 - x2/2! + x4/4! - x6/6! -.....

Source Code
Output

#include<iostream>

using namespace std;

int main()

{
int i,j,n,fact,sign=-1;
float x, p=1,sum=0;

cout<<"Enter the value of x : ";

cin>>x;

cout<<"Enter the value of n : ";

cin>>n;

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

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

p=p*x;

fact=fact*j;

sum+=sign*p/fact;

sign=-1*sign;

cout<<"cos "<<x<<"="<<1+sum;

return 0;

}
Write a program which input principal, rate and time from user and calculate
compound interest.
CI = P(1+R/100)T - P

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cmath>
using namespace std;
int main()
{ float P,R,T,CI;
cout<<"Enter Principal, Rate and Time : ";
cin>>P>>R>>T;
CI=P*pow((1+R/100),T) - P;
cout<<"Compound Interest is : "<<CI;
return 0;
}
Write a program to compute area of triangle. Sides are input by user.
Area = sqrt(s*(s-a)*(s-b)*(s-c))
where s=(a+b+c)/2

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cmath>
using namespace std;
int main()
{ float a,b,c,s,Area;
cout<<"Enter three sides of triangle : ";
cin>>a>>b>>c;
s=(a+b+c)/2;
Area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"Area of triangle is : "<<Area;

return 0;
}
Write a program to check character entered is alphabet, digit or special
character.

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cctype>
using namespace std;
int main()
{ char ch;
cout<<"Enter any character :";
ch=getchar(); if(isalpha(ch))
cout<<"Alphabet"; else if(isdigit(ch))
cout<<"Number"; else
cout<<"Special Character";
return 0;
}
Write a program which display a number between 10 to 100 randomly.

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cstdlib>
#include <ctime>
using namespace std;
int main()
{ int n;
srand(time(0));
n = rand() % 91 + 10;
cout<<"The randomly selected number is :"<<n;
return 0;
}
Write a program which accept a letter and display it in uppercase letter.

ANSI C++
Pre-Standard C++
Output
#include<iostream>
#include<cctype>
using namespace std;
int main()
{ char ch;
cout<<"Enter any character :";
ch=getchar();
ch=toupper(ch);
cout<<ch;

return 0;
}
Write a C++ program to implement the Number Guessing Game. In this
game the computer chooses a random number between 1 and 100, and the
player tries to guess the number in as few attempts as possible. Each time
the player enters a guess, the computer tells him whether the guess is too
high, too low, or right. Once the player guesses the number, the game is
over.

ANSI C++
Pre-Standard C++
Output
#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main()

{
int num, guess, tries = 0;

srand(time(0)); //seed random number generator

num = rand() % 100 + 1; // random number between 1 and 100

cout << "Guess My Number Game\n\n";


do
{

cout << "Enter a guess between 1 and 100 : ";


cin >> guess;

tries++;
if (guess > num)

cout << "Too high!\n\n";


else if (guess < num)

cout << "Too low!\n\n";


else

cout << "\nCorrect! You got it in " << tries << "
guesses!\n";

} while (guess != num);

cin.ignore();

cin.get();
return 0;

}
Write a C++ program using function which accept two integers as an
argument and return its sum. Call this function from main( ) and print the
results in main( ).

ANSI C++
Output
#include<iostream>
using namespace std;
int sum(int, int);
int main()
{ int x,y,s;
cout<<"Enter first number : ";
cin>>x;
cout<<"Enter second number : ";
cin>>y;
s=sum(x,y);
cout<<"The sum is : "<<s;

return 0;
}
int sum(int a, int b)
{
int total;
total=a+b; return total;
}
Write a function to calculate the factorial value of any integer as an argument.
Call this function from main( ) and print the results in main( ).

ANSI C++
Output

#include<iostream>
using namespace std;
int factorial(int);
int main()
{ int x,f;
cout<<"Enter number : ";
cin>>x;
f=factorial(x);
cout<<"The factorial is :"<<f;
return 0;
}
int factorial(int a)
{ int fact=1;
while(a>=1)
{
fact=fact*a;
a--;
}
return fact;
}
Write a function that receives two numbers as an argument and display all
prime numbers between these two numbers. Call this function from main( ).

ANSI C++
Output
#include<iostream>
using namespace std;
void showprime(int, int);
int main()
{
int x,y;
cout<<"Enter first number : ";
cin>>x;

cout<<"Enter second number : ";


cin>>y;
showprime(x,y);

return 0;
}
void showprime(int a, int b)
{ bool flag;
for(int i=a+1;i<=b;i++)
{

flag=false; for(int j=2;j<i;j++)


{
if(i%j==0)
{
flag=true; break;
}
}
if(flag==false && i>1)
cout<<i<<endl;
}
}
Raising a number to a power p is the same as multiplying n by itself p times.
Write a function called power that takes two arguments, a double value for n
and an int value for p, and return the result as double value. Use default
argument of 2 for p, so that if this argument is omitted the number will be
squared. Write the main function that gets value from the user to test power
function.

ANSI C++
Output

#include<iostream>
using namespace std;
double power(double,int=2);
int main()
{ int p; double n,r;
cout << "Enter number : ";
cin >> n;
cout << "Enter exponent : ";
cin >> p;
r = power(n,p);
cout << "Result is " << r;
r = power(n);
cout << "\nResult without passing exponent is " << r;
return 0;
}
double power(double a, int b)
{ double x = 1; for(int i = 1; i <= b; i++)
x = x * a; return x;
}
Write the output of the following program :
#include <iostream>
using namespace std;

void X(int &A, int &B)


{
A = A + B;
B = A - B;
A = A - B;
}

int main()
{
int a = 4, b = 18;
X(a,b);
cout << a << ", " << b;

return 0;
}

Show the answer.


18, 4

7 Write the output of the following program:

#include <iostream>
using namespace std;

void X(int A, int &B)


{
A = A + B;
B = A - B;
A = A - B;
}

int main()
{
int a = 4, b = 18;
X(a,b);
cout << a << ", " << b;

return 0;
}

Show the answer.


4, 4
8 Write the output of the following program:
#include <iostream>
using namespace std;

void Execute(int &B, int C = 100)


{
int temp = B + C;
B += temp;
if (C == 100)
cout << temp << " " << B << " " << C << endl;
}

int main()
{
int M = 90, N = 10;
Execute(M);
cout << M << " " << N << endl;
Execute(M, N);
cout << M << " " << N << endl;

return 0;
}

Show the answer.


190 280 100
280 10
570 10

9 Give the output of the following program


#include <iostream>
using namespace std;

int global = 10;

void func(int &x, int y)


{
x = x - y;
y = x * 10;
cout << x << ", " << y << '\n';
}

int main()
{
int global = 7;
func (::global, global);
cout << global << ", " << ::global << '\n';

func(global, ::global);
cout << global << ", " << ::global << '\n';

return 0;
}

Show the answer.


3, 30
7, 3
4, 40
4, 3

10 Write the output of the following program :


#include <iostream>
using namespace std;

static int i = 100;


void abc()
{
static int i = 8;
cout << "first = " << i++ << endl;
}

int main()
{
static int i = 2;
abc();
cout << "second = " << i << endl;
abc();

return 0;
}

Show the answer.


first = 8
second = 2
first = 9

11 Write the output of the following program:


#include <iostream>
using namespace std;

int func(int &x, int y = 10)


{
if (x % y == 0)
return ++x;
else
return y--;
}

int main()
{
int p = 20, q = 23;

q = func(p, q);
cout << p << " " << " " << q << endl;

p = func (q);
cout << p << " " << " " << q << endl;

q = func (p);
cout << p << " " << " " << q << endl;

return 0;
}

Show the answer.


20 23
10 23
11 11

Write a program that lets the user perform arithmetic operations on two
numbers. Your program must be menu driven, allowing the user to select the
operation (+, -, *, or /) and input the numbers. Furthermore, your program
must consist of following functions:
1. Function showChoice: This function shows the options to the user and
explains how to enter data.
2. Function add: This function accepts two number as arguments and returns
sum.
3. Function subtract: This function accepts two number as arguments and
returns their difference.
4. Function mulitiply: This function accepts two number as arguments and
returns product.
5. Function divide: This function accepts two number as arguments and
returns quotient.

ANSI C++
Output
#include <iostream>
using namespace std;
void showChoices();float add(float, float);float
subtract(float, float);float multiply(float, float);float
divide(float, float);
int main()
{ float x, y; int choice; do {
showChoices();
cin >> choice; switch (choice)
{ case 1:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Sum " << add(x,y) <<endl; break;
case 2:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Difference " << subtract(x,y) <<endl;
break; case 3:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Product " << multiply(x,y) <<endl;
break; case 4:
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Quotient " << divide(x,y) <<endl;
break; case 5: break; default:
cout << "Invalid input" << endl;
}
}while (choice != 5);
return 0;
}
void showChoices()
{
cout << "MENU" << endl;
cout << "1: Add " << endl;
cout << "2: Subtract" << endl;
cout << "3: Multiply " << endl;
cout << "4: Divide " << endl;
cout << "5: Exit " << endl;
cout << "Enter your choice :";
}
float add(float a, float b)
{ return a + b;
}
float subtract(float a, float b)
{ return a - b;
}
float multiply(float a, float b)
{ return a * b;
}
float divide(float a, float b)
{ return a / b;
}
Write a C++ program to find the sum and average of one dimensional integer
array.

ANSI C++
Output
#include<iostream>
using namespace std;
intmain()
{ int Arr[100],n,i,sum=0;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{

cout<<"Enter element "<<i+1<<":";


cin>>Arr[i];
}
for(i=0;i<n;i++)
sum+=Arr[i];

cout<<"\nThe sum of Array is :"<<sum;


cout<<"\nThe average of Array is :"<<sum/i;
return 0;
}
Write a C++ program to swap first and last element of an integer 1-d array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,i,temp;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{

cout<<"Enter element "<<i+1<<":";


cin>>Arr[i];
}

temp=Arr[0];
Arr[0]=Arr[n-1];

Arr[n-1]=temp;

cout<<"\nArray after swapping"<<endl;


for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}
Write a C++ program to swap first and last element of an integer 1-d array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,i,temp;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{

cout<<"Enter element "<<i+1<<":";


cin>>Arr[i];
}

temp=Arr[0];
Arr[0]=Arr[n-1];

Arr[n-1]=temp;

cout<<"\nArray after swapping"<<endl;


for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}
Write a C++ program to find the largest and smallest element of an array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,i,small,large;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}

small=Arr[0];
large=Arr[0];
for(i=1;i<n;i++)
{ if(Arr[i]<small)
small=Arr[i]; if(Arr[i]>large)
large=Arr[i];
}

cout<<"\nLargest element is :"<<large;


cout<<"\nSmallest element is :"<<small;
return 0;
}
Write a C++ program to reverse the element of an integer 1-D array.

ANSI C++
Output
#include<iostream>
using namespace std;
int main()
{ int Arr[100],n,temp,i,j;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}
for(i=0,j=n-1;i<n/2;i++,j--)
{
temp=Arr[i];
Arr[i]=Arr[j];
Arr[j]=temp;
}

cout<<"\nReverse array"<<endl;
for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}
Write a menu driven C++ program with following option
a. Accept elements of an array
b. Display elements of an array
c. Sort the array using insertion sort method
d. Sort the array using selection sort method
e. Sort the array using bubble sort method
Write C++ functions for all options. The functions should have two
parameters name of the array and number of elements in the array.

ANSI C++
Output
#include<iostream>
using namespace std;
void accept(int Arr[], int s);void display(int Arr[], int
s);void isort(int Arr[], int s);void ssort(int Arr[], int
s);void bsort(int Arr[],int s);
int main()
{ int Arr[100],n,choice;
cout<<"Enter Size of Array ";
cin>>n; do {
cout<<"\n\nMENU";
cout<<"\n1. Accept elements of array";
cout<<"\n2. Display elements of array";
cout<<"\n3. Sort the array using insertion sort
method";
cout<<"\n4. Sort the array using selection sort
method";
cout<<"\n5. Sort the array using bubble sort method";
cout<<"\n6. Exit";
cout<<"\n\nEnter your choice 1-6 :";
cin>>choice;
switch(choice)
{ case 1: accept(Arr,n);
break; case 2: display(Arr,n);
break; case 3: isort(Arr,n); break;
case 4: ssort(Arr,n); break; case 5:
bsort(Arr,n); break; case 6: break;
default:cout<<"\nInvalid choice";
}
}while(choice!=6);
return 0;
}
void accept(int Arr[], int s)
{ for(int I=0;I<s;I++)
{
cout<<"Enter element "<<I+1<<":";
cin>>Arr[I];
}
}
void display(int Arr[], int s)
{
cout<<"The elements of the array are:\n"; for(int
I=0;I<s;I++)
cout<<Arr[I]<<" ";
}
void isort(int Arr[], int s)
{ int I,J,Temp; for(I=1;I<s;I++)
{
Temp=Arr[I];
J=I-1; while((Temp<Arr[J]) && (J>=0))
{
Arr[J+1]=Arr[J];
J--;
}
Arr[J+1]=Temp;
}
}
void ssort(int Arr[], int s)
{ int I,J,Temp,Small; for(I=0;I<s-1;I++)
{
Small=I; for(J=I+1;J<s;J++) //finding the
smallest element
if(Arr[J]<Arr[Small])
Small=J; if(Small!=I)
{
Temp=Arr[I]; //Swapping
Arr[I]=Arr[Small];
Arr[Small]=Temp;
}
}
}
void bsort(int Arr[],int s)
{ int I,J,Temp; for(I=0;I<s-1;I++)
{ for(J=0;J<(s-1-I);J++)
if(Arr[J]>Arr[J+1])
{
Temp=Arr[J]; //swapping
Arr[J]=Arr[J+1];
Arr[J+1]=Temp;
}
}
}
P is one-dimensional array of integers. Write a C++ function to efficiently
search for a data VAL from P. If VAL is present in the array then the function
should return value 1 and 0 otherwise.

ANSI C++
Output
#include<iostream>
using namespace std;
bool lsearch(int Arr[], int s, int VAL);
int main()
{ int Arr[100],n,val; bool found;

cout<<"Enter number of elements you want to insert ";


cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}

cout<<"Enter the number you want to search ";


cin>>val;

found=lsearch(Arr,n,val);
if(found)
cout<<"\nItem found"; else
cout<<"\nItem not found";
return 0;
}
bool lsearch(int Arr[], int s, int VAL)
{ for(int I=0; I<s; I++)
{ if(Arr[I]==VAL) return true;
} return false;
}
Suppose a one-dimensional array AR containing integers is arranged in
ascending order. Write a user-defined function in C++ to search for an
integer from AR with the help of Binary search method, returning an integer
0 to show absence of the number and integer 1 to show presence of the
number in the array. Function should have three parameters : (i) array AR (ii)
the number to be searched and (iii) the number of elements N in the array.

ANSI C++
Output
#include<iostream>
using namespace std;
bool bsearch(int AR[], int N, int VAL);
int main()
{ int AR[100],n,val; bool found;

cout<<"Enter number of elements you want to insert ";


cin>>n;

cout<<"Enter element in ascending order\n";


for(int i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>AR[i];
}

cout<<"\nEnter the number you want to search ";


cin>>val;

found=bsearch(AR,n,val);
if(found)
cout<<"\nItem found"; else
cout<<"\nItem not found";

return 0;
}
bool bsearch(int AR[], int N, int VAL)
{ int Mid,Lbound=0,Ubound=N-1; while(Lbound<=Ubound)
{
Mid=(Lbound+Ubound)/2; if(VAL>AR[Mid])
Lbound=Mid+1; else if(VAL<AR[Mid])
Ubound=Mid-1; else
return true;
} returnfalse;
}
Suppose A, B, C are arrays of integers of size M, N, and M + N respectively.
The numbers in array A appear in ascending order while the numbers in array
B appear in descending order. Write a user defined function in C++ to
produce third array C by merging arrays A and B in ascending order. Use A,
B and C as arguments in the function.

ANSI C++
Output
#include<iostream>
using namespace std;
void Merge(int A[], int B[],int C[], int N,int M, int &K);

int main()
{ int A[100], B[100], C[200],i,n,m,k;

cout<<"\nEnter number of elements you want to insert in


first array ";
cin>>n;

cout<<"Enter element in ascending order\n";


for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>A[i];
}

cout<<"\nEnter number of elements you want to insert in


second array ";
cin>>m;

cout<<"Enter element in descending order\n";


for(i=0;i<m;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>B[i];
}

Merge(A,B,C,n,m,k);

cout<<"\nThe Merged Array in Ascending Order"<<endl;


for(i=0;i<k;i++)
{
cout<<C[i]<<" ";
}
return 0;
}

void Merge(int A[], int B[], int C[], int N, int M, int &K)
{ int I=0, J=M-1;
K=0;
while (I<N && J>=0)
{ if (A[I]<B[J])
C[K++]=A[I++]; else if (A[I]>B[J])
C[K++]=B[J--]; else {
C[K++]=A[I++];
J--;
}
}
for (int T=I;T<N;T++)
C[K++]=A[T]; for (int T=J;T>=0;T--)
C[K++]=B[T];
}
uppose X. Y, Z are arrays of integers of size M, N, and M + N respectively. The
numbers in array X and Y appear in descending order. Write a user-defined
function in C++ to produce third array Z by merging arrays X and Y in
descending order.

ANSI C++
Output
#include<iostream>
using namespace std;
void Merge(int A[], int B[], int C[], int N, int M, int &K);

int main()
{ int A[100], B[100], C[200],i,n,m,k;

cout<<"\nEnter number of elements you want to insert in


first array ";
cin>>n;

cout<<"Enter element in descending order\n";


for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>A[i];
}

cout<<"\nEnter number of elements you want to insert in


second array ";
cin>>m;
cout<<"Enter element in descending order\n";
for(i=0;i<m;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>B[i];
}

Merge(A,B,C,n,m,k);

cout<<"\nThe Merged Array in Descending Order"<<endl;


for(i=0;i<k;i++)
{
cout<<C[i]<<" ";
}
return 0;
}

void Merge(int A[], int B[], int C[], int N, int M, int &K)
{ int I=0, J=0;
K=0;
while (I<N && J<M)
{ if (A[I]>B[J])
C[K++]=A[I++]; else if (A[I]<B[J])
C[K++]=B[J++]; else {
C[K++]=A[I++];
J++;
}
}
for (int T=I;T<N;T++)
C[K++]=A[T]; for (int T=J;T<M;T++)
C[K++]=B[T];
}
Write a program to find the length of string.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout<<"Enter string: ";


cin.getline(str, 80);
int i; //Hold length of string

for(i = 0; str[i] != '\0'; i++);

cout << "Length of string is: " << i;


return 0;
}
Assignments » String » Set1 »Solution 2

Write a program to display string from backward.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout<<"Enter string: ";


cin.getline(str, 80);
int l; //Hold length of string

//Find the length of the string


for(l = 0; str[l] != '\0'; l++);
//Display the string backwards
for(int i = l -1; i >= 0; i--)
{
cout << str[i];
}

return 0;
}
Write a program to count number of words in string.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout << "Enter a string: ";


cin.getline(str,80);
int words = 0; // Holds number of words

for(int i = 0; str[i] != '\0'; i++)


{ if (str[i] == ' ') //Checking for spaces
{
words++;
}
}

cout << "The number of words = " << words+1 << endl;
return 0;
}
Write a program to concatenate one string contents to another.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str1[80], str2[80];
cout<<"Enter first string: ";
cin.getline(str1, 80);

cout<<"Enter second string: ";


cin.getline(str2, 80);
int l = 0; //Hold length of first string

//Find length of first string.


for(l = 0; str1[l] != '\0'; l++);
//Adding second string content in first
for(int i = 0; str2[i] != '\0'; i++)
{
str1[l++] = str2[i];
}

str1[l] ='\0';

cout << "\nThe first string after adding second string


content:\n\n" << str1;

return 0;
}
Write a C++ program to compare two strings they are exact equal or not.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str1[80], str2[80];

cout<<"Enter first string: ";


gets(str1);
cout<<"Enter second string: ";
gets(str2);
int i; for (i = 0; str1[i] == str2[i] && str1[i]!= '\0'
&& str2[i] != '\0'; i++);
if(str1[i] - str2[i] == 0)
cout << "Strings are equal"; else
cout << "Strings are not equal";
return 0;
}
rite a program to check a string is palindrome or not.

ANSI C++
Output
#include<iostream>
using namespace std;
int main( )
{ char str[80];

cout<<"Enter string: ";


cin.getline(str, 80);
int l; //Hold length of string

//finding length of string


for(l = 0; str[l] !='\0'; l++);
//Comparing first element with last element till middle
of string
int i; for(i = 0; (i < l/2) && (str[i] == str[l - i -
1]); i++);
if(i == l/2)
cout << "Palindrome"; else
cout << "Not a palindrome";
return 0;
}

Write the output of the following program. Assume that all necessary header
files are included.
void Encrypt(char T[])
{
for (int i = 0; T[i] != '\0'; i += 2)
if (T[i] == 'A' || T[i] == 'E')
T[i] = '#';
else if (islower(T[i]))
T[i] = toupper(T[i]);
else
T[i] = '@';
}

int main()
{
char text[]="SaVE EArtH";
Encrypt(text);
cout << text << endl;

return 0;
}

Show the answer.


@a@E@E#rTH

12 Write the output of the following program


int main()
{
char name[] = "CoMPutER";
for (int x = 0; x < strlen(name);
x++)
if (islower(name [x]))
name [x] =
toupper(name[x]);
else
if (isupper(name[x]))
if (x % 2 == 0)
name[x] =
tolower(name[x]);
else
name[x] =
name[x-1];

cout << name;

return 0;
}
Show the answer.
cOmmUTee

You might also like