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.

Showing posts with label If Else Statement. Show all posts
Showing posts with label If Else Statement. Show all posts

Tuesday, 20 August 2013

C++ program to check whether the given number is Adam Number or not

#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();
}


Monday, 19 August 2013

C++ program to check whether the given string is Palindrome or not (3)

Palindrome:
Palindromes are words or phrases that read the same in both directions.
Some examples of common palindromic words: civic, dewed, deified, dad, mom, devoved, hannah, repaper, radar, level, rotor, kayak, reviver, racecar, redder, madam, redder, bob, pop, tot, malayalam, noon, rotator, rotavator, stats, solos, tenet and refer.

Program:
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char s[50],m[50];
int p=1,i;
clrscr();
cout<<"Enter the string: ";
cin>>s;
strcpy(m,s);
strrev(m);
for(i=0;i<=strlen(s)-1;i++)
{
if(*(m+i)!=*(s+i))
{
p=0;
break;
}
}
if(!p)
cout<<"\n"<<s<<" is not a Palindrome";
else
cout<<"\n"<<s<<" is a Palindrome";
getch();
}



Tuesday, 13 August 2013

C++ program to implement Stack ADT using Array

#include<iostream.h>
#include<conio.h>
#define maxsize 10
class stack
{
private:
int a[maxsize];
int top;
public:
stack();
void push(int i);
void pop();
void display();
}
stack::stack()
{
top=0;
}
void stack::push(int i)
{
if(top==maxsize)
cout<<"\nStack is full";
else
{
top++;
a[top]=i;
}
}
void stack::pop()
{
if(top==0)
cout<<"\nStack is empty";
else
{
int i=a[top];
top--;
}
}
void stack::display()
{
if(top==0)
cout<<"Stack is empty";
else
{
cout<<"\nThe elements in the stack are: ";
for(int i=0; i<=top;i++)
cout<<a[i]<<'\t';
}
}
int main()
{
stack s;
int x,ch;
do
{
cout<<"\n1.Push";
cout<<"\n2.Pop";
cout<<"\n3.Display";
cout<<"\n4.Exit";
cout<<"\nEnter the choice";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the element to be pushed";
cin>>x;
s.push(x);
cout<<"\nThe element is inserted";
break;
case 2:
s.pop();
break;
case 3:
s.display();
case 4:
break;
}
}while(ch!=4);
getch();
}

Monday, 12 August 2013

Program to check whether the given number and its reverse are same

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int dig(int n, int *a);
revnum(int m, int *a);
int n,m;
void main()
{
int x[10], *a=&x[0], newno;
clrscr();
printf("Enter the number:");
scanf("%d", &n);
m=dig(n, a);
newno=revnum(m,a);
printf("\nReverse of the given number %d= %d",n,newno);
if(n==newno)
printf("\n\nEntered number and reverse number are same");
else
printf("\n\nEntered number and reverse number are not same");
getch();
}
int dig(int n, int *a)
{
int i=0, y;
while(n>0)
{
y=n%10;
*(a+i)=y;
n=n/10;
i++;
}
return i;
}
revnum(int m, int *a)
{
int z=0, s=0, d=m;
for(z=0; z<m; ++z, d--)
s=s+*(a+z)* pow(10, d-1);
return s;
}


Thursday, 8 August 2013

Program to check whether the given number is Palindrome or not


#include<stdio.h>
void main()
{
int n, rev=0, rem,temp;
clrscr();
printf("Enter any number: ");
scanf("%d", &n);
temp=n;
while(temp!=0)
{
rem=temp%10;
rev=rev*10+rem;
temp=temp/10;
}
if(rev==n)
printf("%d is a Palindrome",n);
else
printf("%d is not a Palindrome",n);
getch();
}





Wednesday, 7 August 2013

Program to find the number of vowels, consonants, digits, spaces and other characters

#include<stdio.h>
main()
{
char text[100];
int nv=0, nc=0, nd=0, ns=0, no=0;
void input(char text[], int *pv, int *pc, int *pd, int *ps, int *po);
clrscr();
printf("Enter a line of text: ");
gets(text);
input(text, &nv, &nc, &nd, &ns, &no);
printf("\nNo of Vowels:%d", nv);
printf("\nNo of Consonants:%d", nc);
printf("\nNo of Digits:%d", nd);
printf("\nNo of Spaces:%d", ns);
printf("\nNo of Other Characters:%d", no);
getch();
}
void input(char text[], int *pv, int *pc, int *pd, int *ps, int *po)
{
char c;
int i=0;
while((c=toupper(text[i]))!='\0')
{
if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
++*pv;
else if(c>='A'&&c<='Z')
++*pc;
else if(c>='0'&&c<='9')
++*pd;
else if(c==' '||c=='\t')
++*ps;
else
++*po;
++i;
}
}


Tuesday, 30 July 2013

Program to find the type of Triangle

#include<stdio.h>
#include<math.h>
int main()
{
int a, b, c;
clrscr();
printf("Enter the values of the sides of the triangle: \n");
scanf("%d %d %d", &a, &b, &c);
if ((a + b > c && a + c > b && b + c > a) && (a > 0 && b > 0 && c > 0))
{
if (a == b && b == c)
{
printf("Equilateral Triangle");
}
else if (a == b || b == c || a == c)
{
printf("Isosceles Triangle");
}
else
{
printf("Scalene Triangle");
}
}
else
{
printf("Triangle formation is not possible");
}
getch();
}


Thursday, 27 June 2013

Program to check whether the given number is Armstrong Number or not

Armstrong Number:
An Armstrong number (after Michael F. Armstrong) or a narcissistic number (also known as a pluperfect digital invariant (PPDI), or a plus perfect number) is a number that is the sum of its own digits each raised to the power of the number of digits.

Program:
#include<stdio.h>
void main()
{
int n,rem=0,sum=0,a;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
a=n;
while(n>0)
{
rem=n%10;
sum=sum+rem*rem*rem;
n=n/10;
}
if(a==sum)
printf("%d is an Armstrong number",a);
else
printf("%d is not an Armstrong number",a);
getch();
}

Program to check whether the given string is Palindrome or not (2)

#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
clrscr();
printf("Enter the string: ");
gets(a);
strcpy(b,a);
strrev(b);
if(strcmp(a,b) == 0)
printf("\nThe string is a Palindrome");
else
printf("\nThe string is not a Palindrome");
getch();
}


Program to check whether the given string is Palindrome or not (1)

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str1[50],rstr[50],ch;
int i=0,k=0;
cout<<"Enter the string:";
cin>>str1;
strcpy(rstr,str1);
k=strlen(str1);
for(i=0;i<k/2;i++)
{
ch=rstr[i];
rstr[i]=rstr[k-i-1];
rstr[k-i-1]=ch;
}
if(strcmp(str1,rstr))
cout<<"\n"<<str1<<" is not a palindrome";
else
cout<<"\n"<<str1<<" is a palindrome";
getch();
}


Tuesday, 4 June 2013

C++ program to implement Stack ADT using Linked List


#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node *link;
};
class stack
{
private:
struct node *top;
public:
stack();
void push(int n);
void pop();
void display();
};
stack::stack()
{
top=NULL;
}
void stack::push(int n)
{
node *tmp;
tmp= new node;
if(tmp==NULL)
cout<<"\nStack is empty";
else
{
tmp->data=n;
tmp->link=top;
top=tmp;
}
}
void stack::pop()
{
if(top==NULL)
cout<<"\nStack is empty";
else
{
node *tmp;
int n;
tmp=top;
n=tmp->data;
top=top->link;
delete tmp;
}
}
void stack::display()
{
node *tmp;
tmp=top;
if(tmp==NULL)
cout<<"\nStack is empty";
else
{
cout<<"\nThe elements in the stack are:";
while(tmp!=NULL)
{
cout<<tmp->data;
tmp=tmp->link;
}
}
}
void main()
{
clrscr();
stack s;
int x,ch;
do
{
cout<<"\n1.Push";
cout<<"\n2.Pop";
cout<<"\n3.Display";
cout<<"\n4.Exit";
cout<<"\nEnter the choice";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the element to be pushed";
cin>>x;
s.push(x);
cout<<"\nThe element is inserted";
break;
case 2:
s.pop();
break;
case 3:
s.display();
case 4:
break;
}
}while(ch!=4);
getch();
}