C programming examples 

C programming examples 

15 mins read276 Views Comment
Updated on May 25, 2023 13:42 IST

If you want to learn C language by executing programming examples. Then this article offers you 17C programming examples with outputs, so that you can learn fast.

2023_05_MicrosoftTeams-image-11.jpg

The best way to learn any programming language is to practice it as much as possible. Practicing the programs and learning new topics in a language can bring so much perfection to your programming skills. This article will help you to know more about the C language. C Programming skills can be improved by writing several programs on a compiler. Writing a program on a compiler helps you get practical knowledge and improves your programming skills. In this article, we will tell you some basic C programming examples.

C programming examples

Problem 1: Create a C program that prints “Hello world!!!”.

Problem 2: Write a C program that allows the user to enter two integers and displays their addition on the console.

Problem 3: Write a C program that allows the user to find the remainder when the first integer is divided by the second integer.

Problem 4: Create a program that takes the integer value from the user and generates its table. 

Problem 5: Write a C program to check whether a character is an alphabet or not. 

Problem 6: Write a C program to swap the value of two variables.  

#include <stdio.h>
int main()
{
printf("Hello World!!!!"); //print statement
return 0;
}
Copy code

Output-

Explanation 

  • It is a simple c program that prints the string hello world on the console using the function printf()
  • “return 0” depicts that the execution of the program has been completed successfully.
Difference between Malloc and Calloc
Difference between Malloc and Calloc
Malloc and Calloc are dynamic memory allocation methods in C language. You will learn the differences between these two. And will also see the applications of them. In this article...read more
C programming Keywords: A list and Explanation of Usage
C programming Keywords: A list and Explanation of Usage
Keywords in C refer to a set of reserved words with predefined meanings that are used to write programs in the C programming language. These keywords cannot be used as...read more
Unary Operators in C: Everything You Need to Know
Unary Operators in C: Everything You Need to Know
This article will discuss about different unaru operators like Increment operator, Decrement operator,Negation operator, Logical NOT operator,Bitwise NOT operator, Indirection operator, Operator Precedence.
Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

4.24 K
6 weeks
475
6 weeks
– / –
45 hours
4 K
80 hours
– / –
1 month
– / –
40 hours
– / –
2 months
– / –
1 year
4 K
80 hours

Problem 2: Write a C program that allows the user to enter two integers and displays their addition on the console.

Input:

The program must allow the user to enter two integers.

Expected Output:

  The sum of the two integers entered by the user. 

Code:

 
#include <stdio.h>
int main() {
int num1, num2, add; //declaring variables
printf("Enter two integers ");
scanf("%d %d", &num1, &num2); //numbers entered by user
add = num1 + num2; //addition of two numbers
printf("%d + %d = %d", num1, num2, add); //result
return 0;
}
Copy code

Output 

Enter two integers 9
5
9 + 5 = 14

Explanation 

  • In the above program num1, num2 and add are three variables.
  • To get the value of two integers from the user, in the fifth line of code scanf function is used.
  • Add variable holds the addition of num1 + num2
  • At last printf function is used to display the addition of num1 and num2.

Problem 3: Write a C program that allows the user to find the remainder when the first integer is divided by the second integer.

Input:

The program must allow the user to enter two integers.

Expected Output:

Display the remainder when the first number is divided by the second number.

Code:

 
#include <stdio.h>
int main() {
int a, b, rem_;
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
rem_ = a % b;
printf("Remainder is: %d\n", rem_);
return 0;
}
Copy code

Output

Enter two numbers: 8
5
Remainder is: 3

Explanation 

  • In the above example, a, b and rem_ are three variables. 
  • The 6th line of code printf function prints the message” enter two numbers” on the screen. 
  • For taking input from the user scanf function is used. 
  •   In arithmetic expression, rem_ = a % b, rem_ holds the remainder value when a is divided by the b. Here % is a modulo operator that gives the remainder when dividing one number by another.
  • In the last printf(“Remainder is: %d”, rem_); prints the remainder.

Problem 4: Create a program that takes the integer value from the user and generates its table. 

Input:

Take the number from the user to generate the table.

Expected Output:

The table of the number entered by the user

Code:

 
#include <stdio.h>
int main() {
int n, j, r;
printf("Enter the number: ");
scanf("%d", &n);
printf("\nTable of %d is:\n", n);
for (j = 1; j <= 10; j++) {
r = n * j;
printf("%d * %d = %d\n", n, j, r);
}
return 0;
}
Copy code

Output

Enter the number: 5
Table of 5 is:
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

Explanation

  • In the above program, n, j, and r are three integer variables, where n is the number entered by the user to generate its table r and j is another variable which is used in for loop.
  • In each iteration, the value of j is compared to 10. If the comparison evaluates as true, then the code written under for block gets executed and prints the value of r. Simultaneously the value of j is increased by 1 in each iteration. 
  • When the value of j becomes greater than 10, the execution of the for loop is terminated.

Problem 5: Write a C program to check whether a character is an alphabet. 

Input: A character entered by the user. 

Expected Output:

 “It’s an alphabet”. 

Or “not an alphabet”. 

Code:

 
#include <stdio.h>
int main()
{
char ch; //variable declaration
printf("Enter character: ");
scanf("%c", &ch);
//condition to be checked for being an alphabet
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf(" It's an alphabet"); //print statement
}
else
{
printf(" not an alphabet"); //print statement
}
return 0;
}
Copy code

Output

Enter character: 9
not an alphabet

Explanation- 

  • In the above program, a character variable ch is declared. 
  • The user enters the value of ch.
  • The condition to be checked for being an alphabet is written in an if statement, that is if((ch >= ‘a’ && ch <= ‘z’) || (ch >= ‘A’ && ch <= ‘Z’))
  • Using the ASCII value of the character, it is checked that the entered alphabet is in the lowercase or uppercase alphabet range.
  • If both conditions evaluate as true, then the program executes the code printf(“It’s an alphabet”) otherwise, it printf(“not an alphabet”)

Problem 6: Write a C program to swap the value of two variables.  

Input: The program takes two integer values as input.

 Expected Output: The program should output the values of the two variables after swapping. 

Code:

 
#include<stdio.h>
int main() {
int a, b, temp; //variable declaration
printf("Enter first number ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
temp = a;
//expressions for swapping the numbers
a = b;
b = temp;
printf("\nAfter swapping, first number = %d\n", a);
printf("After swapping, second number = %d", b);
return 0;
}
Copy code

Output

Enter first number 6
Enter second number: 7
After swapping, first number = 7
After swapping, second number = 6

Explanation-

  • In the above program, three integer variables are named a, b and temp.
  • The user enters a value of a and b. Three expressions using the assignment operator are used to swap the value of a and b.
  • In the first expression value of a, which is 4 is assigned to the temp variable. In the second step value of b, which is 16 is assigned to a and in the last step value of temp is assigned to b.

Problem 7: Write a C program to swap the value of two variables without using a third variable. 

Input: Two integers to be swapped. 

Expected Output: print the swapped values of the variables.

 Code:

 
#include<stdio.h>
int main() {
int a, b; //variable declaration
printf("Enter first number ");
scanf("%d", &a); //value of a entered by the user
printf("Enter second number: ");
scanf("%d", &b); //value of b entered by the user
a=a-b; //expressions for swapping the numbers
b = a + b;
a = b-a;
printf("\nAfter swapping, first number = %d\n", a);
printf("After swapping, second number = %d", b);
return 0;
}
Copy code

Output

Enter first number 6
Enter second number: 7
After swapping, first number = 7
After swapping, second number = 6

Explanation-

  • In the above program, a and b are two integer variables.
  • After taking the value of a and b from the user, three arithmetic expressions are used to swap their values. 
  • For example, When a user enters a=20 and b =10, then according to the first equation a-b= (20-10)= 10 gets stored in a and now the current value of a is 10. And in the next step, a+b means (10+10) = 20, which gets stored in b, so the current value of b is 20. In the last step b-a = 20-10=10, which gets stored in a. 
  • This is how the values of a and b are swapped. 

Problem 8: Write a C program to calculate the sum of natural numbers.

Input: A positive integer. 

Expected Output: sum of the first n natural numbers. 

Code:

 
#include <stdio.h>
int main() {
int n, j, sum = 0;
printf("Enter a positive number: ");
scanf("%d", &n);
for (j = 0; j <= n; j++) {
sum = sum + j;
}
printf("Sum of the first %d numbers is: %d\n", n, sum);
return 0;
}
Copy code

Output

Enter a positive number: 7
Sum of the first 7 numbers is: 28

Explanation

  • In the above program, there are three integer variables n, j and sum_, in which n holds the value entered by the user, and sum holds the value of the calculated sum of natural numbers. 
  • For loop is used to check whether a number is positive or negative.
  • In each iteration, the value of j is checked with the entered number n, and it also adds the value of j with the sum_.
  • When the value of j becomes greater than n, in such a situation, loops get terminated.

Problem 9: Write a C program to check whether a number is odd or even. 

Input: A number entered by the user using the keyboard. 

Expected Output: The program should print whether the input number is odd or even.

 Code: 

 
#include <stdio.h>
int main() {
int n; //variable declaration
printf("Enter a number ");
scanf("%d", &n);
//putting expression in the if-else to check the odd or even number
if(n % 2 == 0)
printf("%d is even", n);
else
printf("%d is odd", n);
return 0;
}
Copy code

Output

Enter a number 6
6 is even

Explanation 

  • In the above program, one integer variable, n is declared.
  • Using the printf and scanf functions, the value of n is taken by the user.
  • An expression (n%2==0) is used in the if else statement to check whether a number is odd or even.
  • In the expression (n%2==0), % is the modulo operator that returns the value of the remainder when n is divided by 2. If the remainder is equal to zero then the entered number is even; otherwise, it is odd.

Problem 10: Write a C program to check whether a number is positive or negative

Input: a number entered by the user 

Expected Output: Display the number as positive, negative, or zero 

 Code:

 
#include <stdio.h>
int main() {
int n; //variable declaration
printf("Enter a number: ");
scanf("%d", &n);
//using if else
if (n <= 0) {
if (n == 0)
printf("It's 0.");
else
printf("It's a negative number.");
}
else
printf("It's a positive number.");
return 0;
}
Copy code

Output

Enter a number: 4
It’s a positive number.6
dash: 2: 6: not found

Explanation 

  • In the above program n is an integer variable. The value of n is entered by the user using the scanf function.
  • To check whether a number is negative, positive or zero, a nested if else statement is used.
  • First, if the entered number is less than or equal to 0 if the value of n is found equal to zero, then the printf statement It’s 0 prints on the console. 
  • If the entered number is less than zero then “It’s a negative number.” is displayed on the screen. 
  • If the entered number is greater than zero then “It’s a positive number.” is displayed on the screen. 

Problem 11: Write a C program to calculate the power of a number raised to a number. 

Input: The base and exponent numbers are entered as input from the user. 

Expected Output: display the power of the number raised to an exponent.

Code:

 
#include <stdio.h>
int main(){
int b, e;
int result=1; //variable declaration
printf(" base number: ");
scanf("%d", &b); //value of base number entered by the user
printf(" exponent number: ");
scanf("%d", &e); //value of exponent number entered by the user
//loop for calculating the power of a number
while (e != 0) {
result *= b;
--e;
}
printf("Answer = %d", result); //result to be displayed on the screen
}
Copy code

Output

base number: 7
exponent number: 5
Answer = 16807

Explanation 

  • In the above program, there are three variables b, e and result, where b holds the value of the base number, e holds the value of the exponent number and the result variable holds the value of the calculated power of a number.
  • After taking the value of the base and exponent number from the user.
  • While loop is used to calculate the power of a base number raised to an exponent number where in each iteration, the loop multiplies the result to the base number and decreases the value of e by 1.
  • The loop continues until the value of e reaches 0.

Problem 12: Write a C program to find the area of a circle. 

Input: take the radius of the circle from the user. Use pie = 3.14

Expected Output: The area of the circle. 

Code: 

 
#include <stdio.h>
int main() {
float pie = 3.14;
int rad_;
printf("Enter the radius of the circle: ");
scanf("%d", &rad_);
printf("The radius of the circle is %d\n", rad_);
float area_ = pie * rad_ * rad_;
printf("The area of the circle is %f\n", area_);
return 0;
}
Copy code

Output-

Enter the radius of the circle: 8
The radius of the circle is 8
The area of the circle is 200.960007

Explanation 

  • The main function declares three variables, pie, rad_ and area_ of float and integer data types.
  • Using the scanf function, the user enters the radius’s value. 
  • Using the formula area = pi * r * r, the area of the circle is calculated, where pi is a constant value of 3.14, r is the circle’s radius, and * is the multiplication operator. 

Problem 13:Write a C program to find the perimeter of a rectangle. 

Input: the length u and x are initialized to 3, and the width v and y are initialized to 8. 

Expected Output: The perimeter of the Rectangle is 22.000000 

Code:

 
#include<stdio.h>
int main()
{ float u , v, x, y ,perimeter_; //variable declaration
u=x= 3;
v=y= 8;
perimeter_ = 2*(u+v); //formula to find perimeter of rectangle
printf("\n\n Perimeter of Rectangle is %f",perimeter_);
return (0);
}
Copy code

Output-

Perimeter of Rectangle is 22.000000

Explanation 

  • In this program, the variables u, v, x, and y represent the length and width of the Rectangle.
  •   u and x are initialized to 3, and the width v and y are initialized to 8. 
  •   perimeter_ = 2*(u+v) is the formula to calculate the perimeter of a rectangle and store it in the variable perimeter_. 

Problem 14: Write a program in C to check whether a number is prime. 

Input: Any integer number

Expected Output: Display whether the entered number is prime or not. 

Code:

 
#include<stdio.h>
int main(){
int a,j,b=0,flag=0; //variable declaration
printf("Enter number to check");
scanf("%d",&b);
a=b/2;
for(j=2;j<=a;j++) //for loop to check primality of each number
{
if(b%j==0)
{
printf("It's not a prime number");
flag=1;
break;
}
}
if(flag==0)
printf("It's a prime number");
return 0;
}
Copy code

Output:

Enter number to check 9

It’s not a prime number

Explanation 

  • The above program has four integer variables a,j,b and flag.
  • It uses a for loop to check whether the input number is divisible by any number other than 1 and itself. 
  • If the number is divisible by any number other than 1 and itself, it’s not a prime number, and the flag is set to 1. 
  • If the number is 1, it’s neither prime nor composite. 

Problem 15: Write a C program to find the Simple Interest.

Input: Principal (p) = 14000 ,Rate (r) = 10% ,Time (t) = 3 years 

Expected Output: Simple Interest is: 4200.000000 

Code:

 
#include<stdio.h>
int main()
{
float p, r, t, si; //variable declaration
p =14000; r =10; t = 3;
si = (p*r*t)/100; //formula to calculate SI
printf("\n\n Simple Interest is : %f", si);
return (0);
}
Copy code

Output-

Simple Interest is : 4200.000000

Explanation

  • The above program declares four variables p, r, t, and si as float. 
  • After assigning values to the variables that are p =14000; r =10; t = 3, a function si = (p*r*t)/100, which is used to calculate simple interest, is applied here.
  • The value of simple interest is stored in the variable si.

Problem 16: Write a C program to find the first n prime numbers. 

Input: An integer value 

Expected Output: n prime numbers separated by tabs. 

Code:

 
#include <stdio.h>
int main() {
int num, count = 1, flag, a = 2, b;
printf("How many prime numbers do you want?\n");
scanf("%d", &num);
while (count <= num) {
flag = 0;
for (b = 2; b <= a / 2; b++) {
if (a % b == 0) {
flag = 1;
break;
}
}
if (flag == 0) {
printf("%d ", a);
count++;
}
a++;
}
return 0;
}
Copy code

Output-

How many prime numbers do you want?
9
2 3 5 7 11 13 17 19 23

Explanation

  •  In the above program num, count, flag, a and b are variables used to store the data in the program.
  • After taking the value of the number from the user, while loop is used to check the primality of each number.
  • In each iteration of the while loop, the loop checks if the number is prime by using another for loop and looks over if the number is divisible by any number from 2 to half of the number itself.
  •  If the number is divisible, the flag variable is set to 1, which indicates that the number is not prime. And If the flag variable stays 0, the program displays the number as a prime number and increments the count by 1.

Problem 17: Write a C program to find the largest number among four numbers. 

Input: Four integers entered by the user. 

Expected Output: The largest number among the four entered integers. 

Code:

 
#include<stdio.h>
main()
{
int u,v,x,y; //variable declaration
printf("Enter the Four Numbers :");
scanf("%d %d %d %d",&u,&v,&x,&y); //values entered by user
if(u>v)
{
if(u>x)
{
if(u>y)
{
printf("%d is big",u);
}
else
{
printf("%d is big",y);
}
}
}
else if(v>x)
{
if(v>y)
{
printf("%d is big",v);
}
else
{
printf("%d is big",y);
}
}
else if(x>y)
{
printf("%d is big",x);
}
else
{
printf("%d is big",y);
}
}
Copy code

Output

Enter the Four Numbers :9
7
6
5
9 is big

Explanation 

  • The program takes four integers u,v,x, and y, as input from the user using the scanf function. 
  • To identify the largest number among four entered integers, a nested if else statement is used.
  • The first if statement compares the first and second numbers u and v. If u is greater than v, it checks whether u is greater than x and y. If u is greater than all the other numbers, it prints u is big.
  •  If u is not bigger than all the other numbers, it moves ahead to the next if-else statement. The next if-else statement compares the second and third numbers v and x. If v is greater than x, it checks whether v is greater than y. 
  • If v is greater than y, it prints that v is big. If v is not greater than y, it moves to the next if-else statement. The third if-else statement compares the third and fourth numbers x and y. If x is greater than y, it prints that x is big
  •  If x is not greater than y, it prints y is big.

Wrapping Up

All these basic programs in c language will help you to write more programs in c with correct logic and syntax. The best way to improve programming skills and logic is to practice more and more programs in c. 

Contributed by Srashti Soni

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio