C Part-5 Loop Based Programs All Solutions
C Part-5 Loop Based Programs All Solutions
if (original == result)
printf("Armstrong number\n");
else
printf("Not an Armstrong number\n");
return 0;
}
while (N != 0) {
remainder = N % 10;
sum += remainder;
N /= 10;
}
while (N != 0) {
remainder = N % 10;
reversed = reversed * 10 + remainder;
N /= 10;
}
16. Check if a number is an Armstrong number (using a loop for digit processing)
#include <stdio.h>
#include <math.h>
int main() {
int N, original, remainder, result = 0, count = 0;
printf("Enter a number: ");
scanf("%d", &N);
original = N;
if (original == result)
printf("Armstrong number\n");
else
printf("Not an Armstrong number\n");
return 0;
}
18. Calculate the sum of the first N terms of the Fibonacci series
#include <stdio.h>
int main() {
int N, first = 0, second = 1, next, sum = 0;
printf("Enter N: ");
scanf("%d", &N);
20.Find the Greatest Common Divisor (GCD) of two numbers using a loop
#include <stdio.h>
int main() {
int a, b, gcd;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
21. Find the Least Common Multiple (LCM) of two numbers using a loop
#include <stdio.h>
int main() {
int a, b, lcm;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
lcm = (a > b) ? a : b;
while (1) {
if (lcm % a == 0 && lcm % b == 0) {
printf("LCM: %d\n", lcm);
break;
}
lcm++;
}
return 0;
}
22. Calculate the power of a number without using the pow() function
#include <stdio.h>
int main() {
int a, b, result = 1;
printf("Enter base and exponent: ");
scanf("%d %d", &a, &b);
(A perfect number is a number whose sum of factors, excluding itself, equals the number.)
#include <stdio.h>
int main() {
int N, sum = 0;
printf("Enter a number: ");
scanf("%d", &N);
if (sum == N)
printf("%d is a Perfect Number\n", N);
else
printf("%d is not a Perfect Number\n", N);
return 0;
}
return 0;
}
28. Print the decimal equivalent of a binary number (e.g., input binary 101 -> output
decimal 5)
#include <stdio.h>
#include <math.h>
int main() {
int binary, decimal = 0, remainder, power = 0;
printf("Enter a binary number: ");
scanf("%d", &binary);
while (binary != 0) {
remainder = binary % 10;
decimal += remainder * pow(2, power);
binary /= 10;
power++;
}