Basic (simple)
1. Write a program in C++ to calculate factorial of n.
#include<iostream.h>
#include<conio.h>
void main()
{
int i,n,f=1;
cout<<"Enter a number :";
cin>>n;
for(i=1;i<=n;i++)
{
f=f*i;
}
cout<<"Factorial of " << n << " is " << f;
}
2. Write a program in C++ to display a Fibonacci series of 15 terms.
#include<iostream.h>
#include<conio.h>
void main()
{
int i,n=15,a=0,b=1,c;
cout<< a << ", " << b;
for(i=1;i<=n-2;i++)
{
c=a+b;
cout<< c << ", " ;
a=b;
b=c;
}
}
3. Write a program in C++ to accept a number and test whether it is a prime
or not.
#include<iostream.h>
#include<conio.h>
void main()
{
int i,n,f=0;
cout<<"Enter a number :";
cin>>n;
for(i=2;i<n;i++)
{
if(n%i==0)
f=1;
}
if(f==0)
cout<< n << " is prime number";
else
cout<< n << " is not a prime number";
4. Write a program in C++ to find GCD of two natural numbers.
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
cout<<"Enter two integers :";
cin>>a>>b;
while(a!=b)
{
if(a>b)
a=a-b;
if(b>a)
b=b-a;
}
cout<<"GCD = " << a;
}
5. Write a program in C++ to find sum of the digits of a number (integer).
#include<iostream.h>
#include<conio.h>
void main()
{
int n,r,s=0;
cout<<"Enter a integer :";
cin>>n;
while(n>0)
{
r = n % 10;
s = s + r ;
n = n / 10;
}
cout<<"Sum of Digits of a number = " << s;
}
FUNCTIONS
1. Write c++ program using function to swap two integer using function named
„swap‟. Use call by reference method.
#include<iostream.h>
#include<conio.h>
void swap(int& a, int& b)
{
int t;
t=a;
a=b;
b=t;
}
void main()
{
int x,y;
cout<<"Enter two numbers :";
cin>>x>>y;
swap(x,y);
cout<<"After swapping :"<<endl;
cout<<"x=" << x << ", y = " << y;
}
2. Write power function in c++ that returns X raised to the power Y where
y can be any integer.
double power (double x, int y);
#include<iostream.h>
#include<conio.h>
double power (double x, int y)
{
double p=1;
for(int i = 1; i<=y ; i++)
{
p = p * x;
}
return p;
}
void main()
{
double x,p;
int y;
cout<<"Enter base:";
cin>>x;
cout<<"Enter exponent:";
cin>>y;
p = power(x,y);
cout<<"Answer : "<<p;
}
3. Write a program in C++ to find smallest/greatest of 4 given integers
using min()/max() function to return smallest/ greatest of 4 given
integers. int max(int, int, int, int);
#include<iostream.h>
#include<conio.h>
int max(int a, int b, int c, int d)
{
int m;
if(a>b)
m=a;
else
m=b;
if(m<c)
m=c;
if(m<d)
m=d;
return m;
}
void main()
{
int p,q,r,s,m;
cout<<"Enter 4 numbers:";
cin>>p>>q>>r>>s;
m = max(p,q,r,s);
cout<<"Max : "<<m;
}
ARRAY & PIONTER
1. Write a program in C++ to accept set of 10 numbers and print the numbers
using pointers.
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],i,*p;
cout<<"Enter 10 numbers :";
for(i=0;i<10;i++)
{
cin>>a[i];
}
p = a ; // or p = &a[0];
cout<<"Elemets of array are as follows : ";
for(i=0;i<10;i++)
{
cout<<*p<<" ";
p++;
}
}
2. Write a program in C++ to search the address of a given integer of
given array. If given integer is found, the function returns address,
otherwise it returns NULL.
3. Write a program in C++ to accept set of 10 numbers and find out
largest/smallest number in the given array.
#include<iostream.h>
void main()
{
int a[10],i,m;
cout<<"Enter 10 numbers :";
for(i=0;i<10;i++)
{
cin>>a[i];
}
m = a[0];
for(i=1;i<10;i++)
{
if(m < a[i]) // for smallest --> m > a[i]
m = a[i];
}
cout<<"Largest no : " << m ;
}
4. Write a program in C++ to accept set of 10 numbers and prints the sum
& average of the given array elements.
#include<iostream.h>
void main()
{
int a[10],i,sum=0;
float avg;
cout<<"Enter 10 numbers :"
for(i=0;i<10;i++)
{
cin>>a[i];
sum = sum + a[i];
}
avg = sum / 10.0 ;
cout<<"Sum : " << sum << endl ;
cout<<"Average : " << avg << endl ;
}
OOP (class based program)
1. Write a program in C++ to find GCD of two integers. Define a method
find to accept the values and calculate GCD of two numbers print the GCD.
#include<iostream.h>
class GCD
{
int a, b;
public:
void input()
{
cout<<"Enter 2 numbers";
cin>>a>>b;
}
void find()
{
while(a!=b)
{
if(a>b)
a=a-b;
if(b>a)
b=b-a;
}
}
void print()
{
cout << "GCD = " << a;
}
};
void main()
{
GCD g;
[Link]();
[Link]();
[Link]();
}
2. Write a C++ program to implement a circle class. Object of this class
represents a circle, accepting radius as float. Include an area() function
which will calculate the area of circle.
#include<iostream.h>
class Circle
{
float r,a;
public:
void input()
{
cout<<"Enter radius";
cin>>r;
}
void area()
{
a = 3.14 * r * r ;
}
void print()
{
cout << "Area = " << a;
}
};
void main()
{
Circle c;
[Link]();
[Link]();
[Link]();
}
3. Implement a class average. Include a constructor in it which will
accept value of three variables from use. Include two more functions in
it, one to calculate average and other to print it.
#include<iostream.h>
class Average
{
int a,b,c;
float avg;
public:
Average()
{
cout<<"Enter 3 numbers";
cin>>a>>b>>c;
}
void cal()
{
avg = ( a + b + c ) / 3.0 ;
}
void print()
{
cout << "Average = " << avg;
}
};
void main()
{
Average x;
[Link]();
[Link]();
}
4. Implement a class temperature. Include a constructor in it which
accepts value of temperature in degree Celsius. Include two functions in
it. One to convert Celsius to Fahrenheit and other to print answer. Formula
:
#include<iostream.h>
class Temperature
{
float c, f;
public:
Temperature()
{
cout<<"Enter temp. in degree Celsius :";
cin>>c;
}
void convert()
{
f = 9.0/5.0 * c + 32;
}
void print()
{
cout << "Fahrenheit = " << f;
}
};
void main()
{
Temperature t;
[Link]();
[Link]();
}
STRING
1. Write a C++ program to read a line of text & count number of words
in a given string.
#include<iostream.h>
void main()
{
char a[80];
int count=1,i;
cout<<"Enter a string :";
[Link](a,80);
for(i=0;a[i]!=‟\0‟;i++)
{
if(a[i] == „ „)
count++;
}
cout<<"Number of words in entered string =" << count;
}
2. Write a C++ program to replace every space in inputted string with
a hyphen(-) [less than 80 chars]
#include<iostream.h>
void main()
{
char a[80];
int i;
cout<<"Enter a string :";
[Link](a,80);
for(i=0;a[i]!=‟\0‟;i++)
{
if(a[i] == „ „)
{
a[i] = „-„ ;
}
}
cout<<endl<<"New string =" << a;
}
3. Write a C++ program to read a line of text & reverse a string.
#include<iostream.h>
void main()
{
char a[80];
int i,j,len;
cout<<"Enter a string :";
[Link](a,80);
cout << “Original string = ” << a << endl;
for(len=0;a[len]!=‟\0‟;len++){} // or len = strlen(a);
for(i=0,j=len-1; i<j ; i++,j--)
{
char t;
t = a[i];
a[i] = a[j];
a[j] = t;
}
cout<< endl <<"Reversed string = " << a;
}
4. Write a C++ program to read a line of text & copy it to another string
without using library function.
#include<iostream.h>
void main()
{
char a[20],b[20];
int i;
cout<<"Enter a string :";
[Link](a,20);
for(i=0; a[i]!=‟\0‟;i++)
{
b[i] = a[i];
}
b[i] = „\0‟; // append null at the end of string
cout<<"the copied string is " << b;
}