Tag Archives: Basic Programs

C Program to Add Two Numbers using Pointer

In this tutorial, we will learn to create a C program that  will add two number using pointer in 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.
  • Pointers.
  • Basic C programming.

What Is Pointer?

A pointer variable are who hold the  address of another variable.
i.e  direct address of the memory location using &(address) operator.

C Program to Add Two Numbers using Pointer.

In this program first of all we take values from user and passes the address or value to the pointer variable and without using the actual variable we will calculate addition of them using pointer .let see the code now.

Output

C Program to Add Two Numbers using Pointer

Explanation.

In the program, we have  2 integer variables a and b and 2 pointer variables *p1 and *p2 . Now we assign the addresses of a and b to p1 and p2 receptively.

and then adding value of a and b using *p1 and *p2 into  add.

Here

variable p1 holds the address of variable a with &(address) operator and p2 holds the address of variable b. So here & operator provide the address of variables.

and with the help of *(Asterisk) operator we can access the value of given address using pointer variable.

as we can see in above image p1 holds the address of variable a and p2 holds the address of variable b and without using a and b we are adding the values of a and b  using pointer.

So in this program we have calculate two values using pointer variables .

C Program To find Sum of GP Series

In this tutorial, we will learn to create a C program that will find Sum of GP 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.
  • C Operators.
  • Basic input/output.
  • Loops.
  • For loop.

What Is geometric progression(GP)?

GP stands for geometric progression. It is defined as a Geometric Sequence of numbers each term is found by multiplying the previous term by a constant.

Example

This sequence has a factor of 2.Each term (except the first term) is found by multiplying the previous term by 2.

Generally we write a Geometric Sequence like that:
{a, ar, ar2, ar3, … }
where:

  • a is the first term, and
  • r is the factor between the terms (called “common ratio”).

C Program To find Sum of GP Series

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

Output

C Program To find Sum of GP Series

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

  • no = Total Numbers of terms to be printed
  • f =  First term of series.
  • cr= common ratio.
  • sum= adding values of GP.
  • temp=  holding temporary values. 
  • i= for iteration.

In the next statement user will be prompted to enter the number of terms which will be assigned to variable ‘no’ and first value in variable f and common ratio in cr.

Now ,We will loop through the iterator ‘i’ up to the number of terms.Inside the for loop we are printing the value of temp variable and adding the sum of series in variable sum. When loop ends we print the sum of given gp. So in that way we find the sum of given gp number.

C Program To find sum of AP series

In this tutorial, we will learn to create a C program that will find sum of AP Series  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.
  • C loop statements
  • C for Loop.
  • Basic input/output.
  • Conditional statement.
  • If-else statement.

What Is Arithmetic Progression series?

A Series with same constant difference is known as arithmetic series. This constant is called  common difference.The first element of series is  a  and constant difference is d. The series is looks like a, a + d, a + 2d, a + 3d,a+4d . . no. 

Example.

If a=1 and d=3 and total_term=5

C Program To find sum of AP series

In this program we will print A.P 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 A.P series of specified number of terms and sum of terms.

Output
C Program To find sum of AP series

Formulas used

Sum of  series:

Final_term of  series:

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

  • Total_no= number of terms to be printed.
  • d = common difference.
  • final_term= Last term of series.
  • i=  for iteration.
  • sum = adding sum of series.

In the next statement user will be prompt to enter the first term,common difference and total term in the series in following variable total_no,a,d .
The formula used in this program is.

Where Total_no is the last term of a finite sequence and d is common difference and sum the sum of Total_no terms.
Then, we will find the sum of the A.P Series. Here, we used  Loop to display the A.P series.

You can see in image how series  printing and showing the sum or series.

C Program To Count number of vowels in a string

In this tutorial, we will learn to create a C program that  will find the number of vowels in a string  using C programming.

Prerequisites

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

  • Basic input/output.
  • loop statements.
  • for Loop.
  • Array.

What Is  vowel ?

Let us take an example, in the following string “Education” there are five vowels ‘a’,’e’,’i’,’o’ and ‘u’. here in this program we check every character inputted by user in a string and find the number of vowel in it.

C Program To Count number of vowels in a string.

First of all In this program we take string from user,and with the help of array and loop we will find number of vowel in given string.

Output

C Program To Count number of vowels in a string

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

  • str = number of terms to be printed
  • len = counting length of string
  • vowel=  for counting number of vowel
  • i= for iteration of loop.

Explanation.

In this program we will do following task step by step.

  • First of all we will take string as input.
  • then with help of strlen() function we find the length of a string.

  • Then check  each character of the string to check as shown in above  image.
  • If the string  character is a vowel, increment the counter value of vowels.
  • Print the total number of vowels in the end.

C Program to Print small Alphabets a to z

In this tutorial, we will learn to create a C program which will print small alphabet form a-z using C programming.

Prerequisites

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

  • c data type
  • C  loops.
  • Basic input/output.

C Program to Print Alphabets from a-z(small)

In the following  program with the help of for loop we will print alphabet form ‘a-z‘. Firstly we take a variable name ‘alpha‘  of type character .Then initialize loop form alpha=’a’ that will goes till alpha=’z’  and the structure of loop be like this

within the loop body print the value of  alpha.

C Program to Print Alphabets from a-z(small).

In this program we will print value from a -z using for loop.
Firstly we declare required header file and variable. Next we print complete value form a-z using or loop.
Let’s take a  have look.

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 small Alphabets a to z

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

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.