Many Simple C Programs are asked in interviews and university exams. C Programming language is a high-level general-purpose language that is highly used for developing operating and embedded systems.
It is known as the “Mother of all programming languages” as it forms the basis of derivation of all other programming languages. If you are a beginner looking for a few simple C programs to solve, then read this article.
Top 20 Simple C Programs for Beginners
Check some of the simple C programs at the beginner level to start your programming journey.
1. Write a Program for taking input from users.
Simple C Program to take input from users |
#include <stdio.h>
int main() { int integerInput; float floatInput; char charInput; // Taking integer input printf (“Enter an integer: “); scanf (“%d”, &integerInput); //Use scanf() to take user input // Taking floating-point input printf (“Enter a float: “); scanf (“%f”, &floatInput); // Taking character input (skipping leading whitespace) printf (“Enter a character: “); scanf (” %c”, &charInput); // Displaying the inputs received printf (“\nInteger entered: %d\n”, integerInput); printf (“Float entered: %.2f\n”, floatInput); printf (“Character entered: %c\n”, charInput); return 0; } |
Output
2. Write a Simple C Program to find the ASCII value of a character.
Simple C Program to take input from users |
#include<stdio.h>
int main() { char c; printf(“Enter a character : “); scanf(“%c” , &c); printf (“\n\nASCII value of given character %c = %d”,c,c); return 0; } |
Output
Q3. Write a Simple C program to reverse the case of the input character.
Simple C Program to reverse the case of input |
#include<stdio.h>
#include<ctype.h> int main() { char alpha; printf(“Enter an alphabet : “); putchar(‘\n’); alpha=getchar(); printf(“\n\nReverse case of %c is : “,alpha); if(islower(alpha)) //Check if it is in lowercase putchar(toupper(alpha)); else printf(“%c”,tolower(alpha)) ; //Convert into lowercase return 0; } |
Output
Q4. Write a simple c program to check whether an input is a vowel or not.
We will use Switch cases to check whether the given input is a vowel or not. Check the implementation in the table below.
Simple C Program to check for Vowel |
#include<stdio.h>
int main() { char ch; printf (“Enter any Character : “); scanf (“%c”, &ch); switch(ch) { case ‘a’: case ‘A’: case ‘e’: case ‘E’: case ‘i’: case ‘I’: case ‘o’: case ‘O’: case ‘u’: case ‘U’: printf(“\n\n%c is a vowel.\n\n”, ch); break; default: printf(“%c is not a vowel.\n\n”, ch); } return 0; } |
Output
Q5. Write a simple C program to swap two numbers.
We will use a temporary variable to swap the given two numbers. Check the implementation below.
Simple C Program to swap two numbers |
#include <stdio.h>
int main() { int num1, num2, temp; // Input the two numbers printf(“Enter number 1: “); scanf(“%d”, &num1); printf(“Enter number 2: “); scanf(“%d”, &num2); // Display the original numbers printf(“\nBefore swapping:\n”); printf(“Number 1: %d\n”, num1); printf(“Number 2: %d\n”, num2); // Swap the numbers using a temporary variable temp = num1; num1 = num2; num2 = temp; // Display the swapped numbers printf(“\nAfter swapping:\n”); printf(“Number 1: %d\n”, num1); printf(“Number 2: %d\n”, num2); return 0; } |
Output
Q6. Write a simple C program to find the factorial of a number using for loop.
Simple C Program to find factorial of a number |
#include<stdio.h>
void main() { int i, n; long int fact = 1; printf(“Enter a number of your choice: “); scanf(“%d” , &n); for(i = 1; i <= n; i++) { fact = fact * i; } printf(“Factorial of number %d is %ld”, n , fact); } |
Output
Q7. Write a simple C program to print the Fibonacci series.
Simple C Program to print Fibonacci Series |
#include <stdio.h>
void fibonacci(int num); int main() { int num = 0; printf(“Enter the number of terms in the Fibonacci series: “); scanf(“%d”, &num); fibonacci(num); // Call fibonacci function return 0; } void fibonacci(int num) { int a = 0, b = 1, c, i; if (num == 1) { printf(“%d\n”, a); return; } else if (num >= 2) { printf(“%d\t%d”, a, b); } for (i = 3; i <= num; i++) { c = a + b; printf(“\t%d”, c); a = b; b = c; } printf(“\n”); } |
Output
Q8. Write a simple C program to find the average of N numbers.
Simple C Program to print average of n numbers |
#include <stdio.h>
int main() { int N, i; float number, sum = 0.0, average; printf(“Enter the number of elements: “); scanf(“%d”, &N); // Prompt user to enter N numbers printf(“Enter %d numbers:\n”, N); for (i = 0; i < N; i++) { scanf(“%f”, &number); sum += number; // Add each number to the sum } // Calculate average if (N > 0) { average = sum / N; printf(“Average of the entered numbers = %.2f\n”, average); } else { printf(“Cannot calculate average. Number of elements should be greater than zero.\n”); } return 0; } |
Output
Q9. Write a simple C program to find the factors of a given number.
Simple C Program to print the factors of a number |
#include<stdio.h>
int main() { int num, i; printf(“Enter any number to find the factors of : “); scanf(“%d”,&num); printf(“\n\n\nFactors of %d are \n\n”, num); for(i = 1; i <= num/2; i++) { if(num%i == 0) printf(“\t\t\t%d\n”, i); } return 0; } |
Output
Q10. Write a simple C program to check if a given number is an integer or float.
Simple C Program to check for integer or floating numbers |
#include<conio.h>
#include<string.h> int main() { char number[10]; int flag = 0; int length, i = 0; printf(“\n\nEnter a number: “); scanf(“%s”, number); length = strlen(number); // Keep checking until the end. while(number[i++] != ‘\0’) { if(number[i] == ‘.’) { flag = 1; break; } } if(flag) printf(“\nEntered Number is a Floating point Number\n”); else printf(“\nEntered Number is a integer Number\n”); return 0; } |
Output
Q11. Write a simple C program to reverse an array.
Simple C Program to reverse an array |
#include <stdio.h>
// Function to swap two integers void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } // Function to reverse an array void reverseArray(int arr[], int size) { int start = 0; int end = size – 1; while (start < end) { // Swap elements at start and end indices swap(&arr[start], &arr[end]); // Move start index forward start++; // Move end index backward end–; } } void printArray(int arr[], int size) { printf(“Reversed Array: “); for (int i = 0; i < size; i++) { printf(“%d “, arr[i]); } printf(“\n”); } int main() { int arr[100]; // Assuming maximum size of array is 100 int size; // Input the size of the array printf(“Enter the size of the array: “); scanf(“%d”, &size); // Input the elements of the array printf(“Enter %d elements:\n”, size); for (int i = 0; i < size; i++) { scanf(“%d”, &arr[i]); } // Reverse the array reverseArray(arr, size); // Print the reversed array printArray(arr, size); return 0; } |
Output
Q12. Write a simple C program to insert an element in an array.
Simple C Program to insert an element in an array |
#include<stdio.h>
int main() { int array[], position, c, n, value; printf(“\nEnter total number of elements in array:”); scanf(“%d”, &n); printf(“\n\nEnter %d elements\n”, n); for(c = 0; c < n; c++) scanf(“%d”, &array[c]); printf(“\n\nEnter the location where you want to insert the new element: “); scanf(“%d”, &position); printf(“\n\nEnter the value to insert: “); scanf(“%d”, &value); // Shift the elements to right for(c = n-1; c >= position-1; c–) array[c+1] = array[c]; array[position – 1] = value; // insert the value printf(“\n\nResultant array is: “); for(c = 0; c <= n; c++) printf(“%d “, array[c]); return 0; } |
Output
Q13. Write a simple C Program to remove duplicates from a sorted array.
Simple C Program to remove duplicate |
#include <stdio.h>
int remove_duplicate(int arr[], int n) { if (n == 0 || n == 1) return n; int temp[n]; int j = 0; int i; for (i = 0; i < n – 1; i++) if (arr[i] != arr[i + 1]) temp[j++] = arr[i]; temp[j++] = arr[n – 1]; for (i = 0; i < j; i++) arr[i] = temp[i]; return j; } int main() { printf(“Enter the size of array: “); int n; scanf(“%d”, &n); int arr[n]; int i; for (i = 0; i < n; i++) { scanf(“%d”, &arr[i]); } printf(“\n Original Array Before Removing Duplicates: “); for (i = 0; i < n; i++) printf(“%d “, arr[i]); n = remove_duplicate(arr, n); printf(“\nArray After Removing Duplicates: “); for (i = 0; i < n; i++) printf(“%d “, arr[i]); return 0; } |
Output
Q14. Write a simple C program to print the sum of n numbers.
Simple C Program to print sum of n numbers |
#include<stdio.h>
int main() { int n, sum = 0, i, array[100]; printf(“Enter the number of integers to add: “); scanf(“%d”, &n); printf(“\n\nEnter %d integers \n\n”, n); for(i = 0; i< n; i++) { scanf(“%d”, &array[i]); sum += array[i]; // same as sum = sum + array[c] } printf(“\n\nSum = %d\n\n”, sum); return 0; } |
Output
Q15. Write a simple C program to find palindrome using recursion.
Simple C Program to find Palindrome |
#include <stdio.h>
#include <string.h> #include <ctype.h> // Function to check if a character is a valid alphabet letter int isAlphabet(char ch) { return (ch >= ‘a’ && ch <= ‘z’) || (ch >= ‘A’ && ch <= ‘Z’); } // Function to convert a character to lowercase char toLower(char ch) { if (ch >= ‘A’ && ch <= ‘Z’) { return ch + (‘a’ – ‘A’); } return ch; } // Recursive function to check if a string is a palindrome int isPalindrome(char str[], int left, int right) { // Base case: if the left index is greater than or equal to the right index if (left >= right) { return 1; // It’s a palindrome } // Ignore non-alphabet characters and move to the next character if (!isAlphabet(str[left])) { return isPalindrome(str, left + 1, right); } if (!isAlphabet(str[right])) { return isPalindrome(str, left, right – 1); } // Convert characters to lowercase for case-insensitive comparison char leftChar = toLower(str[left]); char rightChar = toLower(str[right]); // Recursive comparison if (leftChar == rightChar) { return isPalindrome(str, left + 1, right – 1); } else { return 0; // Not a palindrome } } int main() { char str[100]; printf(“Enter a string: “); fgets(str, sizeof(str), stdin); // Remove newline character if present in the string str[strcspn(str, “\n”)] = ‘\0’; int len = strlen(str); int result = isPalindrome(str, 0, len – 1); if (result == 1) { printf(“‘%s’ is a palindrome.\n”, str); } else { printf(“‘%s’ is not a palindrome.\n”, str); } return 0; } |
Output
Q16. Write a simple program to find largest element in an array.
Simple C Program to find largest element in an array |
#include <stdio.h>
int findLargest(int arr[], int size) { //Base case if (size == 1) { return arr[0]; } int maxRest = findLargest(arr + 1, size – 1); return (arr[0] > maxRest) ? arr[0] : maxRest; } int main() { int arr[100]; int size; printf(“Enter the size of the array: “); scanf(“%d”, &size); printf(“Enter %d elements:\n”, size); for (int i = 0; i < size; i++) { scanf(“%d”, &arr[i]); } int largest = findLargest(arr, size); //Calling the function recursively printf(“The largest element in the given array is: %d\n”, largest); return 0; } |
Output
Q17. Write a simple C program to Find the LCM of two given numbers.
Simple C Program to find LCM |
#include <stdio.h>
int gcd(int a, int b) { while (b != 0) { int temp = b; b = a % b; a = temp; } return a; } //Function to calculate LCM of two given number. int lcm(int a, int b) { int abs_a = (a < 0) ? -a : a; //Convert into absoulte value if negative int abs_b = (b < 0) ? -b : b; // Use the formula: LCM(a, b) = (|a * b|) / GCD(a, b) int gcd_value = gcd(abs_a, abs_b); int lcm_value = (abs_a * abs_b) / gcd_value; return lcm_value; } int main() { int num1, num2; // Input two numbers from the user printf(“Enter first number: “); scanf(“%d”, &num1); printf(“Enter second number: “); scanf(“%d”, &num2); // Calculate the LCM of the two numbers int result = lcm(num1, num2); // Display the LCM printf(“LCM of %d and %d is: %d\n”, num1, num2, result); return 0; } |
Output
Q18. Write a simple C program to add two numbers using recursion.
Simple C Program to add two number |
#include <stdio.h>
// Recursive function to add two numbers int add(int a, int b) { // Base case: if the second number is 0, return the first number if (b == 0) { return a; } // Recursive case: add one to the first number and decrement the second number return add(a + 1, b – 1); } int main() { int num1, num2; printf(“Enter first number: “); scanf(“%d”, &num1); printf(“Enter second number: “); scanf(“%d”, &num2); int sum = add(num1, num2); //Call the function recursively // Display the result printf(“Sum of %d and %d is: %d\n”, num1, num2, sum); return 0; } |
Output
Q19. Write a simple C program to check input numbers for odd and even.
Simple C Program to check for even and odd number |
#include <stdio.h>
int main() { int number; printf(“Enter a number: “); scanf(“%d”, &number); if (number % 2 == 0) { //Check if the number is divisible by 2 printf(“%d is even.\n”, number); } else { printf(“%d is odd.\n”, number); } return 0; } |
Output
Q20. Write a simple C program to find the area of a triangle using the base and height.
Simple C Program to find the area of a triangle |
#include <stdio.h>
int main() { float base, height, area; //Enter the base and height of triangle printf(“Enter the base of the triangle: “); scanf(“%f”, &base); printf(“Enter the height of the triangle: “); scanf(“%f”, &height); //Use formula area = 0.5 * base * height; printf(“The area of the triangle with base %.2f and height %.2f is: %.2f\n”, base, height, area); return 0; } |
Output
Learn DSA with C++, Java, Python
Enroll in our decode DSA C++ Course with popular languages like Python, C++ and Java to master coding along with data structure and algorithm. Get seamless support during the course with doubt session support, career guidance, and more. Hurry learn from the best only at pwskills.com.
Simple C Programs FAQs
What are simple programs in C?
Some of the simple C programs are Hello World, addition, ASCII value, factorial, palindrome and more. Check out this article.
What is %d in C programming?
%d is a format specifier in C language to take integers as input and is used mainly with printf() and scanf() functions.
What is getch() in C programming?
getch() is used to get a single character from the user as an input.