Category Archives: C Programs

C Programs

C Program to Sort a String in Alphabetical Order

C Program to Sort a String in Alphabetical Order

In this tutorial, we will learn to create a C program that will Sort a String in Alphabetical Order 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 String functions of c language.

Sort a String in Alphabetical Order

As we all know the String is a collection of character data types. In strings, only one variable is declared which can store multiple values. First will take the string from the user. Then will check the number of elements of the string. Now we will arrange that string in alphabetical order.

With the help of this program, we can Sort a String in Alphabetical Order

Algorithm:-

Program to Sort a String in Alphabetical Order:-

 

 

Output:-

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

  • string1[] = it will hold the string value.
  • flag = it will hold the temporary value.
  • i = it will hold the integer value for the loop.
  • j = it will hold the integer value for the loop.
  • n = it will hold the size of the string.

Including the required header files.

Taking Input string.

Printing before the alphabetical order.

Printing a String in Alphabetical Order.

Program Code for arranging.

C Program to Concatenate Two Strings Using strcat

C Program to Concatenate Two Strings Using strcat

In this tutorial, we will learn to create a C program that will Concatenate Two Strings Using strcat 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.
  • Using String functions of c language.

Concatenate Two Strings Using strcat

As we all know the String is a collection of character data types. In strings, only one variable is declared which can store multiple values. First will take the number of elements of string 1 from the user. Then will take the number of elements of string 2 from the user. Then will take the elements from the user for string 1 and as well as for string 2. And concatenate them using the strcat function.

With the help of this program, we can Concatenate Two Strings Using strcat

Algorithm:-

Program to Concatenate Two Strings Using strcat:-

 

 

Output:-

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

  • string1[100] = it will hold the string value.
  • string2[100] = it will hold the string value.

Taking Input string 1 from the user.

Taking Input string 2 from the user.

Printing Output using the strcat function which is available in c language.

C Program to Copy String Without Using strcpy

C Program to Copy String Without Using strcpy

In this tutorial, we will learn to create a C program that will Copy String Without Using strcpy 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 String functions of c language.

Copy String Without Using strcpy

As we all know the String is a collection of character data types. In strings, only one variable is declared which can store multiple values. First will take the string from the user. Then will pass that string to a for loop for copying. The C programming language has many pre-defined functions for string manipulation. but in today’s tutorial, we will do without using strcpy().

With the help of this program, we can Copy String Without Using strcpy

Algorithm:-

Program to Copy String Without Using strcpy:-

 

Output:-

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

  • string1[100] = it will hold the string value.
  • string2[100] = it will hold the string value.
  • i = it will hold the integer value for the loop.

Taking Input string from the user.

Reading the input string from the user.

Copying the string with the help of for loop without using strcpy() function.

Printing output copied string2 without strcpy.

C Program to Find the Length of a String

C Program to Find the Length of a String.

In this tutorial, we will learn to create a C program that will Find the Length of a String 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 String functions of c language.

Find the Length of a String.

As we all know the String is a collection of character data types. In strings, only one variable is declared which can store multiple values. First will take the string from the user. Then will pass that string to a for loop for copying. The C programming language has many pre-defined functions for string manipulation. but in today’s tutorial, we will do it with the help of for loop.

With the help of this program, we can Find the Length of a String.

Algorithm:-

Program to Find the Length of a String:-

 

Output:-

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

  • string1[100] = it will hold the string value.
  • string2[100] = it will hold the string value.
  • i = it will hold the integer value for the loop.

Taking input string from the user.

Reading the input string from the user/Storing the input from the user.

Find the length using for loop of a given string.

Printing the length of the string

C Program to Remove all Characters in a String Except Alphabet

C Program to Remove all Characters in a String Except Alphabet

In this tutorial, we will learn to create a C program that will Remove all Characters in a String Except Alphabet 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.
  • For loop in c programming.
  • While loop in c programming.

Remove all Characters in a String Except Alphabet.

As we all know the String is a collection of character data types. In strings, only one variable is declared which can store multiple values. First will take the string from the user. Then will pass that string to a for loop to filter the numbers from that string.

With the help of this program, we can Remove all Characters in a String Except Alphabet.

Algorithm:-

 

Program to Remove all Characters in a String Except Alphabet:-

 

 

Output:-

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

  • line[150] = it will hold the string value.
  • i  = it will hold the integer value for the loop.
  • j = it will hold the integer value for the loop.

Taking input string from the user.

Scanning the input string from the user.

Storing the string and extracting only string data by for loop.

Printing output of the program.

C Program to Count the Number of Vowels, Consonants and so on

C Program to Count the Number of Vowels, Consonants and so on

In this tutorial, we will learn to create a C program that will Count the Number of Vowels, Consonants, and so on 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.
  • For loop in c programming.

Count the Number of Vowels, Consonants, and so on

As we all know the String is a collection of character data types. In strings, only one variable is declared which can store multiple values. First will take the line of string from the user. Then will take the number of elements in the string, Now we will filter the string using the c programming.

With the help of this program, we can Count the Number of Vowels, Consonants, and so on.

Algorithm:-

Program to Count the Number of Vowels, Consonants, and so on:-

 

Output:-

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

  • line[150] = it will hold the string value.
  • vowels = it will hold the integer value for vowels.
  • consonant  = it will hold the integer value for consonants.
  • digit= it will hold the integer value for numbers.
  • space= it will hold the integer value for spaces.

Input strings from the user.

Assigning blank value to all to prevent garbage value.

Calculating the size of the string.

Filtering vowels from the string.

consonants filtering from the string.

filtering numbers from the string.

filtering white spaces from the string.

Printing output.

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.