Saturday, July 27, 2013

Program to display all prime numbers less than 100

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,f;
for(i=2;i<100;i++)
{
for(j=2;j<i;j++)
{
if(i%j==0)
    {
    f=1;
    break;
    }
else
    f=0;
}
if(!flag)
cout<<i;
}
getch();
}

Friday, July 26, 2013

Program to insert new element in an array

#include<iostream.h>
#include<conio.h>
void main()
{
int s,n,i,a[50],p;
clrscr();
cout<<"Enter the number of elements of array : ";
cin>>n;
cout<<"Enter the elements : ";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the new element and its position : ";
cin>>s>>p;
for(i=n;i>=p;i--)
a[i]=a[i-1];
a[p]=s;
cout<<"The array after inserting new element is : ";
for(i=0;i<n+1;i++)
cout<<a[i]<<"  ";
getch();
}

To know about array click here

Program to reversing a string

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[20],rev[20];
int len,k=0;
cout<<"Enter the string  ";
cin.getline(name,20);
for(len=0;name[len]!='\0';len++)
for(i=len;i>=0;i--)
{
rev[k]=name[i];
k++;
}
rev[k]='\0';
cout<<"Reversed string  ";
cout.write(rev,20);
getch();
}

Program for multiplication of matrix using " class "

To know about 'class' click here


#include<iostream.h>
#include<conio.h>
class matrix
 {
 int a[10][10];
 int m,n;
 public:
 void input();
 void output();
 void multiply(matrix,matrix);
 };
 void matrix::input()
 {
 cout<<"Enter the number of row : ";
 cin>>m;
 cout<<"Enter the number of column : ";
 cin>>n;
 cout<<"Matrix"<<"\n";
 for(int i=0;i<m;i++)
 {
 for(int j=0;j<n;j++)
 {
 cin>>a[i][j];
 }
 }
 }
 void matrix :: output()
 {
 for(int i=0;i<m;i++)
 {
 cout<<"\n";
 for(int j=0;j<n;j++)
 {
 cout<<a[i][j]<<"\t";
 }
 }
 }
 void matrix :: multiply(matrix m1, matrix m2)
 {
 if(m1.n!=m2.m)
    {
     cout<<"matrix multiplication is not possible";
    }
 else
    {
     for(int i=0;i<m1.m;i++)
    {
     for(int j=0;j<m2.n;j++)
    {
     a[i][j]=0;
     for(int k=0;k<m1.n;k++)
     {
     a[i][j]=a[i][j] +( m1.a[i][k]*m2.a[k][j]);
     m=m1.m;
     n=m2.n;
    }
 }
 }
 }
 }
 void main()
 {
 clrscr();
 matrix m1,m2,m3;
 m1.input();
 m2.input();
 m3.multiply(m1,m2);
 m3.output();
 getch();
 }

Program to find out the sum of two matrices



#include<iostream.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,m,n;
clrscr();
cout<<"Enter the number of raws and columns of two matrices : ";
cin>>m>>n;
cout<<"Enter the elements into matrix A\n";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
cout<<"\nEnter the elements into matrix B\n";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>b[i][j];
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=0;i<m;i++)
    {
    cout<<"\n";
    for(j=0;j<n;j++)
    cout<<c[i][j];
    cout<<"\t";
    }
getch();
}


Thursday, July 25, 2013

Program to find n!/(r!(n-r)!)

#include<iostream.h>
#include<conio.h>
int fact(int);
void main()
{
clrscr();
int n,r,f,f1,f2,ft;
cout<<"Enter the value of n and r : ";
cin>>n>>r;
f=fact(n);
f1=fact(r);
f2=fact(n-r);
ft=f/(f1*f2);
cout<<"result : "<<ft;
getch();
}
int fact(int n)
{
int x;
if(n==1)
x=1;
else
x=n*fact(n-1);
return(x);
}

To know about function click here

Program to accept 20 numbers and display it in ascending order and descending order - Bubble sort


#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],i,j,t;
clrscr();
cout<<"Enter 20 numbers : ";
for(i=0;i<20;i++)
cin>>a[i];
for(i=0;i<20;i++)
{
for(j=i+1;j<20;j++)
{
 if(a[i]>a[j])
{
    t=a[i];
    a[i]=a[j];
    a[j]=t;
}
}
}
cout<<"Ascending order : "<<"\n";
for(i=0;i<20;i++)
cout<<a[i]<<"\n";
cout<<"Descending order : "<<"\n";
for(i=19;i>=0;i--)
cout<<a[i]<<"\n";
getch();
}

Program to find the sum of all the elements of a matrix


#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int a[20][20],sum=0,m,n,i,j;
 cout<<"Enter the size of matrix : "
 cin>>m>>n;
 cout<<"Enter the elements : ";
 for(i=0;i<m;i++)
 for(j=0;j<n;j++)
 {
     cin>>a[i][j];
     sum+=a[i][j];
 }
 cout<<"Sum of elements of the matrix is : "<<sum
 getch();
}

Program to find the volume of tray

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float l,b,h,v;
cout<<"Enter the length of tray : ";
cin>>l;
cout<<"Enter the breadth of tray : ";
cin>>b;
cout<<"Enter the height of tray : ";
cin>>h;
v=l*b*h;
cout<<"Volume is : "<<v;
getch();
}

Program to calculate the area of circle

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float r,a;
cout<<"Enter the radius of circle";
cin>>n;
a=(22/7)*r*r;
cout<<"Area of the circle is : ";<<a;
getch();
}

Program to access the elements in two dimensional array and display it


#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr();
 int a[20][20],m,n,i,j;
 cout<<"Enter the size of matrix : "
 cin>>m>>n;
 cout<<"Enter the elements : ";
 for(i=0;i<m;i++)
 for(j=0;j<n;j++)
 cin>>a[i][j];
 cout<<"The matix is : "<<"\n";
 for(i=0;i<m;i++)
 {
 for(j=0;j<n;j++)
 {
 cout<<a[i][j]<<"\t";
 }
 }
 getch();
}

Program to generate the first n elements of a Fibonacci series


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,i,n,c;
a=0;
b=1;
cout << "Enter the number of terms : ";
cin>>n;
cout << a <<"\t"<<b<<"\t";
for(i=2;i<n;i++)
{
    c=a+b;
    a=b;
    b=c;
    cout<<c<<"\t";

}
getch();
}

Wednesday, July 24, 2013

Program to find the factorial of a number using function

#include<iostream.h>
#include<conio.h>
int fact(int);
void main()
{
clrscr();
int n,f;
cout<<"Enter a number : ";
cin>>n;
f=fact(n);
cout<<"Factorial of the given number is : "<<f;
getch();
}
int fact(int n)
{
int x;
if(n==1)
x=1;
else
x=n*fact(n-1);
return(x);
}

Program to calculate the area of a triangle


#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
  clrscr();
  float a,b,c,s,area;
  cout<<"Enter the 3 sides : ";
  cin>>a>>b>>c;
  s=a+b+c;
  area=sqrt(s*(s-a)*(s-b)*(s-c));
  cout<<"Area :"<<area;
  getch();
}

Program to convert temperature in Celsius to Fahrenheit


#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  float cs,fh;
  cout<<"Enter the temp in Celsius :";
  cin>>cs;
  fh=(9/5)*c+32;
  cout<<"Temperature in Fahrenheit =  "<<fh;
  getch();
}

Program to enter your height in centimeters and convert into feet and inches


#include<iostream.h>
#include<conio.h>
void main()
{
  clrscr();
  float ht,inch,feet;
  cout<<"Enter your height in centimeters : ";
  cin>>ht;
  feet=ht/30.48;  
  inch=ht/2.54;
  cout<<"Height in feets : "<<feet;
  cout<<"Height in inch : "<<inch;
  getch();
}

Program to count the number of lower case vowels in a string


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
void main()
{
 clrscr();
 char st[100];
 int l,n,i;
 cout<<"Enter the string\n";
 cin.getline(st,100);
 l=strlen(st);
 for(i=0;i<=l;i++)
 {
   if(islower(st(i))) && ((st(i)==a)||(st(i)==e)||(st(i)==i)||(st(i)==o)||(st(i)==u))
   n++;
 }
 cout<<"The number of lower case vowels is : "<<n;
 getch();
}

Tuesday, July 23, 2013

Program to access the marks of 3 students and display it out

#include<iostream.h>
#include<conio.h>
void main()
{
int n[3],i;
cout<<"Enter the marks\n";
for(i=0;i<3;i++)
cin>>n[i];
cout<<The given marks are\n";
for(i=0;i<3;i++)
cout<<"\n"<<n[i];
getch();
}

Program to convert Binary into Decimal number


#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
  int a[10],i,n,sum=0,j,k=0;
  clrscr();
  cout<<"Enter binary number : ";
  cin>>n;
  i=0;
  while(n>0)
  {
      a[i]=n%10;
      n=n/10;
      i++;
  }
  for(j=0;j<i;j++)
  {
      sum=sum+a[j]*pow(2,k);
       k++;
  }
  cout<<"Decimal number is : "<<sum;
  getch();
}

Monday, July 22, 2013

Program to check whether a number is palindrome or not

A palindromic number is a number that remains the same when its digits are reversed.



#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,a=0,d;
cout<<"Enter the number ";
cin>>n;
i=n;
while(n>0)
{
    d=n%10;
    n=n/10;
    a=a*10+d;
}
if(ans==i)
    cout<<"Number is a Palindrome ";
else
    cout<<"Number is not a Palindrome ";
getch();
}

Program to find whether two strings contain equal number of characters

#include<iostream.h>
#include<conio.h>
#include<string.h>
int main()
{
char s1[60],s2[60];
cout<<"Enter strings ";
cin.getline(s1,60);
cin.getline(s2,60);
if((strlen(s1))==(strlen(s2)))
cout<<"Both strings contain equal number of characters ";
else
cout<<"Both strings contain different number of characters ";
return0;
}

Program to find the roots of a quadratic equation

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int a,b,c,d;
float r1,r2;
cout<<"Enter the values of a,b & c : ";
cin>>a>>b>>c;
d=(b*b)-(4*a*c);
if(d==0)
{
    cout<<"Roots are real and equal ";
    r1=r2=(-b)/(2*a);
    cout<<r1<<" "<<r2;
}
else
if(d>0)
    {
        cout<<"Roots are real and unequal ";
        r1=(-b+sqrt(d))/(2*a);
        r1=(-b-sqrt(d))/(2*a);
    }
else
cot<<"Roots are imaginary";
getch();
}

Program to find the largest number from a set of 'n' numbers

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,lrg,a;
cout<<"Enter the number of numbers : ";
cin>>n;
cout<<"Enter a number : ";
cin>>lrg;
while(n>1)
{
    cout<<"Enter a number : ";
    cin>>a;
    if(a>lrg)
    lrg=a;
    n--;
}
cout<<"Largest number is : "<<lrg;
getch();
}

Program to read a string and to count the number of words

#include<iostream.h>
#include<stdio.h>
int main()
{
clrscr();
char str[80];
int i;
int wordcount=0;
puts("Enter a string upto 80 characters : ");
gets(str);
for(i=0;str[i]!='\0';i++)
{
    if(str[i]==' ')
    wordcount++;
}
cout<<"The number of words are "<<wordcount<<"\n";
return0;
}

Program to input characters and change their case using " do-while loop "

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char ch;
do
{
    cout<<"\n Enter a character (0 to stop) : ";
    cin>>ch;
    if(ch=='\n')
    {
        ch=getchar();
        cout<<end1;
    }
    else
    if(ch>=65 && ch<=90)
    ch=ch+32;
    else
    if(ch>=97 && ch<=122)
    ch=ch-32;
    putchar();
}while(ch!='0');
getch();
}

Program using " Nested loop "

#include<iostream.h>
#include<stdio.h>
void main()
{
clrscr();
char c;
int i,j;
c='A';
for(i=1;i<=5;i++)
{
    for(j=1;j<=i;j++)
    {
        putchar(c);
    }
    putchar('\n');
    fflush(stdout);
}
}

The output of this program will be:-

A
BB
CCC
DDDD
EEEEE

Sunday, July 21, 2013

Program to check whether a given number is prime or not using "for loop"

#include<iostream.h>
#include<process.h>
void main()
{
int num,i;
clrscr();
cout<<"Enter the number ";
cin>>num;
for(i=2;i<=num/2;++i)
if(num%i==0)
{
    cout<<"Not a prime number ";
    exit(0);
}
cout<<"It is prime number ";
getch();
}

Program to print the sum of first n natural numbers using " while loop "

#include<iostream.h>
#include<conio.h>
void main()
{
int i,n,sum;
clrscr();
cout<<"Enter the limit  ";
cin>>n;
sum=0;
i=1;
while(i<n)
{
    sum=sum+i;
    ++i;
}
cout<<"sum of first "<<n<<" natural number is "<<sum;
getch();
}

Program to print the multiplication of a given numbers using "for loop"

#include<iostream.h>
#include<conio.h>
void main()
{
int n;
clrscr();
cout<<"Enter an integer number : ";
cin>>n;
for(i=1;i<=10;i++)
cout<<i<<" * "<<n<<" = "<<i+n;
getch();
}

output of this program if i enter number 5 :-

Enter an integer number :
5
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
10 * 5 = 50

To know about loop click here

Program using switch statement - Accept day numbers and print the name of the day of the week

#include<iostream.h>
#include<conio.h>
void main()
{
int day;
clrscr();
cout<<"Enter number of week's day 1 - 7   ";
cin>>day;
switch(day)
{
    case1:cout<<"sunday";break;
    case2:cout<<"monday";break;
    case3:cout<<"tuesday";break;
    case4:cout<<"wednesday";break;
    case5:cout<<"thursday";break;
    case6:cout<<"friday";break;
    case7:cout<<"saturday";break;
    default:cout<<"invalid day number";
}
getch();
}

Program to find the biggest of two numbers using conditional operator

#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,big;
clrscr();
cout<<"Enter two numbers   ";
cin>>a>>b;
big=(a>b)?a:b;
cout<<"Biggest number is  "<<big<<end1;
getch();
}

Program to find the biggest among three numbers

#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c,big;
clrscr();
cout<<"Enter three numbers  ";
cin>>a>>b>>c;
if(a>b)
    {
    if(a>c)
        big=a;
    else
        big=c;
    }
    else
    {
    if(b>c)
        big=b;
    else
        big=c;
    }
cout<<"Biggest number is  "<<big;
getch();
}

Program to check whether a given number is odd or even

#include<iostream.h>
#include<conio.h>
void main()
{
int num;
clrscr();
cout<<"Enter a Number  ";
cin>>num;
if(num%2==0)
cout<<num<<" is even";
else
cout<<num<<" is odd";
getch();
}

About My Blog

Here I am posting some sample and simple C++ programs that are useful for computer science students.

History of C++

C++ was developed by Bjarne Stroustrup in the 1980's. C++ programming language is extension to C Language. In beginnings it is known as "C with Classes".
Later the name was changed to C++. In 1983, the name of the language was changed from C with Classes to C++. The ++ operator in the C language is an operator for incrementing a variable, which gives some insight into how Stroustrup regarded the language. Many new features were added around this time, the most notable of which are virtual functions, function overloading, references with the & symbol, the const keyword, and single-line comments using two forward slashes (which is a feature taken from the language BCPL).
There are several versions of C++ Programming Language :
  • Visual C++
  • Borland C++
  • Turbo C++
  • Standardize C++ [ANSI C++]