1. Find the sum of digits of a given number.
Sample input: 7895
output: 29
#include <stdio.h>
int main()
{
int num;
int sum = 0;
printf("Enter number:");
scanf("%d", &num);
while(num) {
sum += num % 10;
num /= 10;
}
printf("sum of digits is %d\n", sum);
}
input:
90091
output:
sum of digits is 19
2. Reverse the digits of a given number.
Sample input: 547
output: 745
#include <stdio.h>
int main()
{
int num;
int val = 0;
printf("Enter a number:");
scanf("%d", &num);
while(num) {
val = val * 10 + num % 10;
num /= 10;
}
printf("val=%d\n", val);
}
input:
98651
output:
15689
3. Program to find the value of x power y
sample input:
value = 5
power = 2
output:
5 ^ 2 = 25
#include <stdio.h>
int power_of(int x, int y)
{
int val = 1;
while (y--) {
val *= x;
}
return val;
}
int main()
{
int x, y;
printf("enter a number:");
scanf("%d", &x);
printf("enter power value:");
scanf("%d", &y);
printf("%d ^ %d = %d\n", x, y, power_of(x, y));
}
input
enter a number:2
enter power value:10
output:
2 ^ 10 = 1024
4. Print first n prime numbers
#include <stdio.h>
#include <stdbool.h>
bool is_prime(int val)
{
int i;
if (val < 2)
return false;
for(i = 2; i <= val/2; i++) {
if (val % i == 0)
return false;
}
return true;
}
int main()
{
int num;
int cnt = 0;
int i = 0;
printf("Enter n value:");
scanf("%d", &num);
printf("The first %d prime numbers are:\n", num);
while (cnt < num) {
if (is_prime(i)) {
printf("%d ", i);
cnt++;
}
i++;
}
printf("\n");
}
input
Enter n value:20
output
The first 20 prime numbers are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
5. Find the Factorial of a number
The factorial of a number is the product of all positive integers from 1 up to that number.
Example: Factorial of 6 is 6×5×4×3×2×1=720
#include <stdio.h>
int main()
{
int num;
unsigned int fact = 1;
printf("Enter a number to find its factorial:");
scanf("%d", &num);
if (num == 0)
fact = 0;
while (num)
fact *= num--;
printf("factorial is %u\n", fact);
}
input
Enter a number to find its factorial:2
output
factorial is 2
6. Program to print first n numbers from Fibonacci series:
The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. The sequence typically begins as follows:
0,1,1,2,3,5,8,13,21,34,…
#include <stdio.h>
#include <string.h>
int main()
{
int i = 0;
int prev = 1, tmp;
int cnt;
printf("Enter the count in Fibonacci series:");
scanf("%d", &cnt);
while (cnt--) {
printf("%d, ", i);
tmp = i;
i += prev;
prev = tmp;
}
printf("\n");
}
input:
Enter the count in Fibonacci series:15
output:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,
7. Find the LCM of two numbers
The Least Common Multiple (LCM) of two or more numbers is the smallest positive integer that is divisible by each of the numbers.
#include <stdio.h>
int main()
{
int num1, num2;
int x,y;
printf("enter two numbers:\n");
scanf("%d %d", &num1, &num2);
x = num1;
y = num2;
while (x != y) {
if (x < y)
x += num1;
else
y += num2;
}
printf("LCM of %d and %d is %d\n", num1, num2, x);
}
input
enter two numbers:
6
9
output
LCM of 6 and 9 is 18
8. Find the HCF of a given two numbers
The Highest Common Factor (HCF), also known as the Greatest Common Divisor (GCD), of two or more numbers is the largest positive integer that divides each of the numbers without leaving a remainder.
#include <stdio.h>
int main()
{
int num1, num2;
int x, y;
printf("enter two numbers:\n");
scanf("%d %d", &num1, &num2);
x = num1;
y = num2;
while (x != y) {
if (x > y)
x -= y;
else
y -= x;
}
printf("The HCF of %d and %d is %d\n", num1, num2, x);
}
input
enter two numbers:
28
32
output
The HCF of 28 and 32 is 4