Category Archives: C Programs

C Programs

C program to print date

In this tutorial, we will learn to find current system date day and time  using C programming language. There are many  different method to find system date and time in C programming.

Prerequisites

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

  • C library.
  • Operators.
  • in-built  functions.
  • Basic input/output.
  • Basic C programming.

Often we need to work on current date and time in our program.

C program to print date

Here we write a program, in which we want to  check current date and time to  given date and time to confirm the user age .Now we will see how to write a C program to print current date and time.

Output

Here how the above program works:

Here we have taken The time_t datatype it is a data type from the C ISO library  which is define to store system’s time value.

time() function will returns the system date and time.

With the help of  function ctime()  it will returns a string that shows the local time based on t (parameter) timer. This ctime() function is defined in time.h header file.we just need to include inbuilt header file in our program and function define within header file just need call them and that’s it our will call system day and all..

C program to print date

that will give the current day, month , date , time and year..

C program to check number is positive negative or zero

In this tutorial, we will learn to create a C program that will find weather a number is positive, negative or Zero  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.
  • Nested else-if
  • Basic c programming.
  • Basic input/output.

C program to check whether a number is positive, negative or zero.

In this program we will find whether a number is positive, negative or zero series using if else statement. We would first declared and initialized the required variables. Next, we would prompt user to input any number. Later we will find that number is positive,negative or zero.

Output

Positive Value.

Negative Value

Zero Value.

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

  • number = it will hold the value of number.

In the next statement user will be prompt to enter any number terms which will be assigned to variable ‘number’.

For finding given integer is positive ,negative or zero we will use We will use nested if else statement

in above logic inside if we check  if(number > 0), number is greater than zero then number is positive.

if(number < 0) if number less than zero then number is negative. and if

if(number == 0), then number is zero.

Here the C program to find whether a number is positive, negative or zero.The above approach is easiest way find the problem. First we check condition for positive number, then for negative or and later we find for zero.

C Program to print all Happy Numbers till N

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

Prerequisites

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

  • C Operators.
  • C loop statements.
  • C while and for Loop.
  • Conditional statement.

What Is Happy Number ?

Happy number is a number which finally ends at 1 when it is replaced by the sum of the square of its digits repeatedly.

Example

let’s take an example here 13 is a happy number

,   and      .

On the other hand,The unhappy number will result in a cycle of 4, 16, 37, ….

2 is not a happy number let’s have a look an example of this

and  this process continues to infinite cycle without ever reaching 1

C Program to print all Happy Numbers till N

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

Output

C Program to print all Happy Numbers till N

happy number is defined by the following steps:

  • STEP 1: START.
  • STEP 2: SET tmp =0, sum =0.
  • STEP 3: REPEAT STEP 4 to 6 UNTIL (no>0)
  • STEP 4: tmp=no%10.
  • STEP 5: sum = sum + (tmp*tmp)
  • STEP 6: no= no/10.
  • STEP 7: RETURN sum.
  • STEP 8: END.
  • If the number given is greater than 0 (no>0 ),First we find remainder by dividing the number with 10  ( tmp=no %10 ).
  • Calculate square of tmp and add it to sum.
  • Then divide number by 10 ( no=no/10).
  • Repeat the steps from 4 to 6 until the sum of the square of all digits present in number has been calculated.
  • Finally, print  the sum.

So finally we get all the happy number from one to given number by user…..

C Program to print whether given Number is Happy or not

In this tutorial, we will learn to create a C program that will find the given number is a happy 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.
  • Loop statements.
  • While and for Loop.
  • Conditional statement.

What Is Happy Number ?

Happy number is a number which finally ends at 1 when it is replaced by the sum of the square of its digits repeatedly.

Program to print whether given Number is Happy or not ?

in this program we are going to find that the given number is happy or not ?.As we explained happy number is a number which finally ends at 1 when it is replaced by the sum of the square of its digits repeatedly.In this program we will find that given number is happy 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 happy number or not.

Output

C Program to print whether given Number is Happy or not

A number is said to be happy if it leads to 1 when we calculate the square of each digit present in number and them. if resulting sum is equal to 1 then the given number we can say it is a happy number.

example.

Let’s understand with an example:

In our example we have taken 13 to find that this number is happy or not,we  first break 13 to get the sum of squares of its digits which gives us another number (10), we interchange 13 by 10 to continue this process until we get result as 1.So 13 a happy number.As we discuss  above  happy number is a number that will give 1 at the end on continues after a sequence of steps wherein each step number is replaced by the sum of squares of its digit.
In our example we can see that 13 is a happy number.

C program to enter 5 subjects marks and calculate percentage.

In this tutorial, we will learn to create a C program that  will take five subjects marks and Calculate Percentage   using C programming.

Prerequisites

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

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

Example.

Here we will create a program in which user enter the marks of five subjects and calculate percentage. And you will learn how to find percentage of the five subjects in C language.

C Program to Calculate entered 5 subjects marks and calculate percentage.

First of all we take five number from user and add them to find total  then divide the total by 5 to get our desired percentage .Here is a program for that have a look.

Output

C program to enter 5 subjects marks and calculate percentage.

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

  • m = holds value of math.
  • p = holds value of physics.
  • c = holds value of chemistry
  • h = holds value of Hindi.
  • e= holds value of English.
  • total = calculate sum of value.
  • per= it will hold percentage.

In this  C program to calculate Percentage of Five Subjects .

The first printf statement will ask the user to enter the 5 subjects number.

then we add all 5 given  value  in variable total ( total =m+p+c+h+e ).
Now, we calculated the percentage (per=total/5).

And we will get the required output.This program allows user to enter five values of five different  subjects. And then calculate the Total and Percentage of those Five Subjects.

C program to enter two angles of a triangle and find the third angle.

In this tutorial, we will learn to create a C program that will find side of a Triangle.If two side of a Triangle are given   using C programming.

Prerequisites

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

  • Arithmetic operators
  • Data types
  • Basic input/output.

Basic Properties of triangle

The sum of the interior angle on the same vertex is 180°

 

C Program to enter two angles of a triangle and find the third angle.

Here we take two side of a triangle and with the help of angle property we will find the required third side of a triangle ,Let’s take a look to program.

output

C program to enter two angles of a triangle and find the third angle.

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

  • ang1 =it is for holding the angle value 1.
  • ang2 =it is for holding the angle value 2.
  • ang3 =it is for holding the angle value 3.

One of the common property of a triangles is that all three(3) interior angles (∠a + ∠b +∠c) of a triangle on adding will give to 180 degrees. This is an important theorem of geometry also known as Triangle Angle Sum Theorem(∠a + ∠b +∠c).According to this Theorem, the sum of the three interior angles in a triangle is always 180°.

So using this triangle  angle sum theorem we create a program in which we get two angle from user and by adding them subtract from 180 -(ang1+ang2) we will get our required third angle .

as we can see here by adding two given angle and subtract them from 180 we get the third required angle ..

 

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.