This blog consists of C and C++ programs. C and C++ programming is the basics to learn any programming language. Most of the programs are provided with their respective outputs. This blog also contains Data Structures programs using Object-Oriented Programming (C++). Some of the best books for learning C and C++ programming are also mentioned.

Wednesday 4 September 2013

Tips to convert C program to C++ program

  • Change the header file from "#include<stdio.h>" to "#include<iostream>.h"
  • For input use "cin>>" instead of "scanf".
  • For output use "cout<<" instead of "printf".
  • Don't use "%" symbol and "&" sign in "cin>>" statement.
  • clrscr() must be used before initialization and declaration in C++ programming.
For example, let us compare with the program to find whether the given number is Adam Number or not.


C Program
C++ Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int r,p,m,b,n,rev,a,c,i,z;
r=0;
rev=0;
clrscr();
printf("Enter any number between 10 and 100:");
scanf("%d",&z);
n=z;
c=n*n;
while(n!=0)
{
m=n%10;
r=r*10+m;
n=n/10;
}
printf("\nThe square of %d is %d",z,c);
printf("\nThe reverse of %d is %d",z,r);
p=r*r;
printf("\nThe square of %d is %d",r,p);
while(c!=0)
{
a=c%10;
rev=rev*10+a;
c=c/10;
}
if(rev==p)
printf("\n%d is an Adam Number",z);
else
printf("\n%d is not an Adam Number",z);
getch();
}
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int r,p,m,b,n,rev,a,c,i,z;
r=0;
rev=0;
cout<<"Enter any number between 10 and 100: ";
cin>>z;
n=z;
c=n*n;
while(n!=0)
{
m=n%10;
r=r*10+m;
n=n/10;
}
cout<<"\n"<<"The square of "<<z<<"is "<<c;
cout<<"\n"<<"The reverse of "<<z<<"is "<<r;
p=r*r;
cout<<"\n"<<"The square of "<<r<<"is "<<p;
while(c!=0)
{
a=c%10;
rev=rev*10+a;
c=c/10;
}
if(rev==p)
cout<<"\n"<<z<<" is an Adam Number";
else
cout<<"\n"<<z<<" is not an Adam Number";
getch();
}
 

No comments:

Post a Comment