In this article, I have provided a list of samples of a simple C program with its code and output, which can be useful for C language beginners.
So, before we begin, I assume you have basic knowledge of C language code like declaring variables, using functions like printf() and scanf(), if not, you need to carefully understand and code for first three examples, and then proceed for other simple C programming examples with output.
Table of Contents
- Hello World program in C
- Get user input using scanf and print it in C
- Addition program in C ( Adding two integers)
- C program to find the maximum number between three numbers
- Find number is even or odd program in C
- C Program to get sum of first n Natural Numbers
- C program to check if a number is a prime number or not
- C program to swap two numbers using a Temp (third) variable
- Storing values in array and printing it in C
- C program to find factorial of a number
- C program to find GCD of two number
- C program to find armstrong number
- C program to find LCM of two numbers
- C program to print pyramid pattern
- C program to create simple calculator using Switch case
- C program to calculate simple interest using function
- Matrix multiplication program in C (Without using function/pointers)
Hello World program in C
Let's get started with the easier and simplest program in C to print "Hello world"
#include <stdio.h>
void main()
{
printf("Hello World\n");
}
Output;
Hello World
Get user input using scanf and print it in C
#include <stdio.h>
void main()
{
int num;
printf("Enter a number\n");
scanf("%d", &num); // %d is used for an integer
printf("The number is: %d\n", num);
}
Output:
Enter a number
5
The number is: 5
Addition program in C ( Adding two integers)
I have just recently added the article, related to this in which I have explained about adding two integers in C, but I will again write the code here for you
#include <stdio.h>
void main()
{
//declare three variables, for firstNumber+secondNumber=TotalSumOfBoth
int firstNumber, secondNumber, TotalSumOfBoth;
//ask the user to get first number to add
printf("Enter first number: ");
//store it in first variable using scanf() function
scanf("%d", &firstNumber);
//ask the user to get second number to add
printf("Enter second number:");
//store it in second variable using scanf() function
scanf("%d", &secondNumber);
// sum of two numbers in stored in variable TotalSumOfBoth
TotalSumOfBoth = firstNumber + secondNumber;
// Displays sum
printf("%d + %d = %d", firstNumber, secondNumber, TotalSumOfBoth);
}
Output:
Enter first number: 10
Enter second number:20
10 + 20 = 30
C program to find the maximum number between three numbers
To find the maximum between 3 numbers, we need to follow these steps:
- Take three integer variables, say A, B & C
- Ask the user to enter three numbers and save them in above-declared variables
- If A is greater than B & C, Display A is the largest value
- If B is greater than A & C, Display B is the largest value
- If C is greater than A & B, Display A is the largest value
- Otherwise, Display A, B & C are not unique values
Now, we have the steps, let's implement it in C Code
#include <stdio.h>
int main() {
int a, b, c;
/* Input three numbers from user */
printf("Enter three numbers: ");
scanf("%d%d%d", &a, &b, &c);
if ( a > b && a > c )
printf("%d is the largest.", a);
else if ( b > a && b > c )
printf("%d is the largest.", b);
else if ( c > a && c > b )
printf("%d is the largest.", c);
else
printf("Values are not unique");
return 0;
}
Output:
Enter three numbers: 10
30
18
30 is the largest.
Find number is even or odd program in C
Here the steps, which we need to follow in order to create a program to check if a number is even or odd.
- Declare a variable "A"
- Ask the user to enter the number to check if it even or odd and save it's values in above-declared variables.
- Now, the main logic begins, Perform A modulo 2 and check result if the output is 0
- If true print A is even
- If false print A is odd
#include <stdio.h>
int main() {
int num;
printf("Enter number to check if it's even or odd: ");
scanf("%d",&num);
if (num % 2 == 0)
printf("%d is even\n", num);
else
printf("%d is odd\n", num);
return 0;
}
Output:
Enter number to check if it's even or odd: 24
24 is even
C Program to get sum of first n Natural Numbers
In this example, we will ask user to enter any positive digit number (N) upto which we want to sum all digits one by one using for loop
#include <stdio.h>
void main() {
int n, count, sum = 0;
printf("Enter number upto which you need sum: ");
scanf("%d", & n);
// loop until the number and keep adding values
for (count = 1; count <= n; count++) {
sum = sum + count;
}
printf("Sum of first %d natural numbers is: %d", n, sum);
}
Output:
Enter number upto which you need sum: 45
Sum of first 45 natural numbers is: 1035
C program to check if a number is a prime number or not
We have a great post that explains in detail how to check if a number is a prime number or not in C, and here is the sample code for it.
#include <stdio.h>
void main() {
int no, i, f;
printf("Enter any number: ");
scanf("%d", & no);
f = 0;
i = 2;
while (i <= no / 2) {
if (no % i == 0) {
f = 1;
break;
}
i++;
}
if (f == 0)
printf(" %d is Prime Number", no);
else
printf("%d Not Prime Number", no);
}
Output:
Enter any number: 5
5 is Prime Number
C program to swap two numbers using a Temp (third) variable
Basically, we will be swapping values of two numbers using the third variable, you can read the detailed post for it and here is how we will achieve this.
For example, we have two numbers(10, 20) saved in two variables (firstNumber=10 & secondNumber=20), now we need to interchange these two numbers, here interchanging means at the end of the program, firstNumber =20 and secondNumber =10, so to do that we would need thrid variable Temp, to store a value temporarily.
C program for it
#include<stdio.h>
void main()
{
int firstNumber, secondNumber, Temp;
//get two number from user and save it in variable firstNumber, secondNumber
printf("\nPlease Enter the value of First Number and Second Number\n");
scanf("%d %d", &firstNumber, &secondNumber);
//print numbers before swapping them (example: firstNumber=10, secondNumber =20)
printf("\nBefore Swapping First Number = %d and Second Number = %d\n", firstNumber,secondNumber);
//save firstNuumber in temp variable (Ex: Temp= 10)
Temp = firstNumber;
//get secondNumber in firstNumber now (Ex: firstNumber = 20)
firstNumber = secondNumber;
//get the temp value in second now (Ex: secondNumber = 10 as Teamp =10)
secondNumber = Temp;
//print swapped numbers
printf("\nAfter Swapping: First Number = %d and Second Number = %d\n", firstNumber, secondNumber );
}
Output:
Please Enter the value of First Number and Second Number
10 20
Before Swapping First Number = 10 and Second Number = 20
After Swapping: First Number = 20 and Second Number = 10
Storing values in array and printing it in C
Well in this example,we will basically create array and ask user to enter values in it , after getting array values, we will print it using loop.
#include <stdio.h>
void main()
{
int arr[100], n, c;
//ask the user for getting array size
printf("Enter number of elements in array\n");
scanf("%d", &n);
//ask the user for values
printf("Enter %d elements\n", n);
//saves values in array one by one until the size is reached
for (c = 0; c < n; c++)
scanf("%d", &arr[c]);
printf("The array elements are:\n");
//print the array
for (c = 0; c < n; c++)
printf("%d\n", arr[c]);
}
I have commented out a few lines of code, to make it understandable for you.
Enter number of elements in array
5
Enter 5 elements
10 7 20 6 85
The array elements are:
10
7
20
6
85
C program to find factorial of a number
The Factorial of a specified number refers to the product of all given series of consecutive whole numbers beginning with 1 and ending with the specified number
We use the “!” to represent factorial
Example:
5! = 1 x 2 x 3 x 4 x 5 = 120
let's C the code for the factorial
#include<stdio.h>
#include<conio.h>
void main()
{
int num,f;
clrscr();
printf("\n Enter the number: ");
scanf("%d",&num);
f=factorial(num);
printf("\n The factorial of the number %d is %d",num,f);
getch();
}
int factorial(int number)
{
if(number==0 || number==1)
return 1;
else
return(number * fact(number-1));
}
Output:
Enter the number: 5
The factorial of the number 5 is 120
If you want to read the detailed article on this with various ways of factorial program you can read here: Factorial progam in C (With & without recursion)
C program to find GCD of two number
The HCF or GCD of two integers is the largest integer that can exactly divide both numbers (without a remainder). Let's create a C program for it
#include <stdio.h>
void main()
{
int num1, num2, i, gcd;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
for(i=1; i <= num1&& i <= num2; ++i)
{
// Checks if i is factor of both integers
if(num1%i==0 && num2%i==0)
gcd = i;
}
printf("G.C.D of %d and %d is %d", num1, num2, gcd);
}
Output:
Enter two integers: 5 10
G.C.D of 5 and 10 is 5
C program to find armstrong number
A number is armstrong number when, sum of a number's digits raised to the power total number of digits is armstrong number.
Armstrong numbers example: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634 etc
#include <stdio.h>
int main()
{
int n, sum = 0, t, remainder;
printf("\nPlease enter a number to find whether it is an armstrong or not : ");
scanf("%d",&n);
t = n;
while( t != 0 )
{
remainder = t%10;
sum = sum + remainder*remainder*remainder;
t = t/10;
}
if ( n == sum )
printf("\nThe number %d is an armstrong number", n);
else
printf("\nThe number %d is not an armstrong number", n);
return 0;
}
Output:
Please enter a number to find whether it is an armstrong or not : 121
The number 121 is not an armstrong number
C program to find LCM of two numbers
The LCM of two integers n1 and n2 is the smallest positive integer that is perfectly divisible by both n1 and n2 (without a remainder). For example: the LCM of 72 and 120 is 360.
#include <stdio.h>
void main()
{
int num1, num2, minMultiple;
printf("Enter two positive integers: ");
scanf("%d %d", &num1, &num2);
// maximum number between num1 and num2 is stored in minMultiple
minMultiple = (num1>num2) ? num1 : num2;
// Always true
while(1)
{
if( minMultiple%num1==0 && minMultiple%num2==0 )
{
printf("The LCM of %d and %d is %d.", num1, num2,minMultiple);
break;
}
++minMultiple;
}
}
Output:
Enter two positive integers: 72
60
The LCM of 72 and 60 is 360.
C program to print pyramid pattern
Although we have detailed article on pyramid pattern in C, I will give a simple example here
#include <stdio.h>
int main()
{
//declare variables to use
int i, j, rows;
//Enter the number of rows you want to print, printf shows this command to user
printf("Enter number of rows: ");
//scanf is used to get value, and save that value in 'rows' variable
scanf("%d",&rows);
//now loop for each row
for(i=1; i<=rows; ++i)
{
//this loop will print * until value of outer loop variable(i) is greater than
//inner loop vairable(j)
for(j=1; j<=i; ++j)
{
printf("* ");
}
//as soon as inner loop(j) value becomes greater than outer loop printing * ends
// & it prints new line using \n
printf("\n");
}
return 0;
}
Output:
Enter number of rows: 6
*
* *
* * *
* * * *
* * * * *
* * * * * *
C program to create simple calculator using Switch case
In this example, we will create basic functions of calculator like addtion, subtraction, multiplication and divide using +, -, %, *
# include <stdio.h>
int main() {
char operator;
double firstNumber,secondNumber;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf",&firstNumber, &secondNumber);
switch(operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber + secondNumber);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber - secondNumber);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber * secondNumber);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber / secondNumber);
break;
// operator doesn't match any case constant (+, -, *, /)
default:
printf("Error! operator is not correct");
}
return 0;
}
Output:
Enter an operator (+, -, *,): +
Enter two operands: 20 15
20.0 + 15.0 = 35.0
C program to calculate simple interest using function
In this example, we will create a function which calculates Simple interest and will call this function from main method
#include<stdio.h>
float interest(int P,float R, int N)
{
float SI;
SI=P*R*N/100.0;
return SI;
}
int main()
{
int p,n,i;
float r,Z;
printf("Enter Principal Amount : ");
scanf("%d",&p);
printf("Enter Interest-Rate : ");
scanf("%f",&r);
printf("Enter Time Period : ");
scanf("%d",&n);
Z=interest(p,r,n);
printf("\nSimple-Interest : %.2f\n",Z);
return 0;
}
Output:
Enter Principal Amount : 500
Enter Interest-Rate : 5
Enter Time Period : 6
Simple-Interest : 150.00
Matrix multiplication program in C (Without using function/pointers)
We already have a detailed post Matrix multiplication program in C but we are using one of the programs in this article here to make it easier for our users to read it.
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],result[10][10],m,n,p,q,i,j,k;
//get the nuumber of rows/columns of first matrix
printf("Enter rows and columns of first matrix:");
scanf("%d%d",&m,&n);
//get the nuumber of rows/columns of second matrix
printf("Enter rows and columns of second matrix:");
scanf("%d%d",&p,&q);
//check if rows of A qual to columns of B
if(n==p)
{
printf("\nEnter first matrix:\n");
//fetch the first matrix data from user and save in array a[10][10] declared above
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
scanf("%d",&a[i][j]);
}
}
printf("\nEnter second matrix:\n");
//fetch the second matrix data from user and save in array b[10][10]
for(i=0;i<p;++i)
{
for(j=0;j<q;++j)
{
scanf("%d",&b[i][j]);
}
}
printf("\nThe multiplied matrix result is:\n");
//multiply the matrix and save it in result[][] array & print it
for(i=0;i<m;++i)
{
for(j=0;j<q;++j)
{
result[i][j]=0;
for(k=0;k<n;++k)
result[i][j]=result[i][j]+(a[i][k]*b[k][j]);
printf("%d ",result[i][j]);
}
printf("\n");
}
}
else
{
printf("\n Matrix multiplication can't be done for these rows/columns");
}
return 0;
}
Output:
Enter rows and columns of first matrix:3
3
Enter rows and columns of second matrix:3
3
Enter first matrix:
11
22
33
44
55
66
77
88
99
Enter second matrix:
11
22
33
44
55
66
77
88
99
The multiplied matrix result is:
3630 4356 5082
7986 9801 11616
12342 15246 18150
You may also like to read: