Varun Saharawat is a seasoned professional in the fields of SEO and content writing. With a profound knowledge of the intricate aspects of these disciplines, Varun has established himself as a valuable asset in the world of digital marketing and online content creation.
For beginners, the C program is frequently their first option. Practice some of the basic C programs in this article if you want to learn and gain experience with C programming problems.
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.
Table of Contents
Check some of the simple C programs at the beginner level to start your programming journey.
// Taking integer input
printf (“Enter an integer: “);
scanf (“%d”, &integerInput); //Use scanf() to take user input
// Taking floating-point input
printf (“Enter a float: “);
// Taking character input (skipping leading whitespace)
printf (“Enter a character: “);
// Displaying the inputs received
printf (“\nInteger entered: %d\n”, integerInput);
printf (“Float entered: %.2f\n”, floatInput);
printf (“Character entered: %c\n”, charInput);
Output
printf(“Enter a character : “);
printf (“\n\nASCII value of given character %c = %d”,c,c);
Output
printf(“Enter an alphabet : “);
printf(“\n\nReverse case of %c is : “,alpha);
if(islower(alpha)) //Check if it is in lowercase
printf(“%c”,tolower(alpha)) ; //Convert into lowercase
Output
We will use Switch cases to check whether the given input is a vowel or not. Check the implementation in the table below.
printf (“Enter any Character : “);
printf(“\n\n%c is a vowel.\n\n”, ch);
printf(“%c is not a vowel.\n\n”, ch);
Output
We will use a temporary variable to swap the given two numbers. Check the implementation below.
int num1, num2, temp;
// Input the two numbers
printf(“Enter number 1: “);
printf(“Enter number 2: “);
// Display the original numbers
printf(“Number 1: %d\n”, num1);
printf(“Number 2: %d\n”, num2);
// Swap the numbers using a temporary variable
// Display the swapped numbers
printf(“Number 1: %d\n”, num1);
printf(“Number 2: %d\n”, num2);
Output
long int fact = 1;
printf(“Enter a number of your choice: “);
printf(“Factorial of number %d is %ld”, n , fact);
Output
void fibonacci(int num);
printf(“Enter the number of terms in the Fibonacci series: “);
fibonacci(num); // Call fibonacci function
void fibonacci(int num)
int a = 0, b = 1, c, i;
Output
float number, sum = 0.0, average;
printf(“Enter the number of elements: “);
// Prompt user to enter N numbers
printf(“Enter %d numbers:\n”, N);
sum += number; // Add each number to the sum
average = sum / N;
printf(“Average of the entered numbers = %.2f\n”, average);
printf(“Cannot calculate average. Number of elements should be greater than zero.\n”);
Output
printf(“Enter any number to find the factors of : “);
printf(“\n\n\nFactors of %d are \n\n”, num);
Output
int length, i = 0;
printf(“\n\nEnter a number: “);
// Keep checking until the end.
printf(“\nEntered Number is a Floating point Number\n”);
printf(“\nEntered Number is a integer Number\n”);
Output
// Function to swap two integers
void swap(int *a, int *b)
// Function to reverse an array
void reverseArray(int arr[], int size)
int end = size – 1;
// Swap elements at start and end indices
// Move start index forward
// Move end index backward
void printArray(int arr[], int size)
int arr[100]; // Assuming maximum size of array is 100
// Input the size of the array
printf(“Enter the size of the array: “);
// Input the elements of the array
printf(“Enter %d elements:\n”, size);
// Reverse the array
// Print the reversed array
Output
int array[], position, c, n, value;
printf(“\nEnter total number of elements in array:”);
printf(“\n\nEnter %d elements\n”, n);
printf(“\n\nEnter the location where you want to insert the new element: “);
printf(“\n\nEnter the value to insert: “);
// Shift the elements to right
for(c = n-1; c >= position-1; c–)
array[position – 1] = value; // insert the value
printf(“\n\nResultant array is: “);
Output
int remove_duplicate(int arr[], int n)
if (arr[i] != arr[i + 1])
printf(“Enter the size of array: “);
printf(“\n Original Array Before Removing Duplicates: “);
n = remove_duplicate(arr, n);
printf(“\nArray After Removing Duplicates: “);
Output
int n, sum = 0, i, array[100];
printf(“Enter the number of integers to add: “);
printf(“\n\nEnter %d integers \n\n”, n);
sum += array[i]; // same as sum = sum + array[c]
printf(“\n\nSum = %d\n\n”, sum);
Output
// Function to check if a character is a valid alphabet letter
int isAlphabet(char ch)
// Function to convert a character to lowercase
char toLower(char ch)
return ch + (‘a’ – ‘A’);
// 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
return 1; // It’s a palindrome
// Ignore non-alphabet characters and move to the next character
return isPalindrome(str, left + 1, 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]);
if (leftChar == rightChar)
return isPalindrome(str, left + 1, right – 1);
return 0; // Not a palindrome
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);
printf(“‘%s’ is a palindrome.\n”, str);
printf(“‘%s’ is not a palindrome.\n”, str);
Output
int findLargest(int arr[], int size)
int maxRest = findLargest(arr + 1, size – 1);
return (arr[0] > maxRest) ? arr[0] : maxRest;
printf(“Enter the size of the array: “);
printf(“Enter %d elements:\n”, size);
int largest = findLargest(arr, size); //Calling the function recursively
printf(“The largest element in the given array is: %d\n”, largest);
Output
int gcd(int a, int b)
//Function to calculate LCM of two given number.
int lcm(int a, int 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;
// Input two numbers from the user
printf(“Enter first number: “);
printf(“Enter second number: “);
// 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);
Output
// Recursive function to add two numbers
int add(int a, int b)
// Base case: if the second number is 0, return the first number
// Recursive case: add one to the first number and decrement the second number
return add(a + 1, b – 1);
printf(“Enter first number: “);
printf(“Enter second number: “);
int sum = add(num1, num2); //Call the function recursively
// Display the result
printf(“Sum of %d and %d is: %d\n”, num1, num2, sum);
Output
printf(“Enter a number: “);
printf(“%d is even.\n”, number);
printf(“%d is odd.\n”, number);
Output
float base, height, area;
//Enter the base and height of triangle
printf(“Enter the base of the triangle: “);
printf(“Enter the height of the triangle: “);
area = 0.5 * base * height;
printf(“The area of the triangle with base %.2f and height %.2f is: %.2f\n”, base, height, area);
Output
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.
Some of the simple C programs are Hello World, addition, ASCII value, factorial, palindrome and more. Check out this article.
%d is a format specifier in C language to take integers as input and is used mainly with printf() and scanf() functions.
getch() is used to get a single character from the user as an input.
Let us write a C program to check leap year. We are using If and Else statements to find whether…
C Plus Plus: Welcome to the exciting world of C Plus Plus programming! In this course, you'll embark on a journey…
1LL In C++ denotes a literal constant of type "long long", representing the integer value 1. It's commonly used to…
Telegram Group Join Now WhatsApp Channel Join Now YouTube Channel SubscribeProduct Director Jobs are responsible for managing and supervising the development team throughout the product development process.
Let us write a C program to check leap year. We are using If and Else statements to find whether a given year is a leap year or not.
HTML Programming is a basic markup language used to structure the layout of the web pages. With HTML browser get to know how to display different elements and components on the web page.
These Top 5 Python Basics Internships will help you to boost your career to the next level by providing you with the relevant experience and knowledge.
Grab free internships for freshers with Google, Infosys, Accenture, IITs, and top institutions for free. Work on real-world projects, upskill, and boost your productivity with hands-on experience with internship programs.
Top data science courses In IIMs include- 1. Senior Management Programme in Business Analytics - IIM Calcutta, 2. Business Analytics Programme - IIM Bangalore and much more.