Showing posts with label solution. Show all posts
Showing posts with label solution. Show all posts

Wednesday, 6 April 2016

How To Identify Armstrong Numbers Using Turbo C Compiler

According to illuminations.nctm.org, an Armstrong number is an n-digit number that is equal to the sum of the nth powers of its digits.
To have a clearer understanding of what an Armstrong number is, I have prepared below a solution on how to determine if a number is an Armstrong or not.

#include<math.h>

#include<stdio.h>
void armstrong(void);
main()
{
clrscr();
armstrong();
getch();
}
void armstrong(void)
{
long count=0,m,n,m1,q,r,a,b,sum=0;
scanf("%ld",&n);
m=n;
m1=n;
for(;n!=0;)
{
r=n/10;
count++;
n=r;
}
for(;m!=0;)
{
a=m/10;
b=m%10;
sum=sum+ pow(b,count);
m=a;
}
if(sum==m1)
{
printf("Armstrong");
}
else
{
printf("Not");
}
}


How To Identify Happy Numbers Using Turbo C Compiler

What are happy numbers?

According to Wikipedia...
happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number either equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers).

Using Turbo C compiler for the C programming language...


#include<stdio.h>
void happynumber(void);
main()
   {
   clrscr();
   happynumber();
   getch();
   }

void happynumber(void)
   {
   int n,q,r,t=0,i;
   scanf("%d",&n);

  for(i=1;i<1000;i++)
   { t=0;
     for(;;)
       {
       q=n/10;
       r=n%10;
       t=t+(r*r);
       if(q==0)
{
n=t;
break;
}
n=q;
}
     }
   if(t==1)
      {
      printf("happy number");

      }
   else
      {
      printf("not");
      }



   }