In this tutorial you will learn about the C Program to Generate Armstrong Numbers and its application with practical example.
C Program to Generate Armstrong Numbers
In this tutorial, we will learn to create a C program that will Generate Armstrong Numbers in C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- While loop in C programming.
Program to Generate Armstrong Numbers:-
As we all know the c is a very powerful language. With the help of c programming language, we can make many programs. We can perform many input-output operations using c programming. In today’s tutorial, we take the input numbers from the user. Then we will Generate Armstrong Numbers between the taken range with the help of the c programming language.
With the help of this program, we can Generate Armstrong Numbers.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declare the variables for the program. 2. Taking the input numbers from and to by the user. 3. Calculating the <strong>Armstrong Numbers</strong>. 4. Print the Result. 5. End the program. |
Program to Generate Armstrong Numbers:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
/* C Program to Generate Armstrong Numbers */ #include<stdio.h> int main() { /* Declaring the variables for the program */ int no1, no2, flag, noOfDigit, num, res=0, rem, pow, i; /* Taking the range of numbers between which the we need to get the Armstrong Numbers */ printf("Enter the Interval: "); scanf("%d%d", &no1, &no2); //prinitng the armstrong numbers printf("\nArmstrong Numbers between %d and %d:-\n", no1, no2); while(no1<=no2) { flag = no1; noOfDigit=0; while(flag>0) { flag = flag/10; noOfDigit++; } num = no1; while(num>0) { rem = num%10; pow = 1; i = 0; while(i<noOfDigit) { pow = pow*rem; i++; } res = res + pow; num = num/10; } if(res==no1) //prinitng the armstrong numbers printf(" %d\n", res); no1++; res = 0; } return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- no1 = it will hold the integer value.
- no2 = it will hold the integer value.
- flag = it will hold the integer value.
- noOfDigit = it will hold the integer value.
- num = it will hold the integer value.
- res = it will hold the integer value.
- pow = it will hold the integer value.
- i = it will hold the integer value.
Taking the input integer number interval from the user.