0% found this document useful (0 votes)
2 views6 pages

20 C Programs With Output

The document contains a series of C programming examples demonstrating basic programming concepts such as printing output, arithmetic operations, control structures, and algorithms. Each example includes the code and its corresponding output, covering topics like factorial calculation, Fibonacci series, prime checking, and more. The examples serve as a practical guide for beginners to understand fundamental programming techniques in C.

Uploaded by

hafijask1812
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)
2 views6 pages

20 C Programs With Output

The document contains a series of C programming examples demonstrating basic programming concepts such as printing output, arithmetic operations, control structures, and algorithms. Each example includes the code and its corresponding output, covering topics like factorial calculation, Fibonacci series, prime checking, and more. The examples serve as a practical guide for beginners to understand fundamental programming techniques in C.

Uploaded by

hafijask1812
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/ 6

1.

Hello World
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}

Output: Hello, World!

2. Sum of Two Numbers


#include <stdio.h>
int main() {
int a = 5, b = 3;
printf("Sum: %d", a + b);
return 0;
}

Output: Sum: 8

3. Swap Two Numbers


#include <stdio.h>
int main() {
int a = 5, b = 3, temp;
temp = a;
a = b;
b = temp;
printf("a: %d, b: %d", a, b);
return 0;
}

Output: a: 3, b: 5

4. Check Even or Odd


#include <stdio.h>
int main() {
int num = 4;
if (num % 2 == 0)
printf("Even");
else
printf("Odd");
return 0;
}

Output: Even

5. Factorial using Loop


#include <stdio.h>
int main() {
int i, fact = 1, n = 5;
for(i = 1; i <= n; i++)
fact *= i;
printf("Factorial: %d", fact);
return 0;
}

Output: Factorial: 120

6. Fibonacci Series
#include <stdio.h>
int main() {
int n = 5, t1 = 0, t2 = 1, next;
for(int i = 1; i <= n; i++) {
printf("%d ", t1);
next = t1 + t2;
t1 = t2;
t2 = next;
}
return 0;
}

Output: 0 1 1 2 3

7. Reverse a Number
#include <stdio.h>
int main() {
int n = 123, rev = 0;
while(n != 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
printf("Reversed: %d", rev);
return 0;
}

Output: Reversed: 321

8. Check Prime Number


#include <stdio.h>
int main() {
int n = 7, isPrime = 1;
for(int i = 2; i <= n/2; i++) {
if(n % i == 0) {
isPrime = 0;
break;
}
}
if(isPrime)
printf("Prime");
else
printf("Not Prime");
return 0;
}

Output: Prime

9. Find GCD
#include <stdio.h>
int main() {
int a = 48, b = 18;
while(a != b) {
if(a > b) a -= b;
else b -= a;
}
printf("GCD: %d", a);
return 0;
}

Output: GCD: 6

10. Find LCM


#include <stdio.h>
int main() {
int a = 4, b = 5, max;
max = (a > b) ? a : b;
while(1) {
if(max % a == 0 && max % b == 0) {
printf("LCM: %d", max);
break;
}
max++;
}
return 0;
}

Output: LCM: 20

11. Reverse a String


#include <stdio.h>
#include <string.h>
int main() {
char str[] = "hello";
int len = strlen(str);
for(int i = len - 1; i >= 0; i--)
printf("%c", str[i]);
return 0;
}

Output: olleh

12. Count Digits


#include <stdio.h>
int main() {
int n = 12345, count = 0;
while(n != 0) {
n /= 10;
count++;
}
printf("Digits: %d", count);
return 0;
}

Output: Digits: 5

13. Armstrong Number


#include <stdio.h>
int main() {
int n = 153, sum = 0, temp, digit;
temp = n;
while(temp != 0) {
digit = temp % 10;
sum += digit * digit * digit;
temp /= 10;
}
if(sum == n)
printf("Armstrong");
else
printf("Not Armstrong");
return 0;
}

Output: Armstrong

14. Palindrome Number


#include <stdio.h>
int main() {
int n = 121, rev = 0, temp;
temp = n;
while(temp != 0) {
rev = rev * 10 + temp % 10;
temp /= 10;
}
if(rev == n)
printf("Palindrome");
else
printf("Not Palindrome");
return 0;
}

Output: Palindrome

15. Simple Calculator


#include <stdio.h>
int main() {
char op = '+';
int a = 5, b = 3;
switch(op) {
case '+': printf("Result: %d", a + b); break;
case '-': printf("Result: %d", a - b); break;
case '*': printf("Result: %d", a * b); break;
case '/': printf("Result: %d", a / b); break;
}
return 0;
}

Output: Result: 8

16. Print ASCII Values


#include <stdio.h>
int main() {
for(char c = 'A'; c <= 'Z'; c++)
printf("%c: %d\n", c, c);
return 0;
}

Output: A: 65 ... Z: 90

17. Power of Number


#include <stdio.h>
int main() {
int base = 2, exp = 3, result = 1;
for(int i = 1; i <= exp; i++)
result *= base;
printf("Power: %d", result);
return 0;
}

Output: Power: 8

18. Print Multiplication Table


#include <stdio.h>
int main() {
int n = 5;
for(int i = 1; i <= 10; i++)
printf("%d x %d = %d\n", n, i, n*i);
return 0;
}

Output: 5 x 1 = 5 ... 5 x 10 = 50

19. Sum of Digits


#include <stdio.h>
int main() {
int n = 123, sum = 0;
while(n != 0) {
sum += n % 10;
n /= 10;
}
printf("Sum: %d", sum);
return 0;
}

Output: Sum: 6

20. Largest of Three Numbers


#include <stdio.h>
int main() {
int a = 10, b = 20, c = 15;
if(a >= b && a >= c)
printf("Largest: %d", a);
else if(b >= a && b >= c)
printf("Largest: %d", b);
else
printf("Largest: %d", c);
return 0;
}

Output: Largest: 20

You might also like