Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
22 views8 pages

Solve

The document contains a series of C programming tasks and their corresponding solutions. Each task addresses a specific problem, such as reversing a number, checking for palindromes, counting digits, and finding the GCD and LCM. The solutions are provided in the form of complete C code snippets.

Uploaded by

atoshitabassum4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views8 pages

Solve

The document contains a series of C programming tasks and their corresponding solutions. Each task addresses a specific problem, such as reversing a number, checking for palindromes, counting digits, and finding the GCD and LCM. The solutions are provided in the form of complete C code snippets.

Uploaded by

atoshitabassum4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

Print the reverse of a number


Answer:
#include <stdio.h>

int main() {
int n = 12345, rev = 0;
while (n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
printf("Reverse: %d\n", rev);
return 0;
}

2. Check whether a number is a palindrome


Answer:
#include <stdio.h>

int main() {
int n = 121, temp = n, rev = 0;
while (temp > 0) {
rev = rev * 10 + temp % 10;
temp /= 10;
}
if (n == rev) {
printf("Palindrome\n");
} else {
printf("Not Palindrome\n");
}
return 0;
}
3. Count digits, sum of digits, and product of digits of a number
Answer:
#include <stdio.h>

int main() {
int n = 1234, sum = 0, prod = 1, count = 0;
while (n > 0) {
int d = n % 10;
sum += d;
prod *= d;
count++;
n /= 10;
}
printf("Digits: %d Sum: %d Product: %d\n", count, sum, prod);
return 0;
}
4. Print the multiplication table of any number (up to n)
Answer:
#include <stdio.h>

int main() {
int number, upto;
printf("Enter number for multiplication table: ");
scanf("%d", &number);
printf("Enter range up to: ");
scanf("%d", &upto);
printf("Multiplication Table:\n");
for (int i = 1; i <= upto; i++)
printf("%d x %d = %d\n", number, i, number * i);
return 0;
}
5. Print all prime numbers between 1 to N
Answer:
#include <stdio.h>

int main() {
int N = 50;
for (int i = 2; i <= N; i++) {
int prime = 1;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
prime = 0;
break;
}
}
if (prime) printf("%d ", i);
}
return 0;
}
6. Find the power of a number using a loop
Answer:
#include <stdio.h>

int main() {
int base = 3, exp = 4, result = 1;
for (int i = 0; i < exp; i++)
result *= base;
printf("Power: %d\n", result);
return 0;
}

7. Print a pyramid pattern of stars


*
***
*****
Answer:
#include <stdio.h>

int main() {
int rows = 3;
for (int i = 1; i <= rows; i++) {
for (int j = 0; j < rows - i; j++) printf(" ");
for (int j = 0; j < 2 * i - 1; j++) printf("*");
printf("\n");
}
return 0;
}
8. Find the GCD (HCF) and LCM of two numbers using loops
Answer:
#include <stdio.h>

int main() {
int a = 36, b = 60, gcd = 1;
for (int i = 1; i <= a && i <= b; i++)
if (a % i == 0 && b % i == 0) gcd = i;
int lcm = (a * b) / gcd;
printf("GCD: %d LCM: %d\n", gcd, lcm);
return 0;
}

9. Print a number pyramid


Example:
1
22
333
Answer:
#include <stdio.h>

int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 0; j < i; j++)
printf("%d ", i);
printf("\n");
}
return 0;
}
10. Check whether a given number is perfect or not
A perfect number is equal to the sum of its divisors (excluding itself). Example: 6 → 1 + 2 + 3 = 6
Answer:
#include <stdio.h>

int main() {
int n = 28, sum = 0;

for (int i = 1; i < n; i++) {


if (n % i == 0)
sum += i;
}

if (sum == n) {
printf("%d is a Perfect Number\n", n);
} else {
printf("%d is Not a Perfect Number\n", n);
}

return 0;
}
11. Count the number of vowels, consonants, digits, and spaces in a string​
Answer:
NOTE: SKIP THIS PROBLEM
12. Count the frequency of each digit in a number
Example: Input: 112233 → Output: 1=2, 2=2, 3=2
Answer:
#include <stdio.h>

int main() {
int num = 112233;
for (int d = 0; d <= 9; d++) {
int count = 0, t = num;
while (t > 0) {
if (t % 10 == d) ​
{ count++; }
t /= 10;
}
if (count > 0)​
{ printf("%d = %d\n", d, count); }
}
return 0;
}
13. Convert a decimal number to binary using a loop​
Answer:
#include <stdio.h>

int main() {
int decimal = 13, binary = 0, place = 1;
while (decimal > 0) {
int r = decimal % 2;
binary += r * place;
place *= 10;
decimal /= 2;
}
printf("Binary: %d\n", binary);
return 0;
}

14. Take 10 numbers as input from user and find the max and min​
Answer:
#include <stdio.h>

int main() {
int num, max, min;

printf("Enter number 1: ");


scanf("%d", &num);
max = min = num; // Initialize both to the first input

for (int i = 2; i <= 10; i++) {


printf("Enter number %d: ", i);
scanf("%d", &num);

if (num > max)


max = num;

if (num < min)


min = num;
}

printf("Maximum: %d\n", max);


printf("Minimum: %d\n", min);

return 0;
}​

15. Take a number and count how many even and odd digits it has
Answer:
#include <stdio.h>

int main() {
int n = 123456, even = 0, odd = 0, d;
while (n > 0) {
d = n % 10;
if (d % 2 == 0)
{ even++; }
else
{ odd++; }
n /= 10;
}
printf("Even: %d Odd: %d\n", even, odd);
return 0;
}

You might also like