Tag Archives: Basic Programs

c program to calculate compound interest

In this tutorial, we will learn to create a C program that  will Calculate Compound Interest    using C programming.

Prerequisites

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

  • Arithmetic operators.
  •  Basic input/output
  • C loop statements
  • Operator precedence
  • Data types.

What Is Compound Interest?

Compound interest (or compounding interest) is an interest to the principal sum of loan or deposit by consumer or by bank to consumer, or in other words,Compound Interest is interest on interest.

CI is interest earned from the original principal +  accumulated interest.

formula for Compound Interest is.

Here,

P is principle amount.
R is the rate in Percent.
T is time in year.

C Program to Calculate Compound Interest.

Output

c program to calculate compound interest

c program to calculate simple interest

In this tutorial, we will learn to create a C program that Find Simple Interest using C programming.

Prerequisites

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

  • Basic C programming.
  • Operators.
  • Basic input/output.
  • Basic mathematics calculation.

What Is Simple interest ?

Simple interest is the interest amount given by the bank within a year to consumer or taken by bank from consumer  for a particular principal amount of money at some rate of interest. Simple Interest is a method of calculating the interest for a loan/principal amount.

Where:=>
SI = simple interest.
P = principal
R = interest rate (in percentage).
T = time duration (in years).

C Program to Calculate Simple Interest.

Output

c program to calculate simple interest

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

  • si = For calculating si.
  • p=  for principal.
  • r= for rate of interest.
  • t= for time interval in a year.

First of all we declare 4 variable si,p,r and t then by prompting user to give principal rate and time . Here in this program we get principal rate and time from user.

After accepting values of  principle, rate and time from user , we apply the formula to calculate simple interest is which  is si= (principle x rate x time) / 100. and  after calculating si we will print the Simple Interest. 

Simple Interest is a method used by mostly in Banking and Economic sectors to calculate the interest charges on loans or in saving account by Bank or Economic Sectors.

C Program to Print 1 to 10 Without Using Loop

C Program to Print 1 to 10 Without Using Loop

In this tutorial, we will learn to create a C program that will Print 1 to 10 Without Using Loop in 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.
  • Conditional statements in c programming.

As we all know the String is a collection of character data types. In strings, only one variable is declared which can store multiple values.In c programming language we can do all the work for multiple printing with the help of loops but in today’s tutorial we will complete the program without using the loops.

There are two ways to complete this program.

With the help of this program, we can Print 1 to 10 Without Using Loop.

Algorithm 1:-

Algorithm 2:-

Program 1 to Print 1 to 10 Without Using Loop:-

Program 2 to Print 1 to 10 Without Using Loop:-

Output:-

The above program we have printed each line manually by writing the codes.

 

Output 2:-

The above program we have first initialized the set of variables for the program.

  • i = it will hold the integer value.

User defined Function for printing.

 

Printing the numbers from the function.

Calling the user defined function.

C Program to Find Factor of a Given Number

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

Prerequisites.

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

  • Operators.
  • If else statement.
  • C for Loop.
  • Basic input/output.

What Is Factor of number is?

Factors:=> So factors of a number are numbers that divide the number completely and remainder of divisor is equal to zero.

Example:->

The number 6 has four factors: 1, 2, 3,  and 6 because all this number divide the number six completely.

C Program to Find Factor of a Given Number.

In this program we will  find factor of given number  using if else statement and for loop. We would first declared and initialized the required variables. Next, we would prompt user to input the number . Later we will find factors.

Output

C Program to Find Factor of a Given Number

In this program  we will take a positive integer from user by prompting  and with the help of loop and if else statement  we will displays all the positive factors of that number.

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

  • no = It will hols number given by user.
  • j= for iteration of loop and finding  factor.

So the logic behind computing factors of a number is we will run a loop from 1 to no, increment 1  in each iteration.
The loop structure should look like this

Within the loop  in each iteration we check the condition that j is a factor of no or not. To check factor we use divisibility of number by using  modulo division

i.e. if( no % j == 0) then j is a factor of no If j is a factor of no then we will print the value of j.So we will get the required factors of given number.

C Program to Check Lower Upper Case

In this tutorial,  will learn to find  entered character is an Uppercase or in a lower case  using C programming language.

Prerequisites

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

  • Basic Programming
  • Operators.
  • looping statements
  • For Loop
  • If else statement.
  • Basic input/output.

What Is Upper and lower case.

Uppercase letters are capital letters. they usually signal reader that something is important or significant in a Text.Uppercase letters represent the beginning of a sentence or a proper noun.
uppercase letters: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z.

lowercase letters In writing, most letters are lowercase. Lowercase letters are all letters that do not begin a sentence or refer to a proper noun.Each word in this sentence other than the first word is written in lowercase.
lowercase letters: a b c d e f g h i j k l m n o p q r s t u v w x y z.

C Program to Check Lower Upper Case

Output

C Program to Check Lower Upper Case

Buzz Fizz Program in C

In this tutorial, we will learn to create a C program that  Find BUZZ FIZZ  using C programming.

Prerequisites

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

  • Operators
  • Basic C programming.
  • Basic input/output.
  • Looping statements
  • For Loop
  • If else statement.

What Is Buzz Fizz?

The concept of Buzz and Fizz is if integers between 1 to N, print “Fizz” if an integer is divisible by 3, “Buzz” if an integer is divisible by 5, and “FizzBuzz” if an integer is divisible by both 3 and 5.

Example:

A program that prints the numbers from 1 to no(suppose 15) .If number is a multiple of ‘3’ print “Fizz” instead if multiples of ‘5’ print “Buzz” and if multiple of 3 and 5 both then print “Fizz Buzz”.
1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14,
Fizz Buzz.

Buzz Fizz Program in C.

In this program we will print find  Buzz Fizz  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  find  Buzz Fizz series of specified number of terms.

Output

Buzz Fizz Program in C

 

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

  • no = number of terms to be printed
  • i     = for iteration of loop.

Here in this program we will print  number from 1 to no.If number is  multiple of 3, we print “Fizz” instead of the number. as you can see in image below.


And if number is multiple of 5, print “Buzz” instead of the number.


And in final case if numbers which are multiples of both 3 and 5, print “FizzBuzz” instead of the number.So in our program we take a number from user at run time,suppose user gives a number 15 then the series of fizz , buzz  and fizzbuzz is look like this.

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