Tag Archives: Basic Programs

C program to find power of any 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

 

Program to find power of any number.

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.

Output

C program to find power of any 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 step by step descriptive logic.

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 convert days into years, weeks and days

In this tutorial, we will learn to create a C program that will accept year, month and date form user  and display of the day  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 switch case.
  • Function in C.

Example:

If you give value input in particular format  like year=2021, Month=5, and day is 12 then this  program will display the output as The Given  Date is : 12/05/2021 and Day of Week of the Date : Wednesday.

C Program to Display Day of the month.

In this program we  will print name of the Day of week and Date of calendar for given year, month and day.  Please  check out the program below  to  print, Name of Day of week and Date of calendar.

Output

 

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

This program will accept three values from user Year,Month and Date.Then we passes all three value to function called week_day it will accept all three values.With the help or week_day() function we calculate month day and week day.Within this function we gain called a function findmonth(). we find the month value and returns the value to week_day()  function where we will find the day of week and print our required output.

 

 

C Program To Print Perfect number between 1 and given number

In this tutorial, we will learn to create a C program that will find perfect number between  1 to entered 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.
  • C loop statements.
  • Nested loop.
  • Conditional statement.

What Is Perfect Number is?

Perfect number is a positive integer number,that equals to sum of its positive divisors not include the number itself is equal to that number.

Example

Divisor of  6 are 1,2 and 3. and sum of these  are (1+2+3=6). So we can say that 6 is a perfect number in which we will not induced 6 i.e exclude 6 .So number is  called perfect when divisor of number added and that create the number itself called Perfect number.

C Program To Print all Perfect number between 1 and given number.

In this program we will find that given number is perfect 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 perfect number or not.

Output

C Program To Print Perfect number between 1 and given number

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

  • end_value   =  holding given number
  • sum              = adding all divisor. 
  • i                     = for iteration of a loop.
  • j                     = for iteration of a loop.
Logic behind finding the perfect number is ..
Let : no = 6
Proper divisors of number 6 are 1, 2, 3.
Sum of all its proper divisors exclude 6 = 1 + 2 + 3 = 6.
Hence 6 is a perfect number.

Now run the  loop from 1 to end_value ,by  incrementing i with 1 in each iteration. The loop structure should look like this

and within the loop for each iteration printing the value of i if it is a Perfect number.

So here is the program that  will find  1 to given number perfect number.

C Program to Check Number is Perfect Or Not

In this tutorial, we will learn to create a C program that will find Perfect 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.
    • Loop statements.
    • For Loop.
    • Basic input/output.

What Is Perfect Number?

Perfect number is a positive integer number,that equals to sum of its positive divisors not include the number itself is equal to that number.

Example

Divisor of  6 are 1,2 and 3. and sum of these  are (1+2+3=6). So we can say that 6 is a perfect number in which we will not induced 6 i.e exclude 6 .So number is  called perfect when divisor of number added and that create the number itself called Perfect number.

C Program to Check Whether Number is Perfect Or Not

So here we are going to create a program in which we take a value from user and we will find the given number is perfect or not? By method using adding all divisors of a number.

Output

Perfect number.

C Program to Check Number is Perfect Or Not

Not a perfect number.

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

  • no   =  holding given number
  • sum= adding all divisor. 
  • i       = for iteration of a loop.

Explanation of program, First we take the integer value in ‘no’ variable. So the condition is that A Perfect number is a number which is equal to sum of its divisor.

Example, 6’s divisor  are 1, 2 and 3. and there sum is (1+2+3=6) ,the divisors of  6. So the number 6 is called as perfect number.

From the above image of a perfect number in c,Suppose  user entered value is: no = 6
Step by step explanation for loop is here.

In 1st Iteration…
Number is no= 6, sum = 0 and loop start at i = 1.
If (no % i == 0)  // if( 6%1== 0 ) true.
here number is complete divisible So,
sum = sum + i  // sum= 0 + 1 =1.

Now,Second Iteration..
Values of  sum and i has altered as sum = 1 and i = 2.

If (no % i == 0) // if( 6 %2 ==0 ) true.
sum = sum + i // sum = 1 + 2 = 3.

Third Iteration.
Again the values of both sum and i has altered as sum = 3 and i = 3

If (6 % 3 == 0)  // if( 6%3==0) true.
sum = sum + i //sum = 3 + 3 = 6.

Next fourth and fifth iterations ,condition inside the if will fail
6 % 4 == 0 (FALSE)
6 % 5 == 0 (FALSE)

In next iteration, the value of i becomes 6, So (6 < 6) false . So, the compiler will terminate the for loop.
Then, we check whether the value of sum  is exactly equal to the no or Not.

If it is TRUE, the below print f statement will execute. else print f statement will execute.

C Program to find ncr and npr

In this tutorial, we will learn to create a C program that will Calculate Permutation(nPr) and Combination(nCr)  using C programming.

Prerequisites

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

  • C Operators.
  • Functions in C.
  • Factorial.
  • C Recursion.

What Is Permutation (nPr) and Combination (nCr)?

Permutation:=>

permutation is an ordered arrangement . The term arrangement is referred if the order of things is considered.
Formula. nPr = n!/(n-r)!

Note->Permutation  shows arrange r things out of n.

What is Combination?

“Combination” is any selection or pairing of values within a single category
i.e  combination means selection of things, Order of objects does not matter.
Formula. n!/(r!*(n-r)!).

Note->Combination shows  to select r things out of n.

C Program to find ncr and npr.

Output

C Program to find ncr and npr

In the above program, we have first declared and initialized a set variables required in the program.Firstly we ask user to give the value of n,r by prompting user and by getting the value we pass the value or n and r to function find_combination(n,r)

This function take two parameter while calling and within function we call another function named factorial that will find the possible combination of n and r and at the end this function returns total combination back to caller object and same process we will use for permutation.

This function take two parameter while calling and within function we call another function named factorial that will find the possible permutation of n and r and at the end this function returns total permutation back to caller object and at end we will print total number of combination and permutation.

 

C Program to check vowel or consonant

In this tutorial, we will learn to create a C program that will check whether the entered alphabet by the user is a vowel or a consonant using C programming.

Prerequisites

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

  • C Operators
  • Condition statement.
  • logical operator.

What Is Vowel and consonant?
Vowel :=> Sound that allowing breath to flow out of mouth, without closing any part of the mouth.
Consonant:=> Sound  made by blocking air from flowing out of the mouth with the teeth or throat.

Example

As we know A E I O U are called vowels. And renaming alphabets except these 5 vowels are called consonants.

Program to Check the given value is Vowel or consonant.

So here we creating a program in which we will find the given character is vowel or consonant.
As we know a,e,i,o,u are only vowel and rest are consonant.So simply we will check these and if given character belongs to them  then it is a vowel simple let’s have a look at program.

Output

Note: Here we assumes that the user will enter an alphabet. If not so then non-alphabetic character, it displays the character is neither a vowel nor a consonant.

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

  • ch= hold given character.

First of all  user will be prompted to enter any character.Then with the help of program we will  check whether inputted character is a vowel or consonant,using following logic

As user inputs any character we check whether it’s a vowel both lower-case and upper-case are checked

If a character belong to this condition then it is vowel,and if   doesn’t meet it’s a consonant,and here we also check if it is not vowel and consonant it might be a digit or a special symbol.

C Program to check vowel or consonant

So in this tutorial we have learn to find given character is vowel or consonant.

C Program to Check Leap Year

In this tutorial, you will learn to create a c program to find given year is a leap year or not.

Prerequisites

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

  • C Operators
  • Conditional statement.
  • Basic input output .

Example:

So logic behind to check whether a year is leap or not is:
Logic of Leap Year:
If the given value is completely divisible by 4, 100 and 400 then it is a leap year.
If the given value is divisible by 4 but not  by 100 then it is a leap year.

Logic for  not a Leap Year:
If the given value is not divisible by 4 then it is not a leap year
If the given value is divisible by 4 and 100 but not divisible by 400 then it is not a leap year.

For example,

  • 1221 is not a leap year.
  • 2000 is a leap year.
  • 1890 is not  a leap year.

Program to Check given year is a Leap Year or not.

Here fist we take a year from user and with help of logical operator(&& and ,|| or) we will check multiple condition in a single if statement and get the required result.

Output

C Program to Check Leap Year

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

  • year =taking year value from user.

First of all take a value from user to check whether a year is leap or not is,
then we check if the given value is completely divisible by 4, 100 and 400 then it is a leap year.
If the given value is divisible by 4 but not  by 100 then it is a leap year.

C Program to Print a Semicolon Without Using a Semicolon

C Program to Print a Semicolon Without Using a Semicolon

In this tutorial, we will learn to create a C program that will Print a Semicolon Without Using a Semicolon in C programming.

Prerequisites

Prior to starting this tutorial, we assume that you are best aware of some basic concepts of c programming language.

  • Operators in C Programming.
  • Basic Input and Output function in C Programming.
  • Basic C programming.
  • Concepts of conditional statements.

In todays’ session, we will learn to create a program that will print a semicolon on the screen without using “;”. The semicolon is terminology for completing every statement in the c programming language. The semicolon is used for ending loops and conditional statements.

However, we won’t be using the “;” in the print statement.

With the help of this program, we can Print a Semicolon Without Using a Semicolon.

Algorithm:-

Program to Print a Semicolon Without Using a Semicolon:-

Method type 1 to print a semicolon.

Method type 2 to print a semicolon.

 

 

Output:-

Printing the semicolon.

Method 1 for printing the semicolon on the output console.

Method 2 for printing the semicolon on the output console.

 

 

C Program to Convert Number into Word

C Program to Convert Number into Word

In this tutorial, we will learn to create a C program that will convert Number into Word  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.
  • Switch Case statement in C Programming.
  • Header Libraries and its usage.

Convert Number into Word

In c programming it is possible to take numerical input from the user and convert it into words  with the help of 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 Convert Number into Word.

Algorithm:-

Program to convert Number into Word:-

 

Output:-

The above program we have first initialize the required variable.

  • no = it will hold the integer value.
  • r = it will hold the value of digit.
  • sum = it will hold the sum.

Including the header files required for program.

Input number from user.

Switch Case body.

C Program to Print Without Semicolon

C Program to Print Without Semicolon

In this tutorial, we will learn to create a C program that will Print Without semicolons 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.
  • Concepts of for loop.
  • Using while statement of C language.
  • Using Switch in C programming.

In the C programming language, a semicolon is used to complete any string or statement. but in today’s tutorial, we will learn to create a program that will print a statement without using semicolons. The semicolons are used to end or complete any function.

There are three methods by which we can do this.

  1. Using conditional statements.
  2. With the help of a switch case.
  3. By using looping statements.

With the help of this program, we can Print Without a Semicolon.

Algorithm 1:-

Method 2 Algorithm:-

Method 3 Algorithm:-

 

Program to Print Without a Semicolon:-

Method type 1 to print a semicolon with the help of conditional statement if.

 

Method type 2 to print a semicolon with the help of a switch.

 

Program type 3 to print a semicolon with the help of a while.

 

Output:-

Printing without the semicolon 1.

Method 2 for printing the semicolon on the output console.

Method 3 for printing the semicolon on the output console.

Code for program 1

Program 2 Code

Code For Program 3

C Program to Generate Random Numbers

In this tutorial, we will learn to create a C program that  will generate  a random number  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.
  • Looping statements
  • For Loop.
  • Basic input/output.
  • Inbuilt library function.

What Is Random number?

Random number is are set of digits (i.e., 0, 1, 2, 3, 4, 5, 6, 7, 8, 9) arranged in irregular order.

As you can see here some random number between 1 to 10 are shown in above image we will use here a rand() function to generate 10 different random number in our our program let us see how to do that.

C Program to Generate Random Numbers.

Here in our program we are going to use rand() inbuilt function which is define in header file stdlib.h with the help of this function we genrate 10 differnet random number let have a look at program.

Output

C Program to Generate Random Numbers

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

  • no = For holding random values 
  • i = for iteration.

First of all we declare two variable i for iteration and no  for holding value provided by rand() function. Here in this program we use rand() function to generate 10 random number in our program.In our program we use for loop to print Ten different random value using rand() function at multiple times.

As we can see in above image that in each iteration the rand() function called every time gives us a new random number between 1-10 because we use range from 1-10 to print 10 different values if we want to print ten different values form 1-100  then with rand() function we have to use rand()%100 it will gives us ten different value between 1-100.

c program to calculate simple interest using function

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

Prerequisites

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

  • C Operators.
  • C functions,
  • basic input/output.

What Is Simple interest?

 Simple interest is basically a calculation of the interest charged on a given principal amount with in a particular part of time by the bank to customer or taken by bank.

Simple Interest = (p * n * r) / 100

here,    p = Principal,

             n = Time Period or year.

             r = Rate of Interest.

Example

Let take an example of this,

p=5000,r=10,and t=1 year.

then,

Output

Algorithm

  1. Firstly define Principal, rate and Time of loans.
  2. Apply the formula.
  3. Output Simple Interest.

C Program to Calculate  Simple Interest.

First of all  we take principal ,rate and time  and pass them to function as a argument that will calculate the simple interest.let see a program for these.

Output

 

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

  • a = for taking value of Principal.
  • b = for taking value of rate.
  • c= for taking value of time.
  • p= for holding value of a.
  • r = for holding value of b.
  • t= for holding value of c.
  • si= calculate simple interest.

This program takes three  values of principal,rate and time in a,b,c from user and then passing these value in function Simple_int(a,b,c) as argument the above function after  calculating value function returns simple interest.

As shown in above figure. The value return by the function is hold by result variable and with help of result we can print the simple interest using function.