C Program to Print Integer
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h> int main() { int a,b,*p1,*p2,add; printf("enter values of a and b"); scanf("%d%d",&a,&b); // taking values from user p1=&a; // passing the address of values a p2=&b; // passing the address of values b add=*p1 + *p2; // calculating values. printf("additon of values =%d",add); // print result return 0; } |
Output

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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <stdio.h> #include<math.h> int main () { int f, no, cr, temp, i; //Variable Declaration float sum = 0; printf ("\nEnter First Number of an G.P Series: "); scanf ("%d", &f); //First term<strong> of</strong> series. printf ("\nEnter the Total Numbers in this G.P Series: "); scanf ("%d", &no); //Total Numbers of terms printf ("\nEnter the Common Ratio: "); scanf ("%d", &cr); //Common ratio temp = f; printf ("The G.P Series is : "); //Printing the series for (i = 0; i < no; i++) { printf ("%d ", temp); sum = sum + temp; //Update the sum in each iteration temp = temp * cr; } printf ("\nSum of G P Series is = %f\n", sum); return 0; } |
Output

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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> int main() { int a, total_no, d, final_term, i; int sum = 0; printf("Enter First Term of Series: "); scanf("%d", &a); printf("Enter the Total Numbers in our Series: "); scanf("%d", &total_no); printf("Enter the Common Difference: "); scanf("%d", &d); // mathematical formal for A.P sum = (total_no * (2 * a + (total_no - 1) * d)) / 2; final_term = a + (total_no - 1) * d; // finding final term. printf("\n The Sum of Series A.P. : "); for(i = a; i <= final_term; i = i + d) // printing all values { if(i != final_term) printf("%d + ", i); else printf("%d = %d", i, sum); // printing sum } printf("\n"); return 0; } |
Output

Formulas used
Sum of series:
|
1 |
Sum = Total_no/2(2 *a + (Total_no-1) d) |
Final_term of series:
|
1 |
Tn = a + (n-1) d |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> void main() { char str[100]; int len,i,vowel=0; printf("\nENTER A STRING: "); gets(str); // getting string from user len=strlen(str); // finding length of a string for(i=0;i<len;i++) { // checking vowels in a string if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u' || str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]=='O' || str[i]=='U') vowel=vowel+1; } printf("\nThere are %d Vowels ian a given String",vowel); //printing number of vowels } |
Output

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.
|
1 2 3 4 5 6 7 8 9 10 11 |
#include<stdio.h> int main() { char alpha; // Declaring the variable printf("Alphabets from a to z are :\n "); for (alpha ='a'; alpha <= 'z'; alpha++) // Traversing each character { printf(" %c\t",alpha); // Printing the alphabet from a-z } return 0; } |
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.

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.
|
1 2 3 4 5 6 7 8 9 10 11 |
#include<stdio.h> int main() { char alpha; // Declaring the variable printf("Alphabets from A to Z are :\n "); for (alpha ='A'; alpha <= 'Z'; alpha++) // Traversing each character { printf(" %c\t",alpha); // Printing the alphabet from A-Z } return 0; } |
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.

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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h> int main() { int i; // declaring variable printf("Program to print from 1 to 100 numbers \n"); for(i = 1; i <= 100; i++) // traversing from 1-100 { printf("%d ",i); //printing the required output } return 0; } |
In this above program , we are using for loop to print numbers from 1 to 100.
Output

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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h> #include <math.h> int main() { double a,b,c,rt1,rt2; //declaring variables. printf("enter values of a , b and c"); scanf("%lf%lf%lf",&a,&b,&c); // taking values from user rt1 = (-b + sqrt(b*b-4.*a*c) ) / (2.*a); //calculating quadratic values rt2 = (-b - sqrt(b*b-4.*a*c) ) / (2.*a); printf("\n First root is %lf ",rt1); //printing roots 1 and 2 values printf("\n Second root is %lf ",rt2); return 0; } |
Output

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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> void main() { int no, i, sum = 0; //declaring variables printf(" Enter a positive number: "); scanf("%d", &no); // taking any positive number for (i = 0; i <= no; i++)// traverse until the condition remains true. { sum +=i; //adding value in each iteration } // display the sum of number printf("\n Sum of the given %d no is: %d", no, sum); } |
Output

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?
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include<stdio.h> int main() { int j; //declaring variable printf(“First 10 Natural numbers are\n"); for(j=1;j<=10;j++) // loop for 10 natural values { printf("%d \t",j); // printing required output. } return(0); } |
output

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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdio.h> int main() { int no, rev = 0,sum=0,temp; // declaring variables printf("Enter a number to reverse\n"); scanf("%d", &no); // taking number from user. temp=no; while (no != 0) // iterate till number became zero { rev = rev * 10; rev = rev + no%10; //reversing the value no = no/10; } printf("Reverse of the number = %d\n",rev); //printing reverse number sum=temp+rev; // adding them printf("sum of original + rev is number = %d",sum); printing original and rev number return 0; } |
Output

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.

