C Programs with Outputs
C Programs:
// 1. Hello World
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
// 2. Swap Two Numbers (Without Third Variable)
#include <stdio.h>
int main() {
int a = 5, b = 10;
a = a + b; b = a - b; a = a - b;
printf("Swapped: a=%d, b=%d\n", a, b);
return 0;
}
// 3. Check Even or Odd
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) printf("Even\n");
else printf("Odd\n");
return 0;
}
// 4. Largest of Three Numbers
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d%d%d", &a, &b, &c);
if (a > b && a > c) printf("%d is largest\n", a);
else if (b > c) printf("%d is largest\n", b);
else printf("%d is largest\n", c);
return 0;
}
// 5. Reverse a Number
#include <stdio.h>
int main() {
int num, rev = 0, rem;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
rem = num % 10;
rev = rev * 10 + rem;
num /= 10;
}
printf("Reversed: %d\n", rev);
return 0;
}
// 6. Factorial (Iterative)
#include <stdio.h>
int main() {
int n, i;
long fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) fact *= i;
printf("Factorial: %ld\n", fact);
return 0;
}
// 7. Fibonacci Series (Loop)
#include <stdio.h>
int main() {
int n, t1 = 0, t2 = 1, next;
printf("Enter number of terms: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
printf("%d ", t1);
next = t1 + t2;
t1 = t2;
t2 = next;
}
return 0;
}
// 8. Reverse an Array
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printf("Reversed Array: ");
for (int i = 4; i >= 0; i--)
printf("%d ", arr[i]);
return 0;
}
// 9. Palindrome String
#include <stdio.h>
#include <string.h>
int main() {
char str[100], rev[100];
printf("Enter a string: ");
scanf("%s", str);
strcpy(rev, str);
strrev(rev);
if (strcmp(str, rev) == 0) printf("Palindrome\n");
else printf("Not a Palindrome\n");
return 0;
}
// 10. Star Pyramid
#include <stdio.h>
int main() {
int i, j, n;
printf("Enter rows: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++)
printf("* ");
printf("\n");
}
return 0;
}
Expected Outputs:
1. Hello World
Output:
Hello, World!
2. Swap Two Numbers (Without Third Variable)
Output:
Swapped: a=10, b=5
3. Check Even or Odd
Input: 5
Output:
Odd
4. Largest of Three Numbers
Input: 4 7 2
Output:
7 is largest
5. Reverse a Number
Input: 1234
Output:
Reversed: 4321
6. Factorial (Iterative)
Input: 5
Output:
Factorial: 120
7. Fibonacci Series (Loop)
Input: 5
Output:
0 1 1 2 3
8. Reverse an Array
Output:
Reversed Array: 5 4 3 2 1
9. Palindrome String
Input: madam
Output:
Palindrome
10. Star Pyramid
Input: 4
Output:
*
* *
* * *
* * * *