75% found this document useful (20 votes)
51K views

100 Most Important C++ Programs (Code Only) PDF

The document contains a list of 100 programming problems/exercises in C++ with brief descriptions. It includes problems related to printing output, calculations, data structures, file handling and more. The programs are intended to be compiled and executed using Dev-C++ compiler.

Uploaded by

idris taskıran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
75% found this document useful (20 votes)
51K views

100 Most Important C++ Programs (Code Only) PDF

The document contains a list of 100 programming problems/exercises in C++ with brief descriptions. It includes problems related to printing output, calculations, data structures, file handling and more. The programs are intended to be compiled and executed using Dev-C++ compiler.

Uploaded by

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

Contents

1. Write a simple program to print your name, class.


2. Write C++ program to calculate sum of two numbers
3. Write C++ Program to print your name 100 times.
4. Write C++ program to swap two numbers using macros
5. Write C++ Program To Accept Student Roll No, Marks in 3 Subjects and
Calculate Total, Average and Print it.
6. Write C++ Program to Read Three Numbers And Print The Biggest Of Given
Three Numbers
7. Write C++ Program to print numeric pyramid
8. Write C++ Program to convert binary number to decimal number
9. Write C++ Program to convert a decimal number to binary number
10. Write program to display number in hexadecimal form
11. Write a program to swap to numbers using a third variable called temp.
12. Write a c++ Program to print ODD numbers from 1 to 10
13. Write a c++ Program to print natural numbers from 1 to 10 in Reverse
14. Write a c++ Program to accept a string in any case and print it by another
case.
15. Write C++ Program to check whether a given number is perfect or not.
16. Write C++ Program to print table of any number
17. Write C++ Program to read ‘n’ number and print them in matrix terms in all
orders.
18. Write C++ Program to accept two numbers and print the sum of given two
numbers by using pointers
19. Write a c++ Program to accept a string in upper case and print it by lower
case.
20. Write a c++ Program to accept any single digit number and print it in words.
21. Write C++ program to reverse a number
22. Write C++ program to find largest number of a list of numbers entered
through keyboard
23. Write C++ Program to calculate and print the sum of even and odd integers of
the first n natural numbers.
24. Write C++ Program to find area of a triangle when there sides are given.
25. Write C++ Program to find whether a number is divisible by ‘11’ or not
without actual division.
26. Write C++ Program to check whether a number is prime number or not
27. Write C++ Program to print following series using function: x + x^3/3! + x^5/5!
+.......+ x^n/n!
28. Write C++ program to find sum of series 1 + 2 + 3 +......+ n
29. Write C++ program to find sum of series 1/2+4/5+7/8+......
30. Write C++ program to find sum of series 1+x+x^2+......+x^n
31. Write C++ program to find sum of series 1^2+3^2+5^2+......+n^2.
32. Write C++ Program to print given series:1 2 4 8 16 32 64 128
33. Write C++ Program to Print following series: 1 -4 7 -10..........-40
34. Write C++ program to swap values of two variables using pass by reference
method
35. Write C++ program to print truth table of XY+Z.
36. Write C++ Program to find First three Pythagorian Triplet.
37. Write C++ Program to Check Whether a Number is Unique Number or Not
38. Write a program to swap to numbers without using a third variable
39. Write C++ program to calculate area of a circle,a rectangle or a triangle
depending upon user's choice
40. Write C++ Program to perform all arithmetic calculation using switch case
41. Write C++ Program to do arithmetic operations according to user choice using
switch case
42. Write a program to check given number is multiple of number entered by
user.
43. Write a program to check given year is leap or not.
44. Write a program to convert gallons to liters.
45. Write a C++ program to find the sum of individual digits of a positive integer.
46. Write a C++ program to the number count of letters in a given text.
47. Write a C++ program to sort a list of numbers in ascending order.
48. Write a C++ program to find both the largest and smallest number in a array
of integers.
49. Write a program to find the largest of two numbers.
50. Write a program to generate square root of 1 to 10.
51. Write a program to find out entered number.
52. Write a program to calculate GCD of two numbers.
53. Write a program to calculate factorial of a number.
54. Write a C++ program to generate all the prime numbers between 1 to n,
where n is a value supplied by the user.
55. Write a C++ program to generate the first n terms of the sequence.
56. Write a C++ program to sort a list of names in ascending order.
57. Write a program to find absolute value of the integer.
58. Write a program for addition of 2 matrices.
59. Write a program to find average of number three numbers
60. Write a program to check whether given string is palindrome or not
61. Write a program to Print ASCII value of Digits, Uppercase and Lowercase
alphabates.
62. Write a C++ Program to raise any number x to a positive power n.
63. Write a C++ Program to convert given inches into equivalent yard, feet and
inches.
64. Write a C++ Program to Find ASCII value of a character.
65. Write C++ Program to reverse all the strings stored in an array
66. Write C++ Program to concatenate two strings
67. Write C++ Program to convert first alphabet of every word in a string from
lowercase to uppercase.
68. Write C++ Program to reverse a string.
69. Write C++ Program to find length of a string.
70. Write C++ Program to Find Substring in String (Pattern Matching)
71. Write C++ Program to Remove Spaces From String
72. Write a C++ Program to find Compound Interest.
73. Write a C++ Program to Convert given no. of days into years, weeks and days
74. Write a C++ program to find cube of a number using macros.
75. Write a C++ program to multiply two matrices.
76. Write a C++ program to insert an element in an array.
77. Write a C++ program to find largest and second largest no from a 2D array.
78. Write a C++ program to do linear search in Array.
79. Write a C++ Program to find element in Array using Binary search
80. Write a C++ Program to find highest and lowest element of a Matrix.
81. Write a C++ Program and Algorithm for Selection Sort.
82. Write a C++ Program and Algorithm for Insertion Sort
83. Write a C++ Program to convert a lowercase alphabet to uppercase or vice-
versa.
84. Write a C++ Program to print three numbers in descending order
85. Write C++ Program to find whether a square matrix is a) symmetric b) skew
symmetric c) none of two.
86. Write a C++ Program to calculate roots of quadratic equation ax^2+bx+c=0
87. Write a C++ Program to find quotient and remainder of two numbers.
88. Write a C++ Program to Find LCM and HCF of two numbers
89. Write a c++ Program to accept a number and check the given number is
Armstrong or not.
90. Write C++ Program to Count Occurrence of a Word in a Text File
91. Write C++ Program to Count no. of alphabates, digits and spaces present in a
file test.TXT
90. Write C++ Program to read from a text file and than write in another text file.
91. Write C++ Program to Count Number of Words, Lines and Total Size of a Text
File
92. Write C++ Program to Remove Spaces from String
93. Write C++ program for overloading binary operators, addition, subtraction,
multiplication, division and comparison
94. Write C++ program to swap two numbers using class
95. Write C++ program to add, subtract, multiply and divide two complex
numbers using structures.
96. Write C++ Program to Compare Two Strings Using Pointers
99. Write C++ program to perform a PUSH operation on a dynamically allocated
stack
100. Write C++ Program for Linked List Representation of Linear Queue
Note: All Above programs are compiled and executed with compiler Dev-c++
5.3.0.4.
1. Write a simple program to print your name, class.

#include <iostream>
using namespace std;
int main()
{
cout << "My name is Bakran Ajas" << endl;
cout << "Class: 11-B ." << endl;
return 0;
}
2. Write C++ program to calculate sum of two numbers

#include<iostream>
using namespace std;
int main()
{
int sum,a,b;
cout<<"Enter value of a:"<<endl;
cin>>a;
cout<<"Enter value of b:"<<endl;
cin>>b;
cout<<"The sum of a and b is "<<(a+b);
return 0;
}
3. Write C++ Program to print your name 100 times.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{

int i;
for(i=1;i<=100;++i)
cout<<"Your Name"<<"\n";
return 0;
}
4. Write C++ program to swap two numbers using macros

#include<iostream>
#include<conio.h>
#define SWAP(a,b) {int temp; temp=a; a=b; b=temp;}
using namespace std;
int main()
{
int x,y;
cout<<"Enter two numbers:";
cin>>x>>y;
cout<<"x="<<x<<" y="<<y;
SWAP(x,y);
cout<<"\nx="<<x<<" y="<<y;
return 0;
}
5. Write C++ Program To Accept Student Roll No, Marks
in 3 Subjects and Calculate Total, Average and Print it.

# include <stdio.h>
#include<iostream>
# include <conio.h>
using namespace std;
int main()

{
int r,b,c,d, tot, avg;
cout<<"ENTER STUDENT ROLL NO ; "<<endl;
cin>>r;
cout<<"ENTER FIRST SUBJECT MARKS ;"<<endl;
cin>>b;
cout<<"ENTER SECOND SUBJECT MARKS;"<<endl;
cin>>c;
cout<<"ENTER THIRD SUBJECT MARKS ;"<<endl;
cin>>d;
tot=b+c+d;
avg=tot/3;
cout<<"\n\n\t\t Lovely Professional University \n\n";
cout<<"\t STUDENT RNO :"<<r<<endl;
cout<<"\t FIRST SUBJECT MARKS :"<<b<<endl;
cout<<"\t SECOND SUBJECT MARKS :"<<c<<endl;
cout<<"\t THIRD SUBJECT MARKS :"<<d<<endl;
cout<<"\t AVERAGE MARKS :"<<avg<<endl;
return 0;
}
6. Write C++ Program to Read Three Numbers And Print
The Biggest Of Given Three Numbers

# include <stdio.h>
#include<iostream>
# include <conio.h>
using namespace std;
int main( )
{
int a,b,c,big=0;
cout<<"ENTER VALUE FOR A:";
cin>>a;
cout<<"ENTER VALUE FOR B:";
cin>>b;
cout<<"ENTER VALUE FOR C:";
cin>>c;
if (a>big)
big=a ;
if(b>big)
big=b;
if (c>big)
big=c;
cout<<"BIGGEST OF ABOVE GIVEN THREE NUMBER IS "<<big;
return 0;
}
7. Write C++ Program to print numeric pyramid

# include <stdio.h>
#include<iostream>
# include <conio.h>
using namespace std;
main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
cout<<j;
cout<<"\n";
}
return 0;
}
8. Write C++ Program to convert binary number to
decimal number

#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main()
{
unsigned long i,n,num=0,d;
cout<<"Enter any Binary number:";
cin>>n;
cout<<"\nThe Decimal conversion of "<<n<<" is ";
for(i=0;n!=0;++i)
{
d=n%10;
num=(d)*(pow(2,i))+num;
n=n/10;
}
cout<<num;
return 0;
}
9. Write C++ Program to convert a decimal number to
binary number

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int d,n,i,j,a[50];
cout<<"Enter a number:";
cin>>n; cout<<"\nThe binary conversion of "<<n<<" is 1";
for(i=1;n!=1;++i)
{
d=n%2;
a[i]=d;
n=n/2;
}
for(j=i-1;j>0;--j)
cout<<a[j];
return 0;
}
10. Write program to display number in hexadecimal
form

#include <iostream.h>
int main()
{
int x;
cout << Enter an integer:" << endl;
cin >> x;
// hex keyword displays a number in hexadecimal form.
cout << "x=" << hex << x << endl;
cin.get();
return 0;
}
11. Write a program to swap to numbers using a
third variable called temp.

#include <iostream.h>
int main()
{
int a = 40;
int b = 20;
int temp;
cout << "Value of a (before swap): " << a << endl;
cout << "Value of b (before swap): " << b << endl;
temp=a;
a=b;
b=temp;
cout << "Value of a (after swap): " << a << endl;
cout << "Value of b (after swap): " << b << endl;
return 0;
}
12. Write a c++ Program to print ODD numbers from 1 to 10

# include <iostream>
using namespace std;
int main( )
{
int i;
for (i=1; i<=10; i+=2)
cout<<i<<" ";
return 0;
}
13. Write a c++ Program to print natural numbers from 1 to
10 in Reverse

# include <iostream>
using namespace std;
int main( )
{
int i;
for (i=10; i>=1; i--)
cout<<i<<" ";
return 0;
}
14. Write a c++ Program to accept a string in any case and
print it by another case.

# include <iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
int main( )
{
char ch;
cout<<"enter a string :";
while(( ch=getchar( ))!='\n')
{
if(ch>='A' && ch<='Z')
putchar(ch+32);
else
if(ch>='a' && ch<='z')
putchar(ch-32);
else
putchar(ch);
}
cout<<"is the string";
return 0;
}
15. Write C++ Program to check whether a given number is
perfect or not.
# include <stdio.h>
#include<iostream>
# include <conio.h>
using namespace std;
int main( )
{
int i,n,s=0;
cout<<"enter the number";
cin>>n;
for(i=1;i<n/2;i++)
if(n%i==0)
s+=i;
if(s==n)
cout<<"the number is perfect no";
else
cout<<"the number is not perfect ";
return 0;
}
16. Write C++ Program to print table of any number

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int i,n;
cout<<"Enter number for which you want to generate table:";
cin>>n;
cout<<"\n\n";
for(i=1;i<=10;++i)
cout<<"\t"<<n<<"*"<<i<<"="<<n*i<<"\n";
return 0;
}
17. Write C++ Program to read ‘n’ number and print them in
matrix terms in all orders.

# include <stdio.h>
#include<iostream>
# include <conio.h>
using namespace std;
int main( )
{
int i,n,c,p,q,r,k,a[20];
cout<<"enter the array size";
cin>>n;
cout<<"enter the elements";
for(i=1;i<=n;i++)
cin>>a[i];
i=1;
while(i<=n)
{
if(n%i==0)
{
r=i;
c=n/i;
k=1;
for(p=1;p<=r;p++)
{
for(q=1;q<=c;q++)
cout<<a[k++];
cout<<"\n";
}
i++;
return 0;
}
}
}
18. Write C++ Program to accept two numbers and print the
sum of given two numbers by using pointers

# include <stdio.h>
#include<iostream>
# include <conio.h>
using namespace std;
int main( )
{
int a, b,c;
a=10;
b=20;
c=*(&a)+*(&b);
cout<<c;
return 0;
}
19. Write a c++ Program to accept a string in upper case
and print it by lower case.

# include <stdio.h>
# include <conio.h>
# include <iostream>
using namespace std;
int main( )
{
char ch,c;
cout<<"enter a string in upper case:";
while(( ch=getchar( ))!='\n')
{
c=ch+32;
putchar(c);
}
cout<<" is in lower case";
return 0;
}
20. Write a c++ Program to accept any single digit number
and print it in words.

# include <stdio.h>
# include <conio.h>
# include <iostream>
using namespace std;
int main( )
{
int n;
cout<<"enter a number :";
cin>>n;
switch(n)
{
case 0: cout<<"ZERO";
break;
case 1: cout<<"ONE";
break;
case 2: cout<<"TWO";
break;
case 3: cout<<"THREE";
break;
case 4: cout<<"FOUR";
break;
case 5: cout<<"FIVE";
break;
case 6: cout<<"SIX";
break;
case 7: cout<<"SEVEN";
break;
case 8: cout<<"EIGHT";
break;
case 9cout<<"NINE";
break;
default:
cout<<"please enter the number between 0 and 9";
}
return 0;
}
21. Write C++ program to reverse a number

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
long n,rev=0,d;
cout<<"Enter the number:";
cin>>n;
while(n!=0)
{
d=n%10;
rev=(rev*10)+d;
n=n/10;
}
cout<<"The reversed number is "<<rev;
return 0;
}
22. Write C++ program to find largest number of a list of
numbers entered through keyboard

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int i,n,x,large=0;
cout<<"How many numbers?";
cin>>n;
for(i=0;i<n;++i)
{
cout<<"\nEnter number "<<i+1<<":";
cin>>x;
if(x>large)
large=x;
}
cout<<"\n\nThe largest number is "<<large;
return 0;
}
23. Write C++ Program to calculate and print the sum of
even and odd integers of the first n natural numbers.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n,i,sumeven=0,sumodd=0;
cout<<"Enter value of n:";
cin>>n;
for(i=1;i<=n;++i)
{
if(i%2==0)
sumeven+=i;
else
sumodd+=i;
}
cout<<"\nSum of even Numbers is "<<sumeven;
cout<<"\nSUm of odd Numbers is "<<sumodd;
return 0;
}
24. Write C++ Program to find area of a triangle when there
sides are given.

# include <stdio.h>
#include<iostream>
# include <conio.h>
#include<math.h>
using namespace std;
int main( )
{
int a,b,c;
float s, area;
cout<<"enter there sides of the triangle";
cin>>a>>b>>c;
if((a+b)<c||(b+c)<a||(a+c)<b)
cout<<"finding area is not possible";
else
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"area="<<area;
return 0;
}
25. Write C++ Program to find whether a number is divisible
by ‘11’ or not without actual division.

#include<stdio.h>
#include<conio.h>
#include<iostream>
#include<math.h>
using namespace std;
int main( )
{
int a,b,n,evensum=0,oddsum=0,div;
cout<<"enter a number";
cin>>n;
a=n;
b=n/10;
while(a>0)
{
oddsum=oddsum+(a%10);
a=a/10;
}
while(b>0)
{
evensum=evensum+(b%10);
b=b/10;
}
div=abs(evensum-oddsum);
if(div%11==0)
cout<<"The number is divisible by 11";
else
cout<<"The number is not divisible by 11";
return 0; }
26. Write C++ Program to check whether a number is prime
number or not

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n,i,flag=1;
cout<<"Enter any number:";
cin>>n;
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
flag=0;
break;
}
}
if(flag)
cout<<"\n"<<n<<" is a Prime number";
else
cout<<"\n"<<n<<" is not a Prime number";
return 0;
}
27. Write C++ Program to print following series using
function: x + x^3/3! + x^5/5! +.......+ x^n/n!

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x,n;
double sum(int,int);
double res;
cout<<"x + x^3/3! + x^5/5! +.......+ x^n/n!";
cout<<"\n\nEnter value of x and n:";
cin>>x>>n;
res=sum(x,n);
cout<<"\nSum of series is "<<res;
getch();
}
double sum(int a,int b)
{
long power(int,int);
int i,j;
double s=0,fac=1;
long p;
for(i=1;i<=b;i+=2)
{
p=power(a,i);
for(j=1;j<=i;++j)
{
fac*=j;
}
s+=p/fac;
fac=1;
}
return(s);
}
long power(int x,int i)
{
long res=1,j;
for(j=1;j<=i;++j)
{
res*=x;
}
return(res);
}
28. Write C++ program to find sum of series 1 + 2 + 3 +......+
n

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int i,n,sum=0;
cout<<"1+2+3+......+n";
cout<<"\nEnter the value of n:";
cin>>n;
for(i=1;i<=n;++i)
sum+=i;
cout<<"\nSum="<<sum;
return 0;
}
29. Write C++ program to find sum of series
1/2+4/5+7/8+......

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int i,n;
float sum=0,x,a=1;
cout<<"1/2+4/5+7/8+......";
cout<<"\n\nHow many terms(ex: 1,2,3...n)?";
cin>>n;

for(i=0;i<n;++i)
{
x=a/(a+1);
sum+=x;
a+=3;
}
cout<<"\nSum="<<sum;
return 0;
}
30. Write C++ program to find sum of series
1+x+x^2+......+x^n

#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main()
{
long i,n,x,sum=1;
cout<<"1+x+x^2+......+x^n";
cout<<"\n\nEnter the value of x and n:";
cin>>x>>n;
for(i=1;i<=n;++i)
sum+=pow(x,i);
cout<<"\nSum="<<sum;
return 0;
}
31.Write C++ program to find sum of series
1^2+3^2+5^2+......+n^2.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int n,i;
long sum=0;
cout<<"1^2+3^2+5^2+......+n^2\n\n Enter Value of n:";
cin>>n;
for(i=1;i<=n;i+=2)
sum+=(i*i);
cout<<"\n Sum of given series is "<<sum;
return 0;
}
32.Write C++ Program to print given series:1 2 4 8 16 32
64 128

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int i;
for(i=1;i<=128;i*=2)
cout<<i<<" ";
return 0;
}
33.Write C++ Program to Print following series: 1 -4 7 -
10..........-40

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int i,a=-1,b;
for(i=1;i<=40;i+=3)
{
a*=-1;
b=i;
b*=a;
cout<<b<<" ";
}
return 0;
}
34.Write C++ program to swap values of two variables
using pass by reference method

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a,b;
void swap(int &,int &);
cout<<"Enter two values:";
cin>>a>>b;
cout<<"\nBefor swapping:\na="<<a<<"\tb="<<b;
swap(a,b);
cout<<"\n\nAfter swapping:\na="<<a<<"\tb="<<b;
return 0;
}
void swap(int & x,int & y)
{
int temp;
temp=x;
x=y;
y=temp;
}
35. Write C++ program to print truth table of XY+Z.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x,y,z;
cout<<"X\tY\tZ\tXY+Z";
for(x=0;x<=1;++x)
for(y=0;y<=1;++y)
for(z=0;z<=1;++z)
{
if(x*y+z==2)
cout<<"\n\n"<<x<<"\t"<<y<<"\t"<<z<<"\t1";
else
cout<<"\n\n"<<x<<"\t"<<y<<"\t"<<z<<"\t"
<<x*y+z;
}
return 0;
}
36.Write C++ Program to find First three Pythagorian
Triplet.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x,y,z;
cout<<"X\tY\tZ\tXY+Z";
for(x=0;x<=1;++x)
for(y=0;y<=1;++y)
for(z=0;z<=1;++z)
{
if(x*y+z==2)
cout<<"\n\n"<<x<<"\t"<<y<<"\t"<<z<<"\t1";
else
cout<<"\n\n"<<x<<"\t"<<y<<"\t"<<z<<"\t"
<<x*y+z;
}
return 0;
}
37.Write C++ Program to Check Whether a Number is
Unique Number or Not

#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
long num;
char str[10];
int a[10]={0},flag=1,i=0;
cout<<"Enter any number:";
cin>>num;
itoa(num,str,10); //convert number to character array
while(str[i]!='\0')
{
switch(str[i])
{
case '0': a[0]++;
break;
case '1': a[1]++;
break;
case '2': a[2]++;
break;
case '3': a[3]++;
break;
case '4': a[4]++;
break;
case '5': a[5]++;
break;
case '6': a[6]++;
break;
case '7': a[7]++;
break;
case '8': a[8]++;
break;
case '9': a[9]++;
break;
}
i++;
}

for(i=0;i<10;i++)
{
if(a[i]>1)
{
flag=0;
break;
}
}

if(flag)
cout<<"\nNumber is Unique";
else
cout<<"\nNumber is Not Unique";

return 0;

}
38.Write a program to swap to numbers without using a
third variable

#include <iostream.h>
int main()
{
int a = 80;
int b = 20;
cout << "Value of a (before swap): " << a << endl;
cout << "Value of b (before swap): " << b << endl;
a = a + b;
b = a - b;
a = a - b;
cout << "Value of a (after swap): " << a << endl;
cout << "Value of b (after swap): " << b << endl;
cin.get();
return 0;
}
39.Write C++ program to calculate area of a circle,a
rectangle or a triangle depending upon user's choice

#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main()
{
float a,b,c,s,r,area;
int ch;
cout<<"***Menu***\n1.Area of circle\n2.Area of Rectangle";
cout<<"\n3.Area of triangle\nEnter your choice:";
cin>>ch;

switch(ch)
{
case 1:
{
cout<<"\nEnter radius of the circle:";
cin>>r;
area=3.14*r*r;
break;
}
case 2:
{
cout<<"\nEnter length and breadth:";
cin>>a>>b;
area=a*b;
break;
}
case 3:
{
cout<<"\nEnter three sides of the triangle:";
cin>>a>>b>>c;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
break;
}
default: cout<<"\nWrong choice...!!!";
break;
}

cout<<"Area="<<area;
return 0;
}
40.Write C++ Program to perform all arithmetic
calculation using switch case

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{

float a,b,res;
int ch,q;
cout<<"Arithmetic Operatios";
cout<<"\n\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Mode";
cout<<"\n Enter your choice:";
cin>>ch;

switch(ch)
{
case 1:
{
cout<<"\n\nEnter two variables:";
cin>>a>>b;
res=a+b;
cout<<"\n Result="<<res;
}
break;
case 2:
{
cout<<"\n\nEnter two variables:";
cin>>a>>b;
res=a-b;
cout<<"\n Result="<<res;
}
break;

case 3:
{
cout<<"\n\nEnter two variables:";
cin>>a>>b;
res=a*b;
cout<<"\n Result="<<res;
}
break;

case 4:
{
cout<<"\n\nEnter two variables:";
cin>>a>>b;
if(a>=b)
{
res=a/b;
cout<<"\n Result="<<res;
}
else
cout<<"\n\n\t1st varable should be greater than 2nd.!!!";
}
break;

case 5:
{
cout<<"\n\nEnter two variables:";
cin>>a>>b;
if(a>=b)
{
q=a/b;
res=a-(b*q);
cout<<"\n Result="<<res;
}
else
cout<<"\n\n\t1st variable should be greater than 2nd..!!!";
}
break;
}

return 0;
}
41.Write C++ Program to do arithmetic operations
according to user choice using switch case

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a,b;
char c;
cout<<"Enter any expression(ex:3*7):";
cin>>a>>c>>b;

switch(c)
{
case'+': cout<<"\nResult:"<<a+b;
break;

case'-': cout<<"\nResult:"<<a-b;
break;

case'*': cout<<"\nResult:"<<a*b;
break;

case'/': cout<<"\nResult:"<<a/b;
break;
case'%': cout<<"\nResult:"<<a%b;
break;
}
return 0; }
42. Write a program to check given number is multiple of
number entered by user.

//lets say given number is 78.


#include <iostream.h>
int main()
{
int check_num;
int num=78;
cout << Enter an integer:";
cin >> check_num;

// use % operator to check remainder


if(check_num%78)
cout<<num<<"is not multiple of "<<check_num<<endl;
else
cout<<num<<"is multiple of "<<check_num<<endl;

return 0;
}
43. Write a program to check given year is leap or not.
//conditions for leap year are as follows
// leap years occur in years exactly divisible by four,
// except that years ending in 00 are leap years
// only if they are divisible by 400.
#include <iostream.h>
#include <conio.h>
int main()
{
int year;
cout << "Enter a year (e.g. 2004): ";
cin >> year;
if ((year % 400 == 0) || ((year %100 != 0) && (year % 4 == 0)))
cout << "The year " << year << " is a leap year" << endl;
else
cout << "The year " << year << " is NOT a leap year" << endl;
getch();
return 0;
}
44. Write a program to convert gallons to liters.

#include <iostream.h>
int main()
{
float gallons, liters;
cout << "Enter number of gallons: ";
cin >> gallons; // Read the inputs from the user
liters = gallons * 3.7854; // convert to liters
cout << "Liters: " << liters << endl;
return 0;
}
45. Write a C++ program to find the sum of individual digits
of a positive integer.

#include<iostream.h>
#include<conio.h>
int main()
{
int num, reminder, sum=0;
cout<<"Enter the required number:";
cin>>num;
while(num>0)
{
reminder=num%10;
sum=sum+reminder;
num=num/10;
}
cout<<"Sum of individual digits of a positive integer is:"<< sum;
return 0;
}
46. Write a C++ program to the number count of letters in a
given text.
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
int main( )
{
char str[80];
int i, c1=0,c2=0;
cout<<"Enter the text:";
gets(str);
for(i=0; str[i]!='\0'; i++)
{
c1++;
if(str[i]==' ')
c2;
}
cout<<"Total Letters present in the given text :"<< c1-c2;
return 0;
}
47. Write a C++ program to sort a list of numbers in
ascending order.

#include<iostream>
#include<conio.h>
using namespace std;
int main( )
{
int a[10], n, i, j, temp;
cout<<"Enter the no. of elements:";
cin>> n;
cout<<"Enter the array elements:";
for(i=0; i< n; i++)
cin>>a[i];
for( i=0; i< n; i++)
{
for(j=i; j< n-1; j++)
{
if(a[i]> a[j+1] )
{
temp= a[i];
a[i]= a[j+1];
a[j+1]= temp;
}
}
}
cout<<"Elements after sorting:";
for( i=0; i< n; i++)
cout<< a[i]<<" ";
return 0;
}
48. Write a C++ program to find both the largest and
smallest number in a array of integers.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a[100], max, min, i, n;
cout<<"Enter number elements in the array? :";
cin>>n;
for( i=0; i< n ; i++)
{
cout<<"Enter the numbers: ";
cin>>a[ i ];
if(i==0)
{ max=a[i ];
min=a[i ]; }
if(a[ i]>max)
max= a[i ];
if(a[i ]< min)
min= a[i ];
}
cout<<"Maximum : "<< max<<endl;
cout<<"Minimum : "<< min;
getch( );
}
49. Write a program to find the largest of two numbers.

#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
cin >> b;
if(a < b)
cout << "First number is less than second.\n";
return 0;
}
50. Write a program to generate square root of 1 to 10.

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int num;
double sqroot;
for(num=1; num < 10; num++) {
sqroot = sqrt((double) num); //casting num from integer to double

cout << num << " " << sqroot << '\n';
}
return 0;
}
51. Write a program to find out entered number.

#include <iostream>
using namespace std;
int main()
{
int choice;
cout << "Enter an integer number: 1 - 5 " ;
cin >> choice;
switch (choice)
{
case 1:
cout << "You entered 1.";
break;
case 2:
cout << "You entered 2.";
break;
case 3:
cout << "You entered 3.";
break;
case 4:
cout << "You entered 4.";
break;
case 5:
cout << "You entered 5.";
break;
default:
cout << "Invalid input.";
}

return 0; }
52. Write a program to calculate GCD of two numbers.

#include<iostream>
using namespace std;
int gcd (int, int); //func. declaration.
int main( )
{
int a, b, ans;
cout<<"Enter the two integer values:";
cin>> a >>b;
ans= gcd(a, b); // calling function.
cout<<"GCD for given numbers is :" << ans;
return 0;
}
int gcd( int x, int y) //called function.
{
int z;
z=x%y;
if(z==0)
return y;
gcd(y,z); //recursive function
}
53. Write a program to calculate factorial of a number.

#include<iostream>
using namespace std;
int fact( int); // function declaration
int main( )
{
int num,result;
cout<<"Enter the required number:";
cin>>num;
result = fact( num);
cout<<"Factorial is :" << result;
return 0;
}
int fact(int n)
{
int ft;
for( ft=1; n>=1; n--)
ft=ft*n;
return ft;
}
54. Write a C++ program to generate all the prime numbers
between 1 to n, where n is a value supplied by the user.

#include<iostream>
#include<conio.h>
using namespace std;
int main( )
{
int n, x, flag,ct;

cout<<"Enter the n value:";


cin>> n;
cout<<"Prime Numbers:";
for( ct=1; ct<=n; ct++)
{
x=2; flag=0;
while(x<=ct/2)
{
if(ct%x==0) { flag=1; break; }
x++;
}
if(flag==0)
cout<< ct<<" ";
}
return 0;
}
55. Write a C++ program to generate the first n terms of the
sequence.

#include<iostream>
#include<conio.h>
using namespace std;
int main( )
{
int frst=0, sec=1, t, n, ct;
cout<<"Enter the no. of terms:";
cin>>n;
cout<<"Fibonacci series:";
cout<< frst <<" "<< sec<<" ";
for( ct=3; ct<=n; ct++)
{
t=frst+sec;
cout<< t<<" ";
frst=sec;
sec=t;
}
return 0;
}
56. Write a C++ program to sort a list of names in ascending
order.

#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
char st[10][10],temp[10];
int i, j, n;
cout<<"Enter the no. of names:";
cin>>n;
cout<<"Enter the different names:";
for(i=0; i< n; i++)
cin>>st[i];
for(i=0; i< n; i++)
{
for(j=i; j< n-1; j++)
{
if(strcmp(st[i], st[j+1]) >0)
{
strcpy(temp,st[i]);
strcpy(st[i],st[j+1]);
strcpy(st[j+1],temp);
}
}
}
cout<<"Given names after ascending order:";
for(i=0;i<5;i++)
cout<< st[i] <<" ";
return 0;
}
57. Write a program to find absolute value of the integer.

#include <iostream>
using namespace std;
int main()
{
int number;
int abs_number;
cout << "Enter an integer (positive or negative): ";
cin >> number;
if(number >= 0)
{
abs_number = number;
}
else
abs_number = -number;
cout << "The absolute value of " << number << " is " <<
abs_number;
cout << endl;
return 0;
}
58. Write a program for addition of 2 matrices.

#include<iostream>
using namespace std;
main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
cout << "Enter the number of rows and columns of matrix ";
cin >> m >> n;
cout << "Enter the elements of first matrix\n";
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
cin >> first[c][d];
cout << "Enter the elements of second matrix\n";
for ( c = 0 ; c < m ;c++ )
for ( d = 0 ; d < n ; d++ )
cin >> second[c][d];
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];
cout << "Sum of entered matrices:-\n";
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
cout << sum[c][d] << "\t";
cout << endl;
}
return 0;
}
59. Write a program to find average of number three
numbers

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a,b,c,d;
cout<<"Enter the first no. "<<endl;
cin>>a;
cout<<"Enter the second no. "<<endl;
cin>>b;
cout<<"Enter the third no. "<<endl;
cin>>c;
d=(a+b+c)/3 ;
cout<<"The average of the 3 numbers is"<<d<<endl;
return 0;
}
60. Write a program to check whether given string is
palindrome or not
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int i,j,len,flag=1;
char a[20];
cout<<"Enter a string:";
cin>>a;
for(len=0;a[len]!='\0';++len);
for(i=0,j=len-1;i<len/2;++i,--j)
{
if(a[j]!=a[i])
flag=0;
}
if(flag==1)
cout<<"\nThe string is Palindrome";
else
cout<<"\nThe string is not Palindrome";
return 0;
}
61. Write a program to Print ASCII value of Digits, Uppercase
and Lowercase alphabates.

#include<conio.h>
#include<iostream>
#include<dos.h>
#include<process.h>
using namespace std;
int main()
{
char ch,a[]={"I just love to Learn Programming "};
int j=0;
cout<<"Uppercase Alphabates\n\n";
for(int i=65;i<91;++i)
{
j++;
ch=i;
cout<<ch<<":"<<i<<"\t";
if(j==10)
{
cout<<"\n";
j=0;
}
}
j=0;
cout<<"\n\n\nLowercase Alphabates\n\n";
for(int i=97;i<123;++i)
{
j++;
ch=i;
cout<<ch<<":"<<i<<"\t";
if(j==10)
{
cout<<"\n";
j=0;
}
}
cout<<"\n\n\nDigits\n\n";
for(int i=48;i<58;i++)
{
ch=i;
cout<<ch<<":"<<i<<"\t";
}
cout<<"\n\n\n\n\t\t";
for(int i=0;a[i]!='\0';++i)
{
cout<<a[i];

}
return 0;
}
62. Write a C++ Program to raise any number x to a positive
power n.

#include<iostream>
#include<conio.h>
#include<math.h> //for pow() function
using namespace std;
int main()
{
int x,n,res;
cout<<"Enter value of x and n:";
cin>>x>>n;
res=pow(x,n);
cout<<"\nResult="<<res;
return 0;
}
63. Write a C++ Program to convert given inches into
equivalent yard, feet and inches.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int y,f,i;
cout<<"Enter inches:";
cin>>i;
y=i/432;
i=i%432;
f=i/12;
i=i%12;
cout<<"Yard="<<y<<"\nFeet="<<f<<"\nInches="<<i;
return 0;
}
64. Write a C++ Program to Find ASCII value of a character.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char ch,c;
int cha;
cout<<"Enter a character:";
cin>>ch;
cha=ch;
cout<<"\nASCII value of "<<ch<<" is "<<cha;
return 0;
}
65. Write C++ Program to reverse all the strings stored in an
array

#include<iostream>
#include<conio.h>
#include<string.h>
#include<stdio.h>
using namespace std;
int main()
{
char a[3][50];
int i,j,k,len;
cout<<"Enter 3 strings:\n";
for(i=0;i<3;i++)
{
gets(a[i]);
}
cout<<"\nThe list of orignal strings:\n" ;
for(i=0;i<3;i++)
{
cout<<a[i]<<"\n";
}
cout<<"\nThe list of changed string:\n";

for(i=0;i<3;i++)
{
len=strlen(a[i]);
for(j=0,k=len-1;k>=0;j++,k--)
{
cout<<a[i][k];
}
cout<<"\n";
}
return 0;
}
66. Write C++ Program to concatenate two strings

#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
int main()
{
char str1[30],str2[30],str3[60];
int i,j;
cout<<"Enter first string:";
gets(str1);
cout<<"\nEnter second string:";
gets(str2);
for(i=0;str1[i]!='\0';++i)
str3[i]=str1[i];
for(j=0;str2[j]!='\0';++j)
str3[i+j]=str2[j];
str3[i+j]='\0';
cout<<"\nThe concatenate string is "<<str3;
return 0;
}
67. Write C++ Program to convert first alphabet of every
word in a string from lowercase to uppercase.

#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
using namespace std;
int main()
{
char a[50];
int i;
cout<<"Enter a string:";
gets(a);
cout<<"\n";
if(islower(a[0]))
a[0]=toupper(a[0]);
for(i=0;a[i]!='\0';++i)
{
if(a[i]==' ')
if(islower(a[i+1]))
a[i+1]=toupper(a[i+1]);
}
cout<<"The new string is:"<<a;
return 0;
}
68. Write C++ Program to reverse a string.

#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
int main()
{
char a[20],a1[20];
int i,j;
cout<<"Enter any String:"<<"\n";
gets(a); cout<<"Reverse of the string is: ";
for(i=0;a[i]!='\0';++i);
for(j=i-1;j>=0;--j)
cout<<a[j];
return 0;
}
69. Write C++ Program to find length of a string.

#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
int main()
{
char a[30];
int i;
cout<<"Enter a string:";
gets(a);
for(i=0;a[i]!='\0';++i);
cout<<"\nLenth of the sting '"<<a<<"' is "<<i;
return 0;
}
70. Write C++ Program to Find Substring in String (Pattern
Matching)

#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int i,j,temp;
char str[100]={"This is a pattern matching"};
char substr[20]={"pattern"};
for(i=0;str[i]!='\0';i++)
{
j=0;
if(str[i]==substr[j])
{
temp=i+1;
while(str[i]==substr[j])
{
i++;
j++;
}

if(substr[j]=='\0')
{
cout<<"The substring is present in given string at position "
<<temp<<"\n";
exit(0);
}
else
{
i=temp;
temp=0;
}
}
}

if(temp==0)
cout<<"The substring is not present in given string\n";
return 0;
}
71. Write C++ Program to Remove Spaces From String

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int i,j=0;
char str[30];
cout<<"Enter a String:\n";
gets(str);
for(i=0;str[i]!='\0';++i)
{
if(str[i]!=' ')
str[j++]=str[i];
}
str[j]='\0';
cout<<"\nString After Removing Spaces:\n"<<str;
return 0;
}
72. Write a C++ Program to find Compound Interest.

#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main()
{
float p,r,t,ci;
cout<<"Enter Principle,Rate and Time ";
cin>>p>>r>>t;
ci=pow(p*(1+r/100),t);
cout<<"\n"<<"Compound Interest = "<<ci<<"%";
return 0;
}
73. Write a C++ Program to Convert given no. of days into
years, weeks and days

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int y,d,w;

cout<<"Enter No. of days:";


cin>>d;
y=d/365;
d=d%365;
w=d/7;
d=d%7;
cout<<"\nYears: "<<y<<"\nWeeks: "<<w<<"\nDays: "<<d;
return 0;
}
74. Write a C++ program to find cube of a number using
macros.

#include<iostream>
#include<conio.h>
#define CUBE(x) (x*x*x)
using namespace std;
int main()
{
int n,cube;
cout<<"Enter a number:";
cin>>n;
cube=CUBE(n);
cout<<"Cube="<<cube;
return 0;
}
75. Write a C++ program to multiply two matrices.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{

int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
cout<<"Enter rows and columns of first matrix:";
cin>>m>>n;
cout<<"Enter rows and columns of second matrix:";
cin>>p>>q;
if(n==p)//condition for matrix multiplication.
{
cout<<"\nEnter first matrix:\n";
for(i=0;i<m;++i)
for(j=0;j<n;++j)
cin>>a[i][j];
cout<<"\nEnter second matrix:\n";
for(i=0;i<p;++i)
for(j=0;j<q;++j)
cin>>b[i][j];
cout<<"\nThe new matrix is:\n";
for(i=0;i<m;++i)
{
for(j=0;j<q;++j)
{
c[i][j]=0;
for(k=0;k<n;++k)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
cout<<c[i][j]<<" ";
}
cout<<"\n";
}
}
else
cout<<"\nSorry!!!! Matrix multiplication can't be done, condition not
satisfied";
return 0;
}
76. Write a C++ program to insert an element in an array.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a[20],n,x,i,pos=0;
cout<<"Enter size of array:";
cin>>n;
cout<<"Enter the array in ascending order:\n";
for(i=0;i<n;++i)
cin>>a[i];
cout<<"\nEnter element to insert:";
cin>>x;
for(i=0;i<n;++i)
if(a[i]<=x&&x<a[i+1])
{
pos=i+1;
break;
}
for(i=n+1;i>pos;--i)
a[i]=a[i-1];
a[pos]=x;
cout<<"\n\nArray after inserting element:";
for(i=0;i<n+1;i++)
cout<<a[i]<<" ";
return 0;
}
77. Write a C++ program to find largest and second largest
no from a 2D array.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a[5][5],big1,big2,n,m,i,j;
cout<<"Enter no of rows and columns(max 5):";
cin>>m>>n;
cout<<"Enter the array:\n";
for(i=0;i<m;i++)
for(j=0;j<n;++j)
cin>>a[i][j];
big1=a[0][0];
for(i=0;i<m;++i)
for(j=0;j<n;++j)
{
if(a[i][j]>big1)
big1=a[i][j];
}
big2=a[0][0];
for(i=0;i<m;++i)
for(j=0;j<n;++j)
{
if(a[i][j]>big2&&a[i][j]<big1)
big2=a[i][j];
}
cout<<"\n\nLargest number:"<<big1;
cout<<"\nSecond largest number:"<<big2;
return 0;
}
78. Write a C++ program to do linear search in Array.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a[20],n,x,i,flag=0;
cout<<"How many Elements?";
cin>>n;
cout<<"\nEnter Elements of the Array\n";
for(i=0;i<n;++i)
cin>>a[i];
cout<<"\nEnter Element to search:";
cin>>x;
for(i=0;i<n;++i)
{
if(a[i]==x)
{
flag=1;
break;
}
}
if(flag)
cout<<"\nElement is Found at position "<<i+1;
else
cout<<"\nElement not found";
return 0;
}
79. Write a C++ Program to find element in Array using
Binary search

#include<conio.h>
#include<iostream>
using namespace std;
int main()
{
int search(int [],int,int);
int n,i,a[100],e=-3,res;
cout<<"How Many Elements:";
cin>>n;
cout<<"\nEnter Elements of Array in Accending order\n";
for(i=0;i<n;++i)
{
cin>>a[i];
}
cout<<"\nEnter element to search:";
cin>>e;
res=search(a,n,e);
if(res!=0)
cout<<"\nElement is Founded at "<<res+1<<"st position";
else
cout<<"\nElement is not found....!!!";
getch();
}
int search(int a[],int n,int e)
{
int f,l,m;
f=0;
l=n-1;
while(f<=l)
{
m=(f+l)/2;
if(e==a[m])
return(m);
else
if(e>a[m])
f=m+1;
else
l=m-1;
}
return 0;
}
80. Write a C++ Program to find highest and lowest element
of a Matrix.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int m,n,a[10][10],i,j,high,low;
cout<<"Enter no. of rows and coloumns:";
cin>>m>>n;
cout<<"\nEnter matrix:\n";
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
cin>>a[i][j];
}

for(i=0;i<m;++i)
{
high=a[0][0];
low=a[0][0];
for(j=0;j<n;++j)
{
if(a[i][j]>high)
high=a[i][j];
else
if(a[i][j]<low)
low=a[i][j];
}
}
cout<<"\nHeighst Element:"<<high<<"\nLowest Element:"
<<low<<"\n";
return 0;
}
81. Write a C++ Program and Algorithm for Selection Sort.

#include<iostream>
using namespace std;
int main()
{
int i,j,n,loc,temp,min,a[30];
cout<<"Enter the number of elements:";
cin>>n;
cout<<"\nEnter the elements\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}

for(i=0;i<n-1;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
min=a[j];
loc=j;
}
}
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
cout<<"\nSorted list is as follows\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
return 0;

}
82. Write a C++ Program and Algorithm for Insertion Sort

#include<iostream>
using namespace std;
int main()
{
int i,j,n,temp,a[30];
cout<<"Enter the number of elements:";
cin>>n;
cout<<"\nEnter the elements\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=1;i<=n-1;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j]; //moves element forward
j=j-1;
}

a[j+1]=temp; //insert element in proper place


}
cout<<"\nSorted list is as follows\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
return 0;
}
83. Write a C++ Program to convert a lowercase alphabet to
uppercase or vice-versa.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{

char ch;
cout<<"Enter any Alphabet:";
cin>>ch;
if(ch>='a'&&ch<='z')
{
cout<<"\n\tYou have entered a lowercase alphabet";
ch=ch-32;

cout<<"\n\nThe uppercase alphabet is "<<ch;


}
else
{
cout<<"\n\tYou have entered an Uppercase alphabet";
ch=ch+32;
cout<<"\n\nThe lowercase alphabet is "<<ch;
}
return 0;
}
84. Write a C++ Program to print three numbers in
descending order

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{

int a,b,c,big1,big2,big3;
cout<<"Enter three numbers:";
cin>>a>>b>>c;

big1=a;
if(b>big1)
big1=b;
else
if(c>big1)
big1=c;
if(big1==a)
{
if(b>c)
{
big2=b;
big3=c;
}
else
{
big2=c;
big3=b;
}
}
else
{
if(big1==b)
if(a>c)
{
big2=a;
big3=c;
}
else
{
big2=c;
big3=a;
}
else
{
if(a>b)
{
big2=a;
big3=b;
}
else
{
big2=b;
big3=a;
}
}
}
cout<<"\n\n\tNumbers in descending order......\n\t\t";
cout<<big1<<" "<<big2<<" "<<big3;
return 0; }
85. Write C++ Program to find whether a square matrix is a)
symmetric b) skew symmetric c) none of two.

# include <stdio.h>
#include<iostream>
# include <conio.h>
using namespace std;
int main( )

int a[10][10],i,j,m,n,c=0,c1=0;
cout<<"enter the array size";
cin>>n;
cout<<"enter the elements";
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
cin>>a[i][j];
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
if(a[i][j]==a[j][i])
c=1;
else
if(a[i][j]==a[j][i])
c1=1;
}
cout<<"the given matrix is \n";
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
cout<<"%4d"<<a[i][j];
cout<<"\n";
}
if(c==0)
cout<<"the given matrix is symmetric";
else
if(c1==0)
cout<<"the matrix is skew symmetric";
else
cout<<"none of two";
return 0;
}
86. Write a C++ Program to calculate roots of quadratic
equation ax^2+bx+c=0

#include<iostream>
#include<conio.h>
#include<math.h> //to claculate square root
using namespace std;
int main()
{

float root1,root2,a,b,c,d;
cout<<"Quadratic Equation is ax^2=bx+c=0";
cout<<" Enter values of a,b and c:";
cin>>a>>b>>c;
d=(b*b)-(4*a*c);
if(d>0)
{
cout<<"\nTwo real and distinct roots";
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
cout<<"\nRoots are "<<root1<<" and "<<root2;
}
else
if(d==0)
{
cout<<"\nTwo real and equal roots";
root1=root2=-b/(2*a);
cout<<"\nRoots are "<<root1<<" and "<<root2;
}
else
cout<<"\nRoots are COMPLEX and IMAGINARY....!!!";
return 0;
}
87. Write a C++ Program to find quotient and remainder of
two numbers.
#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int a,b,q,r;
cout<<"Enter two numbers:";
cin>>a>>b;
if(a>b)
{
q=a/b;
r=a%b;
cout<<"\nQuotient="<<q;
cout<<"\nRemainder="<<r;
}
else
cout<<"\nFirst no. should be greater than second no....!!!";
return 0;
}
88. Write a C++ Program to Find LCM and HCF of two
numbers

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a,b,hcf,lcm,max,min,r;
cout<<"Enter two numbers:";
cin>>a>>b;
if(a>b)
{
max=a;
min=b;
}
else
if(b>a)
{
max=b;
min=a;
}
if(a==b)
hcf=a;
else
{
do
{
r=max%min;
max=min;
min=r;
}while(r!=0);
hcf=max;
}
lcm=(a*b)/hcf;
cout<<"\nLCM="<<lcm<<"\nHCF="<<hcf;
return 0;
}
89. Write a c++ Program to accept a number and check the
given number is Armstrong or not.

#include<iostream>
# include <stdio.h>
# include <conio.h>
using namespace std;
int main( )
{
int n, a, b, c, d;
cout<<" Enter a Three Digit Number: ";
cin>>n;
a=n/100;
b=((n/10)%10);
c=n%10;
d=a*a*a*+b*b*b +c*c*c;
if (n==d)
cout<<"The Given Number is Armstrong number";
else
cout<< "The Given Number is Not Armstrong number";
return 0;
}
90. Write C++ Program to Count Occurrence of a Word in a
Text File

#include<iostream>
#include<fstream.h>
#include<string.h>
using namespace std;

int main()
{
ifstream fin("test.txt"); //opening text file
int count=0;
char ch[20],c[20];
cout<<"Enter a word to count:";
gets(c);
while(fin)
{
fin>>ch;
if(strcmp(ch,c)==0)
count++;
}
cout<<"Occurrence="<<count<<"\n";
fin.close(); //closing file
return 0;

}
91. Write C++ Program to Count no. of alphabates, digits
and spaces present in a file test.TXT

#include<fstream.h>
#include<conio.h>
int main()
{
ifstream fin("test.txt");
char ch;
int i,a=0,s=0,d=0;
while(fin)
{
fin.get(ch);
i=ch;
if(i>63&&i<91||i>96&&i<123)
a++;
else
if(ch==' ')
s++;
else
if(i>47&&i<58)
d++;
}
cout<<"No. OF Alphabates:"<<a;
cout<<"\nNo. Of Digits:"<<d;
cout<<"\nNo. Of Spaces:"<<s;
return 0;
}
90. Write C++ Program to read from a text file and than
write in another text file.

#include<fstream.h>
#include<iostream>
using namespace std;
int main()
{
ofstream fout("sare1.txt"); //create a file to write
ifstream fin("sare1.txt");
fout<<"Hello....!!";
fout.close(); //closing the file
fout.open("sare2.txt"); //create file to write
char ch;
while(fin) //loop wiill run till end of file
{
fin>>ch; //reading data from file
fout<<ch; //writing data to file
}
fin.close();
fout.close();
return 0;
}
91. Write C++ Program to Count Number of Words, Lines
and Total Size of a Text File

#include<iostream.h>
#include<fstream.h>
int main()
{
ifstream fin("story.txt"); //opening text file
int line=1,word=1,size; //will not count first word and last line so
initial value is 1
char ch;
fin.seekg(0,ios::end); //bring file pointer position to end of file
size=fin.tellg(); //count number of bytes till current postion for file
pointer
fin.seekg(0,ios::beg); //bring position of file pointer to begining of
file
while(fin)
{
fin.get(ch);
if(ch==' '||ch=='\n')
word++;
if(ch=='\n')
line++;
}

cout<<"Lines="<<line<<"\nWords="<<word<<"\nSize="
<<size<<"\n";
fin.close(); //closing file
return 0;

}
92. Write C++ Program to Remove Spaces from String

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int i,j=0;
char str[30];
cout<<"Enter a String:\n";
gets(str);
for(i=0;str[i]!='\0';++i)
{
if(str[i]!=' ')
str[j++]=str[i];
}
str[j]='\0';
cout<<"\nString After Removing Spaces:\n"<<str;
return 0;

}
93. Write C++ program for overloading binary operators,
addition, subtraction, multiplication, division and
comparison

#include<iostream>
#include<conio.h>
#include<process.h>
using namespace std;
class demo
{
float a,b;
public:
void getdata();
void display();
demo operator +(demo);
demo operator -(demo);
demo operator *(demo);
demo operator /(demo);
int operator ==(demo);
};

void demo::getdata()
{
cout<<"Enter values of a and b:";
cin>>a>>b;
}
void demo::display()
{
cout<<"a="<<a<<"\tb="<<b;
}

demo demo::operator +(demo d1)


{
demo d2;
d2.a=a+d1.a;
d2.b=b+d1.b;
return d2;
}

demo demo::operator -(demo d1)


{
demo d2;
d2.a=a-d1.a;
d2.b=b-d1.b;
return d2;
}

demo demo::operator *(demo d1)


{
demo d2;
d2.a=a*d1.a;
d2.b=b*d1.b;
return d2;
}

demo demo::operator /(demo d1)


{
demo d2;
d2.a=a/d1.a;
d2.b=b/d1.b;
return d2;
}

int demo::operator ==(demo d1)


{
if((a==d1.a)&&(b==d1.b))
return 1;
else
return 0;
}

int main()
{

int ch;
demo d1,d2,d3;

cout<<"First Object:\n";
d1.getdata();
cout<<"\nSecond Object:\n";
d2.getdata();

cout<<"\n\nOperator Overloadig Menu";

cout<<"\n\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Comparison\n6.E
cout<<"\n\nEnter your choice(1-6):";
cin>>ch;

switch(ch)
{
case 1: d3=d1+d2;
cout<<"\nThird Object:\n";
d3.display();
break;

case 2: d3=d1-d2;
cout<<"\nThird Object:\n";
d3.display();
break;

case 3: d3=d1*d2;
cout<<"\nThird Object:\n";
d3.display();
break;

case 4: d3=d1/d2;
cout<<"\nThird Object:\n";
d3.display();
break;

case 5: if(d1==d2)
cout<<"\nObjects are Equal";
else
cout<<"\nObjects are Not
Equal";
break;

case 6: exit(0);
break;

default: cout<<"Wrong Choice!!!Press any key to


exit";
getch();
}
getch();
return 0; }
94. Write C++ program to swap two numbers using class

#include<iostream>
#include<conio.h>
using namespace std;
class swap
{
int a,b;
public:
void getdata();
void swapv();
void display();
};

void swap::getdata()
{
cout<<"Enter two numbers:";
cin>>a>>b;
}

void swap::swapv()
{
a=a+b;
b=a-b;
a=a-b;
}
void swap::display()
{
cout<<"a="<<a<<"\tb="<<b;
}

int main()
{

swap s;

s.getdata();
cout<<"\nBefore swap:\n";
s.display();

s.swapv();
cout<<"\n\nAfter swap:\n";
s.display();

getch();
return 0;
}
95. Write C++ program to add, subtract, multiply and divide
two complex numbers using structures.

#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
struct complex
{
float rel;
float img;
}s1,s2;

int main()
{

float a,b;
cout<<"Enter real and imaginary part of 1st complex number:";
cin>>s1.rel>>s1.img;
cout<<"Enter real and imaginary part of 2nd complex number:";
cin>>s2.rel>>s2.img;

//Addition
a=(s1.rel)+(s2.rel);
b=(s1.img)+(s2.img);
cout<<"\nAddition: "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";
//Subtraction
a=(s1.rel)-(s2.rel);
b=(s1.img)-(s2.img);
cout<<"\nSubtraction: "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";

//Multiplication
a=((s1.rel)*(s2.rel))-((s1.img)*(s2.img));
b=((s1.rel)*(s2.img))+((s2.rel)*(s1.img));
cout<<"\nMultiplication: "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";

//Division
a=(((s1.rel)*(s2.rel))+((s1.img)*
(s2.img)))/(pow(s2.rel,2)+pow(s2.img,2));
b=(((s2.rel)*(s1.img))-((s1.rel)*
(s2.img)))/(pow(s2.rel,2)+pow(s2.img,2));
cout<<"\nDivision: "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";

getch();
}
96.Write C++ Program to Compare Two Strings Using
Pointers

#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
char str1[50],str2[50];
int str_cmp(char*,char*);
cout<<"Enter first string:";
gets(str1);
cout<<"Enter second string:";
gets(str2);

if(str_cmp(str1,str2))
cout<<"\nStrings are equal";
else
cout<<"\nStrings are not equal";

return 0;
}

int str_cmp(char *s1,char *s2)


{
while(*s1==*s2)
{
if(*s1=='\0'||*s2=='\0')
break;

s1++;
s2++;
}

if(*s1=='\0'&&*s2=='\0')
return 1;

return 0;
}
99. Write C++ program to perform a PUSH operation on a
dynamically allocated stack

#include<iostream>
#include<conio.h>
using namespace std;
struct Node
{
int data;
Node *next;
}*top,*p;

Node* newnode(int x)
{
p=new Node;
p->data=x;
p->next=NULL;
return(p);
}

void push(Node *q)


{
if(top==NULL)
top=q;
else
{
q->next=top;
top=q;
}
}

void showstack(Node *q)


{
while(q!=NULL)
{
cout<<q->data<<" ";
q=q->next;
}
}

int main()
{

int x;
char ch='y';
Node *nptr;
while(ch=='y'||ch=='Y')
{
cout<<"\nEnter the data(int type):";
cin>>x;
nptr=newnode(x);
push(nptr);
cout<<"\nThe stack is:";
showstack(top);
cout<<"\n\nWnat push more(y/n):";
cin>>ch;
}
return 0;
}
100. Write C++ Program for Linked List Representation of
Linear Queue
#include<iostream>
#include<stdlib.h>
using namespace std;
using namespace std;

struct node
{
int data;
struct node *next;
}*front=NULL,*rear,*temp;

void ins()
{
temp=new node;
cout<<"Enter data:";
cin>>temp->data;
temp->next=NULL;

if(front==NULL)
front=rear=temp;
else
{
rear->next=temp;
rear=temp;
}
cout<<"Node has been inserted\n";
}

void del()
{
if(front==NULL)
cout<<"Queue is empty\n";
else
{
temp=front;
front=front->next;
cout<<"Deleted node is "<<temp->data<<"\n";
delete(temp);
}
}

void dis()
{
if(front==NULL)
cout<<"Queue is empty\n";
else
{
temp=front;
while(temp->next!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}

cout<<temp->data;
}
}

main()
{
int ch;
while(1)
{
cout<<"\n*** Menu ***"
<<"\n1.Insert\n2.Delete\n3.Display\n4.Exit";
cout<<"\n\nEnter your choice(1-4):";
cin>>ch;
cout<<"\n";

switch(ch)
{
case 1: ins();
break;
case 2: del();
break;
case 3: dis();
break;
case 4: exit(0);
break;
default: cout<<"Wrong Choice!!!";
}
}

return 0;
}

You might also like