Showing posts with label Armstrong. Show all posts
Showing posts with label Armstrong. Show all posts

Check the given number is armstrong number or not using c program.

How To Check the given number is armstrong number or not using c program.

#include<stdio.h>
int main(){
int num,r,sum=0,temp;
printf("\nEnter a number:-");
scanf("%d",&num);
temp=num;
while(num!=0){
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("\nThe number %d is an armstrong number",temp);
else
printf("\nThe number %d is not an armstrong number",temp);
return 0;
}

C Program To Find Armstrong Number

How to write a C Program To Find Armstrong Number in C Programming Language ?


Solution:

#include<stdio.h>
#include<conio.h>

main()
{
   int number, sum = 0, temp, remainder;

   printf("Enter a number\n");    
   scanf("%d",&number);

   temp = number;

   while( temp != 0 )
   {
      remainder = temp%10;
      sum = sum + remainder*remainder*remainder;
      temp = temp/10;
   }

   if ( number == sum )
      printf("Entered number is an armstrong number.");
   else
      printf("Entered number is not an armstrong number.");      

   getch();
   return 0;
}

Armstrong Number Checker in C

How to write a C Program to Armstrong Number Checker in C Programming Language ?


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.


Solution:

  1. #include <stdio.h>
  2.  
  3. int numbers[50];
  4. int digit, i=0, j, sum=0, lengthOfNumber, armstrong;
  5.  
  6. int separateDigits(num){
  7. // This function first takes modulus by dividing the number by 10 and then actually divides it by 10.
  8.     while(num>0){
  9.         numbers[i]=num%10;
  10.         num/= 10;
  11.         i++;
  12.     }
  13.     lengthOfNumber=i;
  14. }
  15. int checkArmstrong(originalNumber){
  16.     for(j=0; j<=lengthOfNumber; j++){
  17.         sum+=numbers[j]*numbers[j]*numbers[j];
  18.     }
  19.     if(sum==originalNumber){
  20.         return 1;
  21.     }
  22.     else return 0;
  23. }
  24.  
  25.  
  26. int main()
  27. {
  28.     printf("Enter Number : ");
  29.     scanf("%d", &digit);
  30.     separateDigits(digit);              //We separate digits by calling a function separateDigits and giving the input as argument.
  31.     armstrong = checkArmstrong(digit);          // Variable 'armstrong' holds the value that the function returns.
  32.     if(armstrong==1){
  33.         printf("\nNumber is Armstrong !");
  34.     }
  35.     else printf("\nNumber is not Armstrong!");
  36.    return 0;
  37. }

Input Number and Check if it's Armstrong C Program

How to write a C Program to input a number and check if it's Armstrong in C Programming Language ?



Solution:
//Program to input a number and check if it's Armstrong.
#include<stdio.h>
#include<conio.h>

void main()
{
int number, sum = 0, temp;

clrscr();

printf("Enter number: ");
scanf("%d", &number);

temp = number;

while(number != 0) {
sum = sum + ((number % 10) * (number % 10) * (number % 10));
number = number / 10;
}

if(sum == temp)
printf("The number %d is an Armstrong number.", temp);
else
printf("The number %d is not an Armstrong number.", temp);

getch();
}

Write a C program to enter any number and check whether the number is Armstrong number.