3/07/2016

C Program To Check Number Is Armstrong Or Not using While Loop

Problem :- Write A C Program To Check Number Is Armstrong Or Not using While Loop .

Armstrong Number :- An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371 source Click Here

Logic :- Logic is very simple you have to you have to separate all digit and Find The sum of the cube of digit .

Example :- let's check 371 then divide by 10 we got 371/10=1 .cube of 1 is 1 store this result and again divide by 10 we got 37/10=7 . cube of 7 is 343 store and add to previously got result 1 so now its become 344  ,again divide by 10 we got 3/10=3 cube of 3 is 27 ,Now again add 27+344=371
so the Number is Armstrong Number 

Note :- Divide the number by 10 until it become Zero

Try Yourself C Program to Check Prime or Armstrong Number Using Function

Solution :-

#include<stdio.h>
int main()
{
  //Ghanendra Yadav 
  int n, n1, rem, num=0;
 
  while(1)
  {
  printf("Enter a Positive Integer To Check For Armstrong : \n");
  scanf("%d", &n);
  
  n1=n;
  
  while(n1!=0)
  {
      rem=n1%10;
      num+=rem*rem*rem;
      n1/=10;
  }
  if(num==n)
    printf("\n%d Is An Armstrong Number.\n\n",n);
  else
    printf("\n%d IsNot an Armstrong Number.\n\n",n);
    }
    return 0;
}

Output:-

C Program To Check Number Is Armstrong Or Not using While Loop

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment