C++ Program to Print Armstrong Number in given range

 

C++ Program to Print Armstrong Number in given range

This is C++ Program to print Armstrong Number in given range || C++ Programming Tutorial #9(beginner) . you will learn to check whether a number entered by the user is an Armstrong number or not and print it in given range. 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 a,b;
    cout<<"Enter the range : ";
    cin>>a>>b;
    for(int i=a;i<=b;i++){
        int n=i,count=0;
    int n1=i,rem,sum=0;
    int n2=i;
    while(n!=0){
        n =n /10;         
        count ++;
    }
    while(n1!=0){
        rem = n1 % 10;
        sumsum + pow(rem,count);
        n1 = n1/10;
    }
    if (n2==sum )
    cout<<sum<<endl;
    }
    return 0;
}
Previous
Next Post »