C++ Program to Check the Number is Armstrong Number or Not
This is C++ Program to Check the Number is Armstrong Number or Not || C++ Programming Tutorial #8(beginner) . you will learn to check whether a number entered by the user is an Armstrong number or not. To understand this example, you should have the knowledge of the following C++ programming topics:
C++ if, if...else and Nested if...else
C++ while and do...while Loop
SOME PRACTICE QUESTION :
Q.1) Write a program in ‘C’ to print your name 5 times using while loop.
Q.2) Write a program in ‘C’ to input any integer and print your name that many times.
Q.3) Write a program in ‘C’ to print the series as 1 2 3 4 5 6 7 ..........n, where ‘n’ is user input.
Q.4) Write a program in ‘C’ to print the series as 1 3 7 15 31 ..........n, where n is given by user.
Q.5) Write a program in ‘C’ to find out sum of the following series:
1+2+3+4+ ……………. +n, where n is given by user.
Q.6) Write a program in ‘C’ to calculate the factorial of a given number.
Q.7) Write a program in ‘C’ to calculate the sum of digits of a given number.
source code :
/*
ARMSTRONG NUMBER : a number that is the sum of its own digits each raised to the power of the number of digits.
example : 153,1634
153 = 1^3 + 5^3 + 3^3 = 153
1634 = 1^4 + 6^4 + 3^4 + 4^4 = 1634
*/
#include<iostream>
#include<math.h>
using namespace std;
int main(){
int n,count=0;
cout<<"enter the number : ";
cin>>n;
int n1=n,rem,sum=0;
int n2=n;
while(n!=0){
n =n /10; //0
count ++;
}
while(n1!=0){
rem = n1 % 10;
sum= sum + pow(rem,count);
n1 = n1/10;
}
if (n2==sum )
cout<<"Armstrong number ";
else
cout<<"not armstrong number ";
return 0;
}
ConversionConversion EmoticonEmoticon