Category Archives: C Programs

C Programs

C Program to print Alphabets from A to Z

In this tutorial, we will learn to create a C program which will print alphabet  form A-Z(Capital) using C programming.

Prerequisites

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

  • Data type
  • C  loops.

Program to Print Alphabets from A-Z.

This a program to print the English alphabets (A-Z) is very simple. The following code is used to print English alphabets form (A-Z).

Here we are going to create a simple c program in which we will display how a alphabet program code and logic works lets begun with our code.

C Program to Print Alphabets from A-Z.

In this program we will print complete series from A-Z using for loop.
Firstly we declare required header file and variable.It is not as  complicated as you imagined. Let’s see the code.

Output

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

  • alpha= for holding value from A-Z.

Here we creating a program in which we print series from ‘A-Z’

Here first we take a variable name alpha and Initialize loop  variable from alpha=’A’, that goes till alpha=’Z ‘, then increment the loop each iteration by 1. The structure of loop look like this

within the loop we print the value of alpha and we will get the following series.

C Program to print Alphabets from A to Z

In this program, the loop display the English alphabet in Uppercase.

C Program to print from 1 to 100 numbers

In this tutorial, we will learn to create a C program that  will print 1-100 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.
  • Basic C Programming.
  • Looping statements.
  • For loop.

Program to print from 1 to 100 numbers.

In the example, we are going to write a Program to Print 1 to 100 number  with using  for Loops.

Let’s see the C code to print 1 to 100 numbers using for  loop.

Program to demonstrate series 1-100.

In this program we will print a series from 1-100 using for loop.
Firstly we declare required header file and variable and also initiates required values in variable. Next we print the series  from  1-100 using loop statement.

In this above program , we are using for loop to print numbers from 1 to 100.

Output

C Program to print from 1 to 100 numbers

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

  • i= for iteration from 1-100.

firstly we have to create a variable named i

Within the program, we used the for loop Statement to check the value of i is less than or equal to 100(i<=100). If the condition is true ,then statements inside the loop will be executed

While the condition is true the print statement from the loop body is executed.which will help to print the series from 1 to 100 .

C Program to Solve Second Order Quadratic Equation

 In this tutorial, we will learn to create a C program that will find Second Order Quadratic Equation and a C program of it using math.h header file using C programming.

Prerequisites

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

  • C Operators
  • Looping statements
  • Basic input/output.
  • Basic C Programming
  • Inbuilt library functions.

What Is Second Order Quadratic Equation?

In Algebra, a quadratic equation is, a equation whose quadratic polynomial or, a polynomial of degree 2 is a function with 1 or more variables in which the highest-degree term is of the second degree.Second degree polynomial, also referred as a quadratic equation .Polynomial function whose general form is:=>

C Program to Solve Second Order Quadratic Equation

In this program we will find quadratic equation.First of all we declare and initialized the required variables.
Next,we would prompt user to input the values of a,b and c then with the help of sqrt() function we will calculate the first and second root or equations.

Output

C Program to Solve Second Order Quadratic Equation

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

  • a,b,c = for holding coordinates
  • rt1= shows first root.
  • rt2= shows second root.

In this program first of we prompted user to input values of a,b and c after getting the values of a,b and c and with the help of inbuilt function sqrt.

whose define in math.h header file we going to find the Second Order Quadratic Equation.

 

as we know the formula of Quadratic Equation. we will use formula to get first and second root of given coordinates using c program.

C program to calculate sum of n numbers

In this tutorial, we will learn to create a C program that will calculate the sum of n natural 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 loop statements.

Logic behind to Calculate the Sum of Natural Numbers.

All natural numbers contains  positive value  starting from 1, 2, 3, to n numbers . let us take an example to calculate the sum of starting 20 numbers. Here  we start adding the numbers from 1 to the given number suppose 20 is a given , this process is called the sum of N natural numbers.

Example

C program to calculate sum of n numbers.

In this program we will calculate sum of n numbers with use of 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  add the given number values.

Output

C program to calculate sum of n numbers

Mathematical Explanation

Here we represent to find the sum of n natural numbers using the mathematical formula:

If we want to calculate the sum of the first 5 natural number using above formula.

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

  • no = number for calculating n natural number.
  • sum= adding all natural numbers.
  • i= i for loop iteration .

In the above program, when user enter a positive number 20 and stores it in variable no the loop continuously iterates over and over till it reaches condition where  i <=20. In every  iteration, the value of i is added to sum, and i is incremented by 1 in every loop. When the condition false, it exits the loop and prints the sum of 20 natural numbers.

C Program To Print First 10 Natural Numbers

In this tutorial, we will learn to create a C program that will display First 10 Natural numbers       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 loop statements

What Is Natural  Number?

Natural numbers As such, it is a whole, non-negative are all number 1, 2, 3, 4,5,6,7,8,9,10… They are usually counting  number and they will continue to infinity.while Whole number are all natural numbers including 0.

Example

C program to display first 10 natural numbers.

In this program we will print  starting 10  natural integer values. Using For loop.
C Program to Print Natural Numbers from 1 to 10 using for Loop.

output

C Program To Print First 10 Natural Numbers

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

  • j= for iteration of loop.

In this program we will need only one variable for traversing first 10 natural number.

Within this Program to display 1 to 10 natural number.

  • The first we will create  an integer variable j.
  • then, we use For Loop to iterate between j=1 to j<=10 and
  • Within the For loop, we print the value of j .
  • In our  C Programming example, shows  value from 1- 10.

that’s all fro this post thank-you.

C Program to Add reversed number with Original Number

 In this tutorial, we will learn to create a C program that add reversed number with Original 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.
  • Basic input/output operations.

C Program to Add reversed number with Original Number.

In this program first of all we reverse a number and that number will be added to original number. For example, user input number 321, the reverse number will be 123.and we add 321 to 123 => 321+123 =  444. Let see the code for that.

Output

C Program to Add reversed number with Original Number

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

  • no= take number from user.
  • rev = it will hold reverse value.
  • temp=  it will hold original .
  • sum= add original and reverse number.

In the next statement user will be prompted to enter a number into variable no. In our program, we use the modulus (%)operator to obtain digits.
How reversing a number is defined by the following steps:

  • 1: START.
  •  2:Take a number from user.
  •  3: SET temp =no, sum =0.
  •  4: REPEAT while (no != 0)
  •  5: rev = rev * 10
  •  6:rev = rev + no%10
  •  7:no = no/10
  •  8: sum = no+ temp
  •  9: Print Sum
  • 10: END.

First of all we take a number from user in variable no. and pass this number to temp variable after that in while loop we reverse the number as show in below image.

After reversing a number we add the reverse and temp variable.

As you can see how the number is reversing in following steps after the reversing number we add original number sum=temp+rev with reverse number to get required output and print the final value stored in sum variable.

C Program to Count number of digits in number without using mod operator

C Program to Count the number of digits in number without using the mod operator.

In this tutorial, we will learn to create a C program that will Count the number of digits in number without using the mod operator 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.
  • String functions in c programming.

Count the number of digits in number without using the mod operator.

As we all know the c programming is a very powerful language. In the c programming language, we have many pre-defined functions for arithmetic operations. but today we will count the number of digits in a number without using the mod operator.

With the help of this program, we can count the number of digits in numbers without using the mod operator.

Algorithm:-

 

Program to count the number of digits in numbers without using the mod operator:-

 

Output:-

Including header files.

In the above program, we have first initialized the required variable.

  • ch[10] = it will hold the string value.
  • digits = it will hold the integer value for numbers.
  • no = it will hold the integer value of number input.

Taking input number from the user.

Converting that number to a string.

Storing the string size by using a predefined function.

Printing output of the program.

C Program to Add numbers without using arithmetic Operators

In this tutorial, we will learn to create a C program that add two number without using arithmetic operator(+)  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
  • C while Loop.

Program to Add two numbers without using arithmetic Operators.

Easiest way for  adding two numbers without using arithmetic operator ‘+’ in C is with using increment(++) and decrement(–) operator .

In this program we will add two 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 add these two values without using arithmetic operator.

Let see the code for our program.

Output

C Program to Add numbers without using arithmetic Operators

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

  • a = taking value from user in first variable.
  • b = taking value from user in second variable.

For adding 2 integer values without using arithmetic operators(+,-*,/), we can do this with using increment and decrement operator.

Take a look in below image.

The above condition tells you what is actually we have done  , we used increment and decrement  operator instead of ( + ) or ( ) to add two numbers

Firstly  we got two numbers from  user in a variable a and b . Then, we check  the condition until the second variable  b is not equal to  0. If it’s zero, we terminate the loop . If it’s a not zero number, then execute the following statement .

in which in each iteration the value of a increment to 1 and the value of b decrement to 1.The above statement will run until while (b!=0).

then in each iteration value of a is added and this process continues and we get final required output.

C Program to Add two numbers without using arithmetic Operators also possible Using logarithm and exponential functions  and either using pointers or using  bit-wise operators.

C Program to Demonstrate Printf inside Another Printf Statement

C Program to Demonstrate Printf inside Another Printf Statement

In this tutorial, we will learn to create a C program that will Demonstrate Printf inside Another Printf Statement 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.

Demonstrate Printf inside Another Printf Statement :-

As we all know every programming language the program cannot be completed without taking input or returning output.  In C programming language printf() and scanf() functions are inbuilt library functions in  which are available in C library by default. These functions are declared and related macros are defined in “stdio.h” which is a header file in C language.

We only have to include the header file named as “stdio.h” as shown in below C program to make use of these printf() and scanf() library functions in C language.

Printf function is used to write the output or show message on the screen.

In nested printf Inner most printf() function writes the output of the program.

Second printf() function counts the output number’s digits and write them.

And third printf() function will write the number of digits returned by the second printf() function

 

Algorithm:-

Program:-

Demonstrate Printf inside Another Printf Statement.

 

 

Output:-

The above program we have to first initialize the required variable.

  • no = it will hold the integer value.

Taking Input from the user an integer value.

Printing using Printf inside Another Printf Statement.

 

C Program to Display Day of the month

In this tutorial, we will learn to create a C program that will accept year,month and date from user and display the day of month 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

C Program to Display Day of the month

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 shut down or turn off computer

C program to shut down or turn off computer

In this tutorial, we will learn to create a C program that will shut down or turn off the computer 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.
  • Header Libraries and its usage.

Shut down or Turn off computer

In c programming it is possible shut down or turn off 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.

Shutting Pc with the help of c program is so easy.

Algorithm:-

Program to shut down or turn off the computer :-

Program For Windows 7:-

Note to shutdown immediately use “C:\\WINDOWS\\System32\\shutdown -s -t 0”. To restart use “-r” instead of “-s”.

Here, “shutdown /s /t x”; where x is the number of seconds after which shutdown will occur.

Program For Windows xp:-

Note to shutdown immediately use “C:\\WINDOWS\\System32\\shutdown -s -t 0”. To restart use “-r” instead of “-s”.

Here, “shutdown /s /t x”; where x is the number of seconds after which shutdown will occur.

Program for Ubuntu:-

 

Output:-

The program we have to first include stdlib.h and then we call the system function.

C program to get IP address

C program to get IP address

In this tutorial, we will learn to create a C program that will get the IP address of computer 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.
  • Array in C Programming.
  • Header Libraries and its usage.

Get IP address

In c programming, it is possible to take or fetch the ip address from the computer and convert it into words or can display in words 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.

Algorithm:-

Program:-

 

Output:-

The above program we have first initialize the required variable.

  • no = it will hold the integer value ip.
  • ifreq = it will hold the interface name in the ifreq structure.
  • ifr = it will hold the address type.
  • array[] = it will hold the integer value for numbers of ip address.

Including the header files required for program

Body of program retrieving the ip address

Main Program