a)C Program to Check Armstrong for 3 digit number
Source Code:
#include <stdio.h>
int main() {
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
result += remainder * remainder * remainder;
originalNum /= 10;
}
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
OutPut:
Enter a three-digit integer: 371
371 is an Armstrong number.
b) C Program to Check Armstrong for any digit number
Source Code:
#include <math.h>
#include <stdio.h>
int main() {
int num, originalNum, remainder, n = 0;
float result = 0.0;
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num;
for (originalNum = num; originalNum != 0; ++n) {
originalNum /= 10;
}
for (originalNum = num; originalNum != 0; originalNum /= 10) {
remainder = originalNum % 10;
result += pow(remainder, n);
}
if ((int)result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
#include <stdio.h>
int main() {
int num, originalNum, remainder, n = 0;
float result = 0.0;
printf("Enter an integer: ");
scanf("%d", &num);
originalNum = num;
for (originalNum = num; originalNum != 0; ++n) {
originalNum /= 10;
}
for (originalNum = num; originalNum != 0; originalNum /= 10) {
remainder = originalNum % 10;
result += pow(remainder, n);
}
if ((int)result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
Output:
Enter an integer: 1634
1634 is an Armstrong number.
C) C Program for Tower of Hanoi Problem Using Recursion
Source Code:
#include<stdio.h>
void TOH(int,char,char,char);
void main()
{
int n;
printf("How many plates?");
scanf("%d",&n);
TOH(n,'A','B','C');
}
void TOH(int n,char x,char y,char z)
{
if(n>0)
{
TOH(n-1,x,z,y);
printf("\n%c -> %c",x,y);
TOH(n-1,z,y,x);
}
}
Output:
How many plates?3
A -> B
A -> C
B -> C
A -> B
A -> B
C -> A
C -> B
A -> B
No comments:
Post a Comment