//3. Program to check whether a given number is Armstrong, odd or even, perfect square or cube.
#include<stdio.h>
#include<math.h>
void main()
{
int number,i,temp,sflag=0,cflag=0;
double remainder, result = 0,n=0;
clrscr();
printf("Enter a number: ");
scanf("%d", &number);
temp = number;
while (temp != 0)
{
temp =temp / 10;
++n;
//printf("n=%d\n",n); //This is for debugging.
}
temp = number;
while (temp != 0)
{
remainder = temp%10;
result = result + pow(remainder, n);
temp = temp / 10;
//fflush(stdin);
}
if(result == number)
printf("It is an Armstrong number.\n");
else
printf("It is not an Armstrong number.\n");
// True if the number is perfectly divisible by 2
if(number % 2 == 0)
printf("It is an even number.\n");
else
printf("It is an odd number.\n");
for(i=0;i<number/2;i++)
{
if(i*i==number)
{
printf("It is a perfect square.\n");
sflag=1;
}
if(i*i*i==number)
{
printf("It is a perfect cube.\n");
cflag=1;
}
}
if(sflag==0)
printf("It is not a perfect square.\n");
if(cflag==0)
printf("It is not a perfect cube.\n");
getch();
}
#include<stdio.h>
#include<math.h>
void main()
{
int number,i,temp,sflag=0,cflag=0;
double remainder, result = 0,n=0;
clrscr();
printf("Enter a number: ");
scanf("%d", &number);
temp = number;
while (temp != 0)
{
temp =temp / 10;
++n;
//printf("n=%d\n",n); //This is for debugging.
}
temp = number;
while (temp != 0)
{
remainder = temp%10;
result = result + pow(remainder, n);
temp = temp / 10;
//fflush(stdin);
}
if(result == number)
printf("It is an Armstrong number.\n");
else
printf("It is not an Armstrong number.\n");
// True if the number is perfectly divisible by 2
if(number % 2 == 0)
printf("It is an even number.\n");
else
printf("It is an odd number.\n");
for(i=0;i<number/2;i++)
{
if(i*i==number)
{
printf("It is a perfect square.\n");
sflag=1;
}
if(i*i*i==number)
{
printf("It is a perfect cube.\n");
cflag=1;
}
}
if(sflag==0)
printf("It is not a perfect square.\n");
if(cflag==0)
printf("It is not a perfect cube.\n");
getch();
}
No comments :
Post a Comment