Showing posts with label armstrong. Show all posts
Showing posts with label armstrong. Show all posts

C++ Program/Code For Printing all the armstrong number between 1 to n

Leave a Comment
//In this program, the maximum limit 'n' will be asked from user & the Armstrong series will be printed on screen

#include<iostream>                            //For Input/Output Stream Related Functions
#include<conio.h>                              //For Console Input/Output function like clrscr & getche
#include<math.h>                              //For mathematical functions
using namespace std;                         //Using the "standard" namespace of iostream class
void main()
{
clrscr();                                               //For Clearing the screen
long n,i,s,r,k;                                
cout<<"Enter no.:";
cin>>n;                                               //Value of 'n' entered by the user                  

for(i=1;i<=n;i++)
{
k=i;                                                    //Copying the value of 'k' in the variable 'i'
s=0;                                                    //Value of 's', is initialized with '0' at every looping turn
while(k!=0)                                       //Continue till 'k' is not equal to '0'
{
r=k%10;                                           //remainder will be stored in 'r'
k=k/10;                                             //reducing the value of 'k' by a factor of '10'
s=s+pow(r,3);                                   //'pow(r,3)' will give the cube of variable 'r'
}
if(s==i)                                             //If sum 's' is equal to 'i' then its an Armstrong Number
cout<<s<<"\t";
}
getche();                                         //To exit the program after an user's keystroke
}
Read More...

C++ program/code to check wether a number is Armstrong or Not

Leave a Comment
 //This program lets the user to enter a number, and check whether that is Armstrong or not

#include<iostream.h>                           //For Input/Output Stream Function
#include<conio.h>                                 //For Console Input/Output function like clrscr & getche
#include<math.h>                                 //For mathematical functions
void main()
{
clrscr();                                                  //For clearing the screen
int n,i,s=0,r;                                           //'s' is initialized by '0'
cout<<"Enter no.:";
cin>>n;                                                  //user inputs the number
i=n;                                                        //Storing a temporary copy
while(n!=0)
{
r=n%10;                                               //Calculation of the remainder when divided by 10
n=n/10;                                                 //'n' is decreased by a factor of 10
s=s+pow(r,3);                                       //cube of remainder is added to the sum
}
if(s==i)                                                 //If sum is equal to the original copy of the input data
cout<<"\nTne No. is Armstrong.";
else
cout<<"\nThe No. is not Armstrong.";
getche();                                              //To exit the program after a key stroke
}
Read More...

C Program To Check for armstrong, palindrome, & perfect number in a single program

Leave a Comment
#include<stdio.h>
#include<conio.h>
int main()
{
int n,c,r,s,t,i;
do{
printf("\nEnter the choice!\n");
printf("\nEnter 1 for checking Armgstrong number!\nEnter 2 for checking perfect square number!\nEnter 3 for checking perfect number!\nEnter 0 to exit!\n");
printf("\nEnter your choice:");
scanf("%d", &c);
if(c==0)
{
printf("\nClosing the program!!");
getche();
exit(0);
}
printf("\nEnter the number for checking:");
scanf("%d", &n);
switch(c)
{
case 1:{
t=n;s=0;
while(t!=0)
{
r=t%10;
t=t/10;
s=s+r*r*r;
}
if(s==n)
printf("\nEntered number is an Armgstrong number!");
else
printf("\nEntered number is not an Armgstrong number!");
break;
}
case 2:{
t=0;
for(i=1;i<=n;i++)
{
if((i*i)==n)
{
t=1;
break;
}
}
if(t==1)
printf("\nThe number is a perfect square!");
else
printf("\nThe number is a not perfect square!");
break;
}
case 3:{ s=0;
for(i=1;i<=n/2;i++)
{
if(n%i==0)
{
s=s+i;
}
}
if(s==n)
printf("\nThe number is perfect!!");
else
printf("\nThe number is not perfect!!");
break;
}
default: printf("\nWrong choice entered!!");
}
}while(n!=0);
getche();
return 0;
}

Read More...