Tag Archives: Basic Programs

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 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.

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.