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 While Loop. Show all posts
Showing posts with label While Loop. Show all posts

Saturday, 15 March 2014

C++ program to get the Arithmetic Mean of first 10 natural numbers (from 1 to 10) using FOR loop, WHILE loop and DO WHILE loop

C++ program to get the Arithmetic Mean of first 10 natural numbers (from 1 to 10) using FOR loop, WHILE loop and DO WHILE loop:

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i, a;
float mean, sum=0;
cout<<"Mean of first 10 natural rumbers (1 to 10): ";
i=1;
cout<<"\n\nEnter your choice:"<<"\n"<<"1-> Using for loop"<<"\n"<<"2-> Using while loop"<<"\n"<<"3-> Using do...while loop"<<"\n\n";
cin>>a;
switch(a)
{
case 1:
for(i=1; i<=10; i++)
sum=sum+i;
cout<<"\nSum = "<<sum;
mean=(float)sum/10;
cout<<"\nMean = "<<mean;
break;
case 2:
while(i<=10)
{
sum=sum+i;
i++;
}
cout<<"\nSum = "<<sum;
mean=(float)sum/10;
cout<<"\nMean = "<<mean;
break;
case 3:
do
{
sum=sum+i;
i++;
}
while(i<=10);
cout<<"\nSum = "<<sum;
mean=(float)sum/10;
cout<<"\nMean = "<<mean;
default:
break;
}
getch();
}

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, 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 calculate the Power of a number using WHILE loop (2)

#include<stdio.h>
void main()
{
int bas, exp, a, power=1;
clrscr();
printf("Enter the value of base number: ");
scanf("%d",&bas);
printf("\nEnter the value of exponent: ");
scanf("%d", &exp);
a=exp;
while (a!=0)
{
power*=bas;
--a;
}
printf("\nThe value of %d power %d = %d", bas, exp, power);
getch();
}


Program to calculate the Power of a number using WHILE loop (1)

#include<stdio.h>
#include<conio.h>
void main()  
{  
int bas, exp, count = 1, power = 1 ;  
clrscr() ;  
printf("Enter the value of base number: ") ;
scanf("%d", &bas) ;  
printf("\nEnter the value of exponent : ") ;  
scanf("%d", &exp) ;
while(count <= exp)  
{  
power = power * bas ;  
count ++ ;  
}  
printf("\nThe value of %d power %d = %d", bas, exp, power) ;
getch() ;  
}


Tuesday, 2 July 2013

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

#include<stdio.h>
void main()
{
int num,i,f,r,sum=0,temp;
clrscr();
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num)
{
i=1,f=1;
r=num%10;
while(i<=r)
{
f=f*i;
i++;
}
sum=sum+f;
num=num/10;
}
if(sum==temp)
printf("%d is a strong number",temp);
else
printf("%d is not a strong number",temp);
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();
}

Wednesday, 5 June 2013

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


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

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