Category Archives: C Programs

C Programs

C Program to Find Power of a Number

In this tutorial, we will learn to create a C program that will find the power of a given number using C programming.

Prerequisites

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

  • C Operators
  • C  Loops.
  • Basic input/output functions.

Logic behind to find power of any number without using pow() function in C programming.

Example

Input:
take  value of  base : 3
take  value of  exponent : 3
Output:
Input base is : 3
Input exponent : 3

So power of any number basen given as base*base*…..*base (n-times). Here base is called base and n is called the exponent.Here we will write a C program to find the power of a given number.

Program to find power of any number.

In this program we will find power of any given number using for loop. We would first declared and initialized the required variables. Next, we would prompt user to input the number and power of number .Then we will find the power of given number.

Output

C Program to Find Power of a Number

Note: The above program will works only if the exponent is a positive integer (number).

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

  • base = it hold the value of base 
  • expo = it will hold the power 
  • power= it will hold the result.

Let see the logic of program step by step.

Step 1. Input base and exponents from user, and put  it in two variables  base and expo.
Step 2. Initialize another variable that will store power power=1.
Step 3.within the loop value of i start from 1 to expo, increment loop  by 1 in each iteration. The structure  look like.
Step 4.For every iteration the value of  power  multiply with base.
Step 5.Finally we get the answer in Power.
In this tutorial we understand how to write a program without using Power Function In C to calculate power of a number.

C Program to Calculate Sum Of Digits In a Number

In this tutorial, we will learn to create a C program that will find sum of inputted values  using C programming.

Prerequisites

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

  • C Operators.
  • Basic input/output.
  • C loop statements.
  • C for Loop.

Example

C program to calculate the sum of digits of any number inputted by user, here we use modulus operator (%) to break digits of a number and keep on adding them.you can see what we gonna do in our example.

Logic to find sum of digits of a number

The idea behind  to find sum of digits is.

suppose number is 21.

Break the last digit of the given number.      //=> step=>  no%21=>1=>rem=1 

  1. Add the removed last digit to variable add. //=> add=add+rem. // add=0+1
  2. By removing last digit from given number.  // =>  no=21/20 => no=2.

Must repeat above steps till the number became 0. Finally you will get sum of digits.

Program for Sum of Digits of a Given Number.

In this program we will find the sum of digit of a given number  using while 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 the sum of digit using following logic.

Output

C Program to Calculate Sum Of Digits In a Number

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

  • no = number inputted by user.
  • add = adding digit.
  • rem=hold remainder in between.

Sum of digits Steps.

To get sum of each digits by c program, use the following steps.

  • Step 1: Take  any number from user.
  • Step 2: break last number of digit and store in rem. (rem=no%10)
  • Step 3: add the remainder of the number (add=add+rem).
  • Step 4: Divide the number by 10 to remove last digit of number (no=no/10).
  • Step 5: Repeat the step 2 while number is greater than 0 (no>0).

At end of all this you will get your required output i.e. sum of all digit.

C Program to Find Cube Root of a Given Number

In this tutorial, we will learn to create a C program that will find Cube Root of given number  using C programming.

Prerequisites

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

  • C Operators.
  • Basic input/output.
  • In-built functions.
  • mathematical functions.

What Is cube root?

Cube root , is a factor that we multiply by itself three times to get that number (2*2*2=8).

C Program to Find Cube Root of a Given Number.

Here in this program we are going find cube root of a given number first of all we take a value from user and pass this value to in-built function to find cube root of a number.

Output

C Program to Find Cube Root of a Given Number

You can  also use following function like  cbrt() with double and crbtl() with long double  data type.

Function cbrtf() takes only single  parameter(no) and returns the cube root.which we hold in cube_root variable.

with the help of cube_root  variable we can print the cube of given number of user

Algorithm

Algorithm for  this program is:−

START
1: →Take a variable no.
2 :→Take value from user and assign to no.
3 :→ Pass no variable to cube_root function (cbrtf(no)).
4 :→ This function returns value to cube_root variable.
5 :→ Print the value of cube_root.
STOP
Explanation of Cube root, cube root of a number is a value ,when multiplied by itself 3 times .

For example.
The cube root of number 27, denoted as 3√27, is 3,
because when we multiply 3 by itself thrice times we get  3 x 3 x 3 = 27 = 33.

C Program to Find Square Root of a Given Number

In this tutorial, we will learn to create a C program that will find Square Root of given number  using C programming.

Prerequisites

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

  • C Operators.
  • Basic input/output.
  • In-built functions.
  • mathematical functions.

What Is Square root?

Squares are found when numbers multiplying by itself 5 * 5= 25. Where a square root of a number on getting multiplied by itself gives the original √25 = 5. In other word a value that can be multiplied by itself to give the original number. 

 

C Program to Find Square Root of a Given Number.

Here in this program we are going find square root of a given number first of all we take a value from user and pass this value to in-built function to find square root of a number.

Output

C Program to Find Square Root of a Given Number

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

  • no = Which will hold given value by user
  • ctr = It will hols square root of a number.

Function sqrt() takes only a single parameter(no) and returns the square root .which we hold in sq_rt variable.

with the help of sq_rt variable we can print the square of given number by user.

Explanation

Explanation of Square root

Square root of a number is a value,that will occurs on multiplication by itself gives the original number. Any positive number, when multiplied by itself  5 * 5= 25 , represents the square of that number. The square root of the square of a positive number gives the original number √25 = 5.

Example

Input
Enter any number:  16
Output
Square root of 16 = 4.

C Program to Find LCM of Two Numbers

In this tutorial, we will learn to create a C program that  will find LCM of two number   using C programming.

Prerequisites

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

  • Operators.
  • Loop statements
  • While loop.
  • if else Statement.

What Is LCM?

LCM is a method to finding the smallest possible multiple of two or more numbers. LCM of a numbers is divisible by both the numbers. For example, LCM of 5 and 4 is 20.

C Program to Find LCM of Two Numbers

In this program we will find LCM of two gievn number using while loop. We would first declared and initialized the required variables. Next, we would prompt user to input two numbers.Later we will find the LCM of given number.

Output

C Program to Find LCM of Two Numbers

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

  • no1 =It will hold first number given by user.
  • no2 =It will hold second number given by user.
  • max=This will find biggest among the number.

Let see the step by step method of finding LCM.

LCM of two number, is as follows:
1: user  will be prompted to enter two the positive integer values no1 and no2.
2: Storing the biggest value of no1 & no2 into the max variable.
3: On checking condition whether the max is divisible by both variables no1 and no2 loop  will iterate until it will find a value which divisible by both number.
4: If variable max is divisible,Then display max as the LCM of these two numbers.
5: Else, the value of max is increased, and go to step 3 again and again.
6: Stop the program.

LCM stands for Least Common Multiple (LCM). It’s the smallest possible number that is completely divisible by both integer’s no1 and no2, without leaving the remainder. It is also known as the Lowest Common Multiple. and represented by LCM (a, b)/ lcm(a,b).

C Program to Find HCF of Two Numbers

In this tutorial, we will learn to create a C program that wil find HCF of two numbers  using C programming.

Prerequisites

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

  • Operators.
  • General C programming.
  • Basic input/output.
  • Loop statements
  • While loop.
  • if else Statement.

What Is HCF?

HCF stands for Highest Common Factor. When  we finding the factor of two numbers, Highest Common Factor of  numbers is the Largest factor that divides these two numbers. The greatest factor found from the common factors is called the HCF. It is also called the Greatest common factor.

C Program to Find HCF of Two Numbers

In this program we will find HCF of two given number using For loop. We would first declared and initialized the required variables. Next, we would prompt user to input two numbers.Later we will find the HCF of given number.

Output

C Program to Find HCF of Two Numbers

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

  • no1 =It will hold first number given by user.
  • no2 =It will hold second number given by user.
  • hcf= it will hold Greatest common factor.

Let see the step by step method of finding HCF.

HCF of two number, is as follows:
1: user  will be prompted to enter two the positive integer values no1 and no2.
2: Storing the given value in variable  no1 & no2 .
3:Initialize hcf =1.
4:Within the loop we check if  i  divide completely these two numbers.
i.e. if i exactly divides the given numbers and remainder is equal to zero, then the value of i is assigned to hcf variable.


5:When the loop is completed, the greatest common divisor of two numbers is stored in variable hcf.
6: Stop the program.

The HCF  of two integers is the greatest integer that can exactly divide both numbers (with a remainder ==0) .

C Program to find HCF and LCM

C Program to find HCF and LCM

In this tutorial, we will learn to create a C program that will find HCF and LCM  using C programming.

Prerequisites

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

  • Operators in C Programming.
  • Basic Input and Output function in C Programming.
  • Basic C programming.
  • While loop in C programming.
  • Arithmetic operations in C Programming.

Convert Number into Word

In c programming, it is possible to take numerical input from the user and find HCF and LCM with the help of a very small amount of code. The C language has many types of header libraries which has supported function in them with the help of these files the programming is easy.

With the help of this program, we can find HCF and LCM.

Here,

LCM:-Least Common Multiple.

HCF:- Highest Common Factor.

Algorithm:-

Program to find HCF and LCM:-

 

Output:-

The above program we have first initialize the required variable.

  • x = it will hold the integer value1.
  • y = it will hold the integer value2.
  • a = it will hold the integer value of x.
  • b = it will hold the integer value of y.
  • t = it will hold the integer temporary value.
  • hcf = it will hold the integer value  of calculated hcf.
  • lcm= it will hold the integer value of calculated lcm .

Input number from user.

Program Logic Code.

Printing output

C Program to Check Palindrome Number

In this tutorial, we will learn to create a C Program to check palindrome number using C programming language. In this article we will create C programs to check if the input number is palindrome or not. 1) using while loop 2) using function 3) using recursion.

Prerequisites

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

  • C if else
  • C while loop
  • C do while loop
  • C Function
  • C Recursion

What Is Palindrome Number?

A palindrome number is a number that remains the same even when its digits are reversed. For example 121, 212, 12321 are palindrome numbers. Let’s take example of 12321, when we reverse its digits we get 12321 that is equal to original number.

C Program to Check Palindrome Number Using While Loop

In this program we will check palindrome number using while loop. We would first declared and initialized the required variables. Next, we would prompt user to input an integer number. Later in the program we will to check whether given number is palindrome or not using while loop. Finally, we will display given number is palindrome or not.

Output:-

C-Program-to-Check-Palindrome-Number-using-while-loop

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

  • num = it hold the input number
  • rnum = used to hold the reverse number
  • rem = it hold remainder
  • temp = it hold temporary value

In the next statement user will be prompted to enter the an integer number value which will be assigned to variable ‘num’ . Then, we will loop through the input number to find reverse of the number and assign it to ‘rnum’. We will now compare ‘rnum’ and ‘num’ as following:

if it return true, we will display input number is a palindrome number. If it return false, we will display input number is not a palindrome number.

Check Palindrome Number Using Function

In this program we will check palindrome number by creating a user defined function. We would first declared and initialized the required variables. Next, we would prompt user to input an integer number. Later in the program we will to check whether given number is palindrome or not using a function. Finally, we will display given number is palindrome or not.

Output:-

C-Program-to-Check-Palindrome-Number-using-function

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

checkPalindrome() :- This function means to accept an integer number and return true(1) or false(0) value.

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

  • num = it holds the first integer number value

In the next statement user will be prompted to enter the an integer number value which will be assigned to variable ‘num’ . Then, we will call checkPalindrome() function to find and compare reverse of the number with original number. This will return true(1) or false(0) value.

We will now compare return value of checkPalindrome() function as following:

if the above condition return true, we will display input number is a palindrome number. If it return false, we will display input number is not a palindrome number.

Check Palindrome Number Using Recursion

In this program we will check palindrome number by creating a recursive function. We would first declared and initialized the required variables. Next, we would prompt user to input an integer number. Later in the program we will to check whether given number is palindrome or not using a recursive function. Finally, we will display given number is palindrome or not.

Output:-

C-Program-to-Check-Palindrome-Number-using-recursion

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

  • num = it holds a 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 checkPalindrome() function to return reverse number of the input number, We will assign reverse number to ‘rnum’. We will now compare ‘rnum’ and ‘num’ as following:

if it return true, we will display input number is a palindrome number. If it return false, we will display input number is not a palindrome number.

C Program to Find Greatest Number Among three Number

In this tutorial, we will learn to create a program to find greatest number among three numbers using C programming language. We will show you two different method to find greatest number among three numbers :

  • Using C If statement
  • Using Nested If else statement
  • Using If else Ladder Statement

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 If else

C Program to Find the Greatest Number Among Three Numbers Using If Statement

In this program we will compare three integer numbers and returns the greatest number as result. We would first declared and initialized the required variables. Next, we would prompt user to input three integer numbers to compare. Later in the program we will compare input numbers using C if Statement. Finally, we will display the largest number among three numbers.

Output:-

C-Program-to-Find-Greatest-Number-Among-three-Number-using-if-statement

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
  • num3 = it holds the third number value

In the next statement user will be prompted to enter the three integer number values which will be assigned to variable ‘num1’ , ‘num2’ and ‘num3’ respectively. Then, we will perform the comparison of the given three numbers respectively.

num1 is greatest when following condition returns true:

num2 is greatest when following condition returns true:

num3 is greatest when following condition returns true:

Finally, we will display the largest number among three numbers using printf statement.

C Program to Find the Greatest Number Among Three Numbers Using Nested If Else Statement

In this program we will compare three integer numbers and returns the greatest number as result. We would first declared and initialized the required variables. Next, we would prompt user to input three integer numbers to compare. Later in the program we will compare numbers using C nested if else Statement. Finally, we will display the largest number among three numbers.

Output:-

C-Program-to-Find-Greatest-Number-Among-three-Number-using-nested-if-else-statement

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
  • num3 = it holds the third number value

In the next statement user will be prompted to enter the three integer number values which will be assigned to variable ‘num1’ , ‘num2’ and ‘num3’ respectively. Then, we will perform the comparison of the given three numbers respectively. We have first if else statement to with following condition:

if it returns true, we have another if else statement:

if it return true, means  num1 is largest. If it return false, means num3 is largest.

If the outer if else statement returns false, then in the else block we have another if else statement:

if it return true, means  num2 is largest. If it return false, means num3 is largest.

Find the Largest Number Among Three Numbers Using If else Ladder Statement

In this program we will compare three integer numbers and returns the greatest number as result. We would first declared and initialized the required variables. Next, we would prompt user to input three integer numbers to compare. Later in the program we will compare numbers using C if else ladder Statement. Finally, we will display the largest number among three numbers.

Output:-

C-Program-to-Find-Greatest-Number-Among-three-Number-using-nested-if-else-statement

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
  • num3 = it holds the third number value

In the next statement user will be prompted to enter the three integer number values which will be assigned to variable ‘num1’ , ‘num2’ and ‘num3’ respectively. Then, we will perform the comparison of the given three numbers respectively. We have first if else statement to with following condition:

if it return true, means  num1 is largest. If it return false, then we have else if statement:

if it return true, means  num2 is largest. If it return false, then we have final else statement that means num3 is largest.

C Program to Check Armstrong Number

In this tutorial, we will learn to create a program to check whether a given number is an Armstrong number or not using C programming language.

Prerequisites

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

  • C if else
  • C while loop

What Is Armstrong Number?

An Armstrong number of a 3 digit number is a number in which the sum of the cube of the individual digits in number  is equal to the number itself. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers. Let’s try to understand why 153 is an Armstrong number.

153 = ((1*1*1)+(5*5*5)+(3*3*3) )

153 =( 1 + 125 + 27)

153 = 153 (So this is an Armstrong number.)

Similarly, Let’s take number 370 and understand why 370 is an Armstrong number.

370=(3*3*3+7*7*7+0*0*0)

370=(27+343+0)

370=370.(So this is a Armstrong number.)

So this is how we can check whether a given 3 digit number is an Armstrong number or not

C Program to Check Armstrong Number

In this program we will find whether a given three digit number is Armstrong number or not using while loop. We would first declared and initialized the required variables. Next, we would prompt user to input a three digit number. Later we will find given no is Armstrong number or not.

Output:-

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

  • no = take given number by user.
  • copyno = to copy original no.
  • rem= for holding remainder
  • armno= for result

Here we created a program in which we find a three digit number is a Armstrong number or not. First we copy the original no to copyno variable and then with the help of while loop iterate loop until we reach the last value or given no.

And in loop we break the number’s  last digit and multiply this number three time itself and store in armno variable and after that we break copyno to two digit no with (/) operator and copy the new two digit number in our copyno variable and repeat this process to last number then we compare the given number and armno is equal it to the given no if they are equal then the number is Armstrong number else the given number is not a Armstrong number.

C Program to Generate Fibonacci Series

In this tutorial, we will learn to generate Fibonacci series using C programming language. We will show you two different method to generate Fibonacci Series in C programming:
1) Fibonacci Series without recursion
2) Fibonacci Series using recursion

Prerequisites

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

What Is Fibonacci Series?

The Fibonacci series is a sequence of numbers where the next numbers is the sum of the previous two numbers in the series. The first two numbers of the Fibonacci sequence are 0 followed by 1.

Example:-

c-program-to-generate-fibonacci-series

In the example above, 0 and 1 are the first two terms of the Fibonacci series. The third term of series is evaluated by adding the previous two terms. In this case 0 and 1 are the previous two terms so by adding them (0+1 = 1) we get 1 as the third term of series. Similarly, the next term is evaluated by adding second and third term of the series. This process is repeated until the number of the terms requested is reached. Please have look at the image below to understand the process.

c-program-to-generate-fibonacci-series

In this program we will print fibonacci series using for loop. We would first declared and initialized the required variables. Next, we would prompt user to input the number of terms. Later we will generate the fibonacci series of specified number of terms.

Output:-

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

  • n = number of terms to be printed
  • ctr = counter for iteration
  • t1= term 1, initialized with 0
  • t2= term 2, initialized with 1
  • t3 = next term to be computed

In the next statement user will be prompted to enter the number of terms which will be assigned to variable ‘n’. Now we will loop through the iterator ‘ctr’ upto the number of terms. Inside the for loop we are first printing the value of t1(initialized with 0) then in the next statement we are computing the next term(t3) value by adding previous two terms that is t1 and t2. Then we are assigning new values to t1 and t2 as to hold previous two terms value for another next term. This will be repeated until it print the fibonacci series of required number of terms.

In this program we will print fibonacci series using a recursive function. A function is called recursive when calls itself. It very important to have termination condition in recursive function. Please take look at the example image below, to understand the process of evaluating fibonacci series of 3 terms using a recursive function.

c-program-to-generate-fibonacci-series-using-recursion

Example:-

Output:-

In the above program, we have first declared a function called as fibonacci that will recursively generate the fibonacci terms. Later in the main function, we declared required variables.

  • n = number of terms to be printed
  • t = hold the term value, initialized with 0
  • i = for iteration

In the next statement user will be prompted to enter the number of terms which will be assigned to variable ‘n’. Now we will loop through the iterator ‘i’ upto the number of terms. Inside the loop we are calling fibonacci function to return fibonacci terms recursively upto the number of terms.

Next, we have defined the fibonacci function. Inside the function, we first check if the number n is zero or one. If yes, we return the value of n. If not, we recursively call fibonacci function with the values n-1 and n-2 until it reach to a terminating condition.