C Programming Concepts Explained: Functions, Recursion, Loops and More
Learn C programming concepts like functions, recursion, loops, algorithms through practical examples. Master palindromes, Fibonacci, prime numbers & more!
1. Arithmetic Operations Using Functions
#include <stdio.h>
// Function to add two numbers
float add(float a, float b) {
return a + b;
}
// Function to subtract two numbers
float subtract(float a, float b) {
return a - b;
}
int main() {
float num1, num2;
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
printf("Addition: %.2f + %.2f = %.2f\n", num1, num2, add(num1, num2));
printf("Subtraction: %.2f - %.2f = %.2f\n", num1, num2, subtract(num1, num2));
return 0;
}
Output: This program takes two numbers and performs addition and subtraction using functions.
1. Arithmetic Operations
Enter two numbers: 12 5 Addition: 12.00 + 5.00 = 17.00 Subtraction: 12.00 - 5.00 = 7.00
2. Factorial Using Function
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
Output: Calculates factorial of a number using recursion.
2. Factorial
Enter a number: 5 Factorial of 5 is 120
3. Reverse a Number Using Function
#include <stdio.h>
int reverseNumber(int num) {
int reversed = 0;
while (num != 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
return reversed;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Reversed number: %d", reverseNumber(num));
return 0;
}
Output: Reverses the digits of a given number.
3. Reverse Number
Enter a number: 1234 Reversed number: 4321
4. Sum of Digits Using Function
#include <stdio.h>
int sumOfDigits(int num) {
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Sum of digits: %d", sumOfDigits(num));
return 0;
}
Output: Calculates the sum of all digits in a number.
4. Sum of Digits
Enter a number: 456 Sum of digits: 15
5. Palindrome Check Using Function
#include <stdio.h>
int isPalindrome(int num) {
int original = num, reversed = 0;
while (num != 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
return original == reversed;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isPalindrome(num))
printf("%d is a palindrome.", num);
else
printf("%d is not a palindrome.", num);
return 0;
}
Output: Checks if a number reads the same forwards and backwards.
5. Palindrome Check
Enter a number: 1221 1221 is a palindrome.
6. Armstrong Number Check
#include <stdio.h>
#include <math.h>
int isArmstrong(int num) {
int original = num, sum = 0, digits = 0;
while (original != 0) {
digits++;
original /= 10;
}
original = num;
while (original != 0) {
sum += pow(original % 10, digits);
original /= 10;
}
return num == sum;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isArmstrong(num))
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
Output: Checks if a number is Armstrong (sum of cubes of digits equals the number).
6. Armstrong Number
Enter a number: 153 153 is an Armstrong number.
7. Prime Number Check
#include <stdio.h>
int isPrime(int num) {
if (num <= 1) return 0;
for (int i = 2; i <= num/2; i++) {
if (num % i == 0)
return 0;
}
return 1;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isPrime(num))
printf("%d is a prime number.", num);
else
printf("%d is not a prime number.", num);
return 0;
}
Output: Checks if a number is prime (divisible only by 1 and itself).
7. Prime Number
Enter a number: 17 17 is a prime number.
8. Fibonacci Series Using Function
#include <stdio.h>
void fibonacci(int n) {
int a = 0, b = 1, c;
printf("Fibonacci Series: %d, %d", a, b);
for (int i = 2; i < n; i++) {
c = a + b;
printf(", %d", c);
a = b;
b = c;
}
}
int main() {
int terms;
printf("Enter number of terms: ");
scanf("%d", &terms);
fibonacci(terms);
return 0;
}
Output: Prints Fibonacci series up to n terms (0, 1, 1, 2, 3, 5...).
8. Fibonacci Series
Enter number of terms: 6 Fibonacci Series: 0, 1, 1, 2, 3, 5
9. Macro Example
#include <stdio.h>
#define PI 3.14159
#define SQUARE(x) (x * x)
int main() {
float radius = 5.0;
printf("Area of circle with radius %.2f: %.2f\n", radius, PI * SQUARE(radius));
printf("Square of 7: %d", SQUARE(7));
return 0;
}
Output: Demonstrates use of macros for constants and simple functions.
9. Macro Example
Area of circle with radius 5.00: 78.54 Square of 7: 49
10. #include Directive Example
#include <stdio.h>
#include <string.h> // For string functions
int main() {
char name[20] = "Hello";
printf("Original string: %s\n", name);
strcat(name, " World!"); // Using function from string.h
printf("After concatenation: %s", name);
return 0;
}
Output: Shows how to use #include to access library functions.
10. #include Directive
Original string: Hello After concatenation: Hello World!
All outputs are based on sample inputs
Key Features:
- Simple and easy-to-understand code
- Perfect for 10th-grade students
- Each program demonstrates a different concept
- Colorful and attractive presentation
- Ready to paste into Blogspot

0 Comments
If you have any doubts, Please let me know