codescracker


c

C Program to Generate Armstrong Numbers



« Previous Program Next Program »


Generate Armstrong Numbers in C

To generate Armstrong number in C programming, you have to ask to the user to enter the interval to print the Armstrong numbers between the given interval

C Programming Code to Generate Armstrong Numbers

Following C program ask to the user to enter the interval, to print the Armstrong numbers in the given interval. Since if you start from 1 then first Armstrong number will be 153. So enter the interval which contain 153 like enter starting number as 1, 2, 3, 4......etc and enter ending number like 154, 155, 156.......etc. It is just a clue that first Armstrong number is 153 so to check, you can follow it. Generate Armstrong number from the following C program.

/* C Program - Generate Armstrong Numbers */
		
#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	int num1, num2, i, n, rem, temp, count=0;
	//enter the interval (enter two number)
	printf("Enter Starting Number : ");
	scanf("%d",&num1);
	printf("Enter Ending Number : ");
	scanf("%d",&num2);
	for(i=num1+1; i<num2; i++)
	{
		temp=i;
		n=0;
		while(temp!=0)
		{
			rem=temp%10;
			n = n + rem*rem*rem;
			temp=temp/10;
		}
		if(i==n)
		{
			if(count==0)
			{
				printf("Armstrong numbers between the given interval are : \n");
			}
			printf("%d  ",i);
			count++;
		}
	}
	if(count==0)
	{
		printf("Armstrong number not found between the given interval");
	}
	getch();
}

When the above c program is compile and executed, it will produce the following result. Above C Programming Example Output (for Armstrong numbers found):

c program generate armstrong number

Above C Programming Example Output (for Armstrong numbers not found):

c program display armstrong number between range

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test