Category Archives: C Programs

C Programs

Program to Count Number Of Digits In Number

In this tutorial, we will learn to create a C Program to Count Number Of Digits In Number. For example: if the input number is 4321, then the output of the program will be 4. We will show you different method to count number of digits in number:

  • Using while loop
  • Using logarithmic function
  • By creating function to count digits
  • Using recursion

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • C Data Types
  • C Operator
  • C If else
  • C while loop
  • C Function
  • C Recursion

Program to Count Number Of Digits In Number using while loop

In this program we will count number of digits In number using while loop.. We have first declared and initialized the required variables. Next, we would prompt user to input a number to count number of digits in it. Later in the program we will count number of digits using while loop. We will then display the number of digits in given number.

Output:-

C-Program-to-Count-Number-Of-Digits-In-Number-using-while-loop

Next, we have declared and initialized a set variables required in the program.

  • num = it hold the first integer number value
  • count = will hold count of digit in number

In the next statement user will be prompted to enter an integer number which will be assigned to variable ‘num’. Next, we will compute the number of digits in a given number using c while loop. In the program, we use the division(/) operator to obtain individual digits of the number and increase count by 1(count = count + 1). Finally, we will be displaying the number of digits In ‘num’ using printf statement.

Program to Count Number Of Digits In Number using logarithmic function

In this program we will count number of digits In number using logarithmic function log10(). We have first declared and initialized the required variables. Next, we would prompt user to input a number to count number of digits in it. Later in the program we will count number of digits using log10() function. The log10() is the predefined function in math.h. We will then display the number of digits in given number.

Output:-

C-Program-to-Count-Number-Of-Digits-In-Number-using-logarithmic-log10-function

In the above program, we have used log10() function to count number of digits in a given number. The log10() is the predefined function in math.h. The formula to calculate number of digits using log10() function is as following:

Next, we have declared and initialized a set variables required in the program.

  • num = it hold the first integer number value
  • count = will hold count of digit in number

In the next statement user will be prompted to enter an number which will be assigned to variable ‘num’. Next, we will use log10() function to count number of digits in a given number. Finally, we will be displaying the number of digits In ‘num’ using printf statement.

Program to Count Number Of Digits In Number using function

In this program we will count number of digits In number using c function. We have first declared and initialized the required variables. Next, we would prompt user to input a number to count number of digits in it. Later in the program we will count number of digits using a user defined function. We will then display the number of digits in given number.

Output:-

C-Program-to-Count-Number-Of-Digits-In-Number-using-function

In the above program, we have first defined following function:

digits() :- This function means to accept an integer number and return the number of digits In that number.

Next, we have declared and initialized a set variables required in the program.

  • num = it hold the first integer number value
  • count = will hold count of digit in number

In the next statement user will be prompted to enter an number which will be assigned to variable ‘num’. Next, we will be calling digits() function to count number of digits In number. Finally, we will be displaying the number of digits In ‘num’ using printf statement.

Program to Count Number Of Digits In Number using recursion

In this program we will count number of digits In number using a recursive function. A function is called recursive when calls itself. We have first defined a recursive function in the program to return number of digits In given number. Next, we have declared and initialized the required variables. We would then prompt user to input a number. Later in the program we have invoked the digits() function to find number of digits in given number. We will then display the number of digits in given number.

Output:-

C-Program-to-Count-Number-Of-Digits-In-Number-using-recursion

In the above program, we have first defined a function called as digits(). This digits() function will find number of digits in given number. Next, we have declared required variables in the main function.

  • num = it holds the input number value
  • count = will hold count of digit in number

Next, user prompted to enter a number which assigned to variable ‘num’. Next, we will be calling digits() function to count number of digits In number. Finally, we will display the number of digits In given number.

C Program to Reverse Number Using While Loop and Recursion

In this tutorial, we will learn to create a c program to reverse number using while loop and using recursion. For example, if the input number is 123, then the output will be 321. We will show you two different method to reverse a given number:

  • Using while loop
  • Using recursion

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • C Data Types
  • C Operator
  • C If else
  • C while loop
  • C Function
  • C Recursion

C Program to reverse number using while loop

In this program we will reverse a number using while loop. We have first declared and initialized the required variables. Next, we would prompt user to input a number to be reversed. Later in the program we will reverse the number using while loop. We will then display the reversed number of the given number to user.

Output:-

C-Program-to-Reverse-a-Given-Number-using-while-loop

In the above program, we have first declared and initialized a set variables required in the program.

  • num = it holds the first integer number value
  • rnum = will hold reversed number, initialized with 0
  • rem = it holds the remainder during operations

In the next statement user will be prompted to enter an integer number which will be assigned to variable ‘num’. Next, we will compute the reverse of the given number using c while loop. In the program, we use the modulus operator (%) to obtain individual digits of the number. Finally, we will be displaying the reversed number of the given number using printf statement.

C Program to reverse number using recursion

In this program we will reverse a number using a recursive function. A function is called recursive when calls itself. We have first defined a recursive function in the program to return reverse of the given number. Next, we have declared and initialized the required variables. We would then prompt user to input a number to be reversed. Later in the program we have invoked the reverse() function to return reverse value of the number. We will then display the reversed number of the given number.

Output:-

C-Program-to-Reverse-a-Given-Number-using-while-loop

In the above program, we have first defined a function called as reverse(). Thie reverse() function will compute the reverse of the given number. Next, we have declared required variables in the main function.

  • num = it holds the first integer number value
  • rnum = will hold reversed number

Next, user prompted to enter a number which assigned to variable ‘num’. Next, we will be calling reverse() function to compute and return reverse value of the ‘num’ variable. Finally, we will display the reversed number of the given number.

C Program To Print Multiplication Table Of Given Number

In this tutorial, we will learn to create a program to generate and print multiplication table of given number using for loop In C programming language.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • C Operators
  • C Variables
  • C Input Output
  • C for loop

Program to Generate Multiplication Table Of a Number

In this program we will generate multiplication table of a number entered by the user. We would first declared and initialized the required variables. Next, we would prompt user to input an integer number to print multiplication table. Later in the program we will generate and print multiplication table of that number.

Output:-

C-Program-To-Print-Multiplication-Table-Of-Given-Number

In the above program, we have first declared and initialized a set variables required in the program.

  • num = it holds the an integer number value
  • i = iterator used in for loop

In the next statement user will be prompted to enter an integer number value. This will be assigned to variable ‘num’. Next, we will generate the multiplication table of the number using c for loop. Now, we will print the multiplication table of the integer number entered by user.

C Program To find factorial of a number

In this tutorial, we will learn to create a program find factorial of a given number using C programming language. We will show you following different method to find factorial of a given number:

  • factorial of a number using for loop
  • factorial of a number using function
  • factorial of a number using recursion

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • C Data Types
  • C Operator
  • C If else
  • C for loop
  • C Function
  • C Recursion

What is factorial?

Factorial of a positive integer number n is the product of all it’s positive descending integers till 1. Factorial of a positive number n is represented as following.

Mathematically !(bang) sign is used to represent factorial of given number.

Example:-

Factorial 5 is represented as following:

Similarly, factorial of 3 is represented as following:

Program To find factorial of a number using for loop

In this program we will find the factorial of a number using for loop. We have first declared and initialized the required variables. Next, we would prompt user to input an integer number. Later in the program we will find the factorial of the number using for loop. We will then display the factorial of the given number to user.

Output:-

C-Program-To-find-factorial-of-a-number

In the above program, we have first declared and initialized a set variables required in the program.

  • num = it holds the first integer number value
  • i = iterator for loop
  • fact = it holds the factorial of the given number, initialized with 1

In the next statement user will be prompted to enter an integer number which will be assigned to variable ‘num’. Next, we will compute the factorial of the given number using c for loop. Now, we will be displaying the factorial of the given number using printf statement.

Program To find factorial of a number using function

In this program we will find the factorial of a number using c function. We have first declared and initialized the required variables. Next, we would prompt user to input an integer number. Later in the program we will find the factorial of the number using a user defined function. We will then display the factorial of the given number to user.

Output:-

C-Program-To-find-factorial-of-a-number-using-function

In the above program, we have first defined following function:

factorial() :- This function means to accept an integer number and return the factorial value of that number.

Next, we have declared and initialized a set variables required in the program.

  • num = it holds the first integer number value
  • i = iterator for loop
  • fact = it holds the factorial of the given number, initialized with 1

In the next statement user will be prompted to enter an integer number which will be assigned to variable ‘num’. Next, we will be calling factorial() function to compute and return factorial value of the ‘num’ variable. Finally, we will be displaying the return value of the factorial() function using printf statement.

Program To find factorial of a number using recursion

In this program we will find the factorial of a number using a recursive function. A function is called recursive when calls itself. We have first defined a recursive function in the program to return factorial of the given number. Next, we have declared and initialized the required variables. We would then prompt user to input an integer number. Later in the program we have invoked the factorial function to find factorial value of the number. We will then display the factorial of the given number to user.

Output:-

C-Program-To-find-factorial-of-a-number-using-recursion

In the above program, we have first defined a function called as factorial() that will recursively compute the factorial of the given number. Later in the main function, we declared required variables.

  • num = it holds the first integer number value

In the next statement user will be prompted to enter an integer number which will be assigned to variable ‘num’. Next, we will be calling factorial() function to compute and return factorial value of the ‘num’ variable. Finally, we will be displaying the return value of the factorial() function using printf statement.

C program to perform addition, subtraction, multiplication and division

In this tutorial, we will learn to create a program to perform addition, subtraction, multiplication and division of two numbers using C programming language.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • C Data Types
  • C Variables
  • C Input Output
  • C Operators
  • C Typecasting

Program to perform basic arithmetic operations

In this program we will accept two operands from the user. Then, we will perform the basic arithmetic operations(addition, subtraction, multiplication and division) on the two operands. Later in the program we will display the result.

Output:-

C-program-to-perform-addition-subtraction-multiplication-and-division

In the above program, we have first declared and initialized a set variables required in the program.

  • num1 = it holds the first number value
  • num2 = it holds the second number value
  • add = it holds the sum of two numbers
  • subtract = it holds subtraction result of two numbers
  • multiply = it holds the multiplication result of two numbers
  • divide = it holds the division result of two numbers

In the next statement user will be prompted to enter the two number values which will be assigned to variable ‘num1’ and ‘num2’ respectively. Then, we will perform the basic arithmetic operations(addition, subtraction, multiplication and division) on the two operands and assign the result to respective variable add, subtract, multiply and divide. Finally, we will be displaying the result using printf statement.

C Program to Perform Arithmetic Operations Using Switch

In this tutorial, we will learn to create a simple calculator program to perform arithmetic operations using C Switch Statement in C programming language.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • C Data Types
  • C Variables
  • C Input Output
  • C Operators
  • C Switch Case Statement

Program to Perform Arithmetic Operations Using Switch

In this program we will accept two operands and an arithmetic operator (+, -, *, /) from the user. Then, we will perform the calculation on the two operands based upon the operator entered by the user. Later in the program we will display the result based on operands and arithmetic operator provided.

Output:-

C-Program-to-Perform-Arithmetic-Operations-Using-Switch

In the above program, we have first declared and initialized a set variables required in the program.

  • num1 = it holds the first number value
  • num2 = it holds the second number value
  • chr = it holds the operator choice

In the next statement user will be prompted to enter the two number values which will be assigned to variable ‘num1’ and ‘num2’ respectively. Next, we will display operator menu and ask user to input operator (+, -, *, /) choice. When user enter operator, it will be assigned to ‘chr’ variable. Then, depending on the value of ‘chr’ switch statement we will perform the calculation on the two operands. Finally, this will be displaying the result using printf statement.

C Program to Check Given Number is Prime or not

In this tutorial, we will learn a program to check provided number is Prime number or not using C programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • C Operators.
  • If else statement.
  • C loop statements.

What Is Prime number?

Any positive integer number which is greater than 1 and only it can be divisible by itself and by 1 and it has maximum two factors is called as prime number.Rather we can say that a prime number is a number which only divided by him or 1.

Example:-

Fact   2  is the only positive(even) and smallest integer prime number.

Program to check Prime number in C.

In this program we will find that given number is prime number or not using for loop.
Firstly we declare required header file and variable and also initiates required values in variable. Next we take value from user at run time and then after we will find that the given value is prime number or not.
Let’s take a  look to the program.

Output:-

C-Program-to-check-prime-number

In the above program, we have first declared and initialized a set variables required in the program.

  • no = number to hold given value.
  • count = count for number divisibility occurrence.
  • i= for iteration of a loop.

In the next statement user will enter the number which will be assigned to variable ‘no’. Now loop start from 2 to given number.In ever iteration we will check ‘no’ is completely divisible by ‘i’ or not.

Then we check if any number that divide completely we increase the value of count ++.
In starting we declare count=0 so in every iteration when value is completely divide we increment the value of count to one.
So , if given number is prime after the end of loop the value of count=1 however if given number is not the value of count is more than 1.
So , we will check that if number is a prime number then the value of count must be equal to one(1).

As we can see if our condition satisfy then the given number is prime number if not is not a prime number.

C Program to Swap two numbers Using Function

In this tutorial, we will learn to create a program to swap two numbers using function (call by reference) in C programming language.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • C Data Types
  • C Variables
  • C Input Output
  • C Operators
  • C Function
  • C Pointer

Program to Swap Numbers Using Function ( call by reference )

In this program we will swap two integer numbers using function (call by reference). We would first declare and initialize the required variables. Next, we would prompt user to input two integer numbers. Later in the program we will swap numbers by calling a function( call by reference ). Finally, we will display number values after swapping of numbers.

Output:-

C-Program-to-Swap-Two-Numbers-without-using-Temporary-Variable

In the above program, we have first defined a function called swap() as following:

swap(int *a, int *b) :- Function accept two parameter as call by reference and swap them.

In the above program, we have first declared and initialized a set variables required in the program.

  • num1 = it holds the first integer number value
  • num2 = it holds the second integer number value

In the next statement user will be prompted to enter the two integer number values which will be assigned to variable ‘num1’ and ‘num2’ respectively. Next, we will swap two integer numbers using swap() function passing ‘num1’ and ‘num2’ by reference.

C Program to Swap two numbers without third variable

In this tutorial, we will learn to create a program to swap two numbers without using temporary variable in C programming language.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • C Data Types
  • C Variables
  • C Input Output
  • C Operators

Program to Swap Numbers Without Using Temporary Variables

In this program we will swap two integer numbers without using temporary variable. We would first declared and initialized the required variables. Next, we would prompt user to input two integer numbers. Later in the program we will swap numbers without using a temporary variable. Finally, we will display number values after swapping of numbers.

Output:-

C-Program-to-Swap-Two-Numbers-without-using-Temporary-Variable

In the above program, we have first declared and initialized a set variables required in the program.

  • num1 = it holds the first integer number value
  • num2 = it holds the second integer number value

In the next statement user will be prompted to enter the two integer number values which will be assigned to variable ‘num1’ and ‘num2’ respectively. Next, we will swap two integer numbers without using a temporary variable. Finally, we will display number values after swapping of numbers.

C Program to Swap two numbers using pointers

In this tutorial, we will learn to create a program to swap two numbers using pointers in C programming language.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • C Data Types
  • C Variables
  • C Input Output
  • C Operators
  • C Pointer

Program to swap two numbers using pointers

In this program we will swap two integer numbers using pointers. We have first declared and initialized the required variables. Next, we would prompt user to input two integer numbers. Later in the program we will swap numbers using pointers. Finally, we will display number values after swapping of numbers.

Output:-

C-Program-to-Swap-Two-Numbers

In the above program, we have first declared and initialized a set variables required in the program.

  • num1 = it holds the first integer number value
  • num2 = it holds the second integer number value
  • *a = pointer variable
  • *b = pointer variable
  • temp = it is used to hold temporary value

In the next statement user will be prompted to enter the two integer number values which will be assigned to variable ‘num1’ and ‘num2’ respectively. Next, we will swap two integer numbers using pointer variable *a and *b variable. Finally, we will display number values after swapping of numbers.

C Program to Swap Two Numbers Using Bitwise Operators

In this tutorial, we will learn to create a program to swap two numbers using bitwise XOR operator in C programming language.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • C Data Types
  • C Variables
  • C Input Output
  • C Operators
  • C Bitwise Operators

Program to Swap Two Numbers Using Bitwise Operator

In this program we will swap two integer numbers entered by the user using bitwise XOR operator. We have first declared and initialized the required variables. Next, we would prompt user to input two integer numbers. Later in the program we will swap numbers using bitwise XOR operator. Finally, we will display number values after swapping of numbers.

Output:-

C-Program-to-Swap-Two-Numbers

In the above program, we have first declared and initialized a set variables required in the program.

  • num1 = it holds the first integer number value
  • num2 = it holds the second integer number value

In the next statement user will be prompted to enter the two integer number values which will be assigned to variable ‘num1’ and ‘num2’ respectively. Next, we will swap two integer numbers using bitwise XOR operator. Finally, we will display number values after swapping of numbers.

C Program to Swap Two Numbers

In this tutorial, we will learn to create a program to swap two numbers in C programming language.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • C Data Types
  • C Variables
  • C Input Output
  • C Operators

Program to Swap Two Numbers

In this program we will swap two integer numbers entered by the user. We would first declared and initialized the required variables. Next, we would prompt user to input two integer numbers. Later in the program we will swap numbers using a temporary variable. Finally, we will display number values after swapping of numbers.

Output:-

C-Program-to-Swap-Two-Numbers

In the above program, we have first declared and initialized a set variables required in the program.

  • num1 = it holds the first integer number value
  • num2 = it holds the second integer number value
  • temp = it is used to hold temporary value

In the next statement user will be prompted to enter the two integer number values which will be assigned to variable ‘num1’ and ‘num2’ respectively. Next, we will swap two integer numbers using a temporary variable. Finally, we will display number values after swapping of numbers.