Tag Archives: Basic Programs

C Program to Swap two numbers Using Function

In this tutorial, we will learn to create a program to swap two numbers using function (call by reference) in C programming language.

Prerequisites

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

  • C Data Types
  • C Variables
  • C Input Output
  • C Operators
  • C Function
  • C Pointer

Program to Swap Numbers Using Function ( call by reference )

In this program we will swap two integer numbers using function (call by reference). We would first declare and initialize the required variables. Next, we would prompt user to input two integer numbers. Later in the program we will swap numbers by calling a function( call by reference ). Finally, we will display number values after swapping of numbers.

Output:-

C-Program-to-Swap-Two-Numbers-without-using-Temporary-Variable

In the above program, we have first defined a function called swap() as following:

swap(int *a, int *b) :- Function accept two parameter as call by reference and swap them.

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

  • num1 = it holds the first integer number value
  • num2 = it holds the second integer number value

In the next statement user will be prompted to enter the two integer number values which will be assigned to variable ‘num1’ and ‘num2’ respectively. Next, we will swap two integer numbers using swap() function passing ‘num1’ and ‘num2’ by reference.

C Program to Swap two numbers without third variable

In this tutorial, we will learn to create a program to swap two numbers without using temporary variable in C programming language.

Prerequisites

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

  • C Data Types
  • C Variables
  • C Input Output
  • C Operators

Program to Swap Numbers Without Using Temporary Variables

In this program we will swap two integer numbers without using temporary variable. We would first declared and initialized the required variables. Next, we would prompt user to input two integer numbers. Later in the program we will swap numbers without using a temporary variable. Finally, we will display number values after swapping of numbers.

Output:-

C-Program-to-Swap-Two-Numbers-without-using-Temporary-Variable

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

  • num1 = it holds the first integer number value
  • num2 = it holds the second integer number value

In the next statement user will be prompted to enter the two integer number values which will be assigned to variable ‘num1’ and ‘num2’ respectively. Next, we will swap two integer numbers without using a temporary variable. Finally, we will display number values after swapping of numbers.

C Program to Swap two numbers using pointers

In this tutorial, we will learn to create a program to swap two numbers using pointers in C programming language.

Prerequisites

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

  • C Data Types
  • C Variables
  • C Input Output
  • C Operators
  • C Pointer

Program to swap two numbers using pointers

In this program we will swap two integer numbers using pointers. We have first declared and initialized the required variables. Next, we would prompt user to input two integer numbers. Later in the program we will swap numbers using pointers. Finally, we will display number values after swapping of numbers.

Output:-

C-Program-to-Swap-Two-Numbers

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

  • num1 = it holds the first integer number value
  • num2 = it holds the second integer number value
  • *a = pointer variable
  • *b = pointer variable
  • temp = it is used to hold temporary value

In the next statement user will be prompted to enter the two integer number values which will be assigned to variable ‘num1’ and ‘num2’ respectively. Next, we will swap two integer numbers using pointer variable *a and *b variable. Finally, we will display number values after swapping of numbers.

C Program to Swap Two Numbers Using Bitwise Operators

In this tutorial, we will learn to create a program to swap two numbers using bitwise XOR operator in C programming language.

Prerequisites

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

  • C Data Types
  • C Variables
  • C Input Output
  • C Operators
  • C Bitwise Operators

Program to Swap Two Numbers Using Bitwise Operator

In this program we will swap two integer numbers entered by the user using bitwise XOR operator. We have first declared and initialized the required variables. Next, we would prompt user to input two integer numbers. Later in the program we will swap numbers using bitwise XOR operator. Finally, we will display number values after swapping of numbers.

Output:-

C-Program-to-Swap-Two-Numbers

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

  • num1 = it holds the first integer number value
  • num2 = it holds the second integer number value

In the next statement user will be prompted to enter the two integer number values which will be assigned to variable ‘num1’ and ‘num2’ respectively. Next, we will swap two integer numbers using bitwise XOR operator. Finally, we will display number values after swapping of numbers.

C Program to Swap Two Numbers

In this tutorial, we will learn to create a program to swap two numbers in C programming language.

Prerequisites

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

  • C Data Types
  • C Variables
  • C Input Output
  • C Operators

Program to Swap Two Numbers

In this program we will swap two integer numbers entered by the user. We would first declared and initialized the required variables. Next, we would prompt user to input two integer numbers. Later in the program we will swap numbers using a temporary variable. Finally, we will display number values after swapping of numbers.

Output:-

C-Program-to-Swap-Two-Numbers

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

  • num1 = it holds the first integer number value
  • num2 = it holds the second integer number value
  • temp = it is used to hold temporary value

In the next statement user will be prompted to enter the two integer number values which will be assigned to variable ‘num1’ and ‘num2’ respectively. Next, we will swap two integer numbers using a temporary variable. Finally, we will display number values after swapping of numbers.

C Program to Print Size of int, float, double and char

In this tutorial, we will learn to create a program that finds the size of various data types such as int, float, double and char in C programming.

Prerequisites

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

  • C Data Types
  • C Variables
  • C Input Output

Program to Print Size of int, float, double and char

In this program we will find the size of various data types such as int, float, double and char using sizeof operator. In C Programming, the sizeof operator returns the amount of the memory allocated to a variable of specific datatype such as int, float, double and char. In this program we will first declare and initialize a set variables each one of different data type. Later in the program we will display size of various data types such as int, float, double and char.

Output:-

C-Program-to-Print-Size-of-int-float-double-and-char

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

  • intVar = it is a variable of integer data type
  • floatVar = it is a variable of float data type
  • doubleVar = it is a variable of double data type
  • charVar = it is a variable of char data type

Later in the program we will find the size of various data types such as int, float, double and char using sizeof operator and will print using printf statement.

C Program to Find Quotient and Remainder

In this tutorial, we will learn to create a program to find the quotient and remainder when an integer number is divided by another integer number using C programming.

Prerequisites

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

  • C Data Types
  • C Variables
  • C Input Output
  • C Operators

Program to Find Quotient and Remainder

In this program we will find quotient and remainder when an integer number is divided by another integer number. In this program user is first asked to enter two integers (dividend and divisor). Next, we will compute the quotient and remainder based on the input numbers. Later in the program we will display quotient and remainder.

Output:-

C-Program-to-Find-Quotient-and-Remainder

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

  • num1 = it holds the dividend value
  • num2 = it holds the divisor value
  • quotient = it holds the quotient
  • remainder = it holds the remainder

In the next statement user will be prompted to enter the two integer number values which will be assigned to variable ‘num1’ and ‘num2’ respectively. Next,we will computes quotient using division operator ( / ) and remainder using modulus operator ( % ). Finally, we will be displaying the quotient and remainder of the provided numbers using printf statement with corresponding format specifier.

Program to find Quotient and Remainder using function

In this program we will find quotient and remainder of given numbers using function. We would first declared and initialized the required variables. Next, we would prompt user to input two integers (divisor and dividend) and the quotient and the remainder computed using user defined function and later we will display quotient and the remainder.

Output:-

C-Program-to-Find-Quotient-and-Remainder

In the above program, we have first defined two function as following:

findQuotient() :- Function to computer quotient

findRemainder() :- Function to computer remainder

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

  • num1 = it holds the dividend value
  • num2 = it holds the divisor value
  • quotient = it holds the quotient
  • remainder = it holds the remainder

In the next statement user will be prompted to enter the two integer number values which will be assigned to variable ‘num1’ and ‘num2’ respectively. Next,we will calling findQuotient() and findRemainder() to compute and return quotient and remainder respectively. We have assigned resultant quotient and remainder to ‘quotient’ and ‘remainder’ variables respectively.Finally, we will be displaying the quotient and remainder of the provided numbers using printf statement with corresponding format specifier.

C Program to Print ASCII Value of a Character

In this tutorial, we will learn to create a program to print ASCII value of a character in C programming.

Prerequisites

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

  • C Data Types
  • C Variables
  • C Input Output

What Is ASCII value of a Character?

ASCII value is typically a numeric representation of the English characters (an integer number between 0 and 127) rather than that character itself. This integer value is referred as ASCII code of the character. For example, the ASCII value of uppercase ‘A’ is 65. This means when you assign ‘A’ to a character variable, 65 is stored in the variable rather than ‘A’ itself.

Program to Print ASCII Value of a Character

In this program we will print ASCII value of a character entered by the user. We would first declared and initialized the required variables. Next, we would prompt user to input a character. Later in the program we will display the ASCII value of the user specified character.

Output 1:-

C-Program-to-Print-ASCII-Value-of-Character-1

Output 2:-

C-Program-to-Print-ASCII-Value-of-Character-2

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

  • ch = it holds the character value entered by user

In the next statement user will be prompted to enter a character values which will be assigned to variable ‘ch’. Next, we will be displaying the ASCII value of the character using integer format specifier(%d) in printf statement.

C Program to Multiply two Floating Point Numbers

In this tutorial, we will learn to create a program to multiply two floating point numbers using C programming.

Prerequisites

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

  • C Variables
  • C Data Types
  • C Input Output
  • C Operators
  • C Function

Program to Multiply two Floating Point Numbers

In this program we will multiply two floating point numbers entered by user. We would first declared and initialized the required variables. Next, we would prompt user to input two floating point numbers. Later in the program we will multiply the numbers entered by user. We will then display the result to user.

Output:-

C-Program-to-Multiply-two-Floating-Point-Numbers

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

  • n1 = it holds the first float number
  • n2 = it holds the second float number
  • product = it holds the product of n1 and n2

In the next statement user will be prompted to enter the two floating point number values which will be assigned to variable ‘n1’ and ‘n2’ respectively. Next, we will perform multiplication of both float numbers numbers (n1 and n2) and assign result to product. Now, we will be displaying the multiplication result of two floating point numbers using printf statement.

Program to multiply two numbers using function

In this program we will multiply two floating point numbers entered by user. We would first declared and initialized the required variables. Next, we would prompt user to input two floating point numbers. Later in the program we will multiply using a user defined function and display the product of the float numbers.

Output:-

C-Program-to-Multiply-two-Floating-Point-Numbers-using-function

In the above program, We have defined a user defined function called multiply(), which means to accept two float numbers and return product of the numbers passed as parameters.

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

  • n1 = it holds the first float number
  • n2 = it holds the second float number
  • product = it holds the product of n1 and n2

In the next statement user will be prompted to enter the two floating point values which will be assigned to variable ‘n1’ and ‘n2’ respectively. Next, we have called multiply function and assigned its return value to ‘product’. Now, we will be displaying the product of two float numbers using printf statement.

C Program to check number is even or odd

In this tutorial, we will learn to create a program to check whether a given number is even or odd 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 if else statement

How to check number is even or odd

An integer number that is exactly divisible by 2 is known as even number, example: 0, 8, -24. Programmatically, we can check number is even or add using modulus (%) operator. If modulus 2 of any integer number is equals to 0 then, the number is even otherwise odd.

Program to check number is even or odd

In this program we will check a number entered by user is even or odd using modulus (%) operator. We would first declared and initialized the required variables. Next, we would prompt user to input an integer number. Later in the program we will check number entered by user is even or odd using modulus (%) operator. We will then display message number is even or odd.

Output 1:-

C-Program-to-check-number-is-even-or-odd-11

Output 2:-

C-Program-to-check-number-is-even-or-odd-2

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

  • num = it holds the input integer number

In the next statement user will be prompted to enter an integer number to check is assigned to variable ‘num’. In the next statement we check modulus 2 of integer number is equals to 0 or not i.e. if(num % 2 == 0) then the number is even otherwise odd. Now, we will be displaying the number is even or odd.

Program to check number is even or odd Using Bitwise operator

In this program we will check a number entered by user is even or odd using bitwise(&) operator. We would first declared and initialized the required variables. Next, we would prompt user to input an integer number. Later in the program we will check number entered by user is even or odd using bitwise(&) operator. We will then display message number is even or odd.

Output 1:-

C-Program-to-check-number-is-even-or-odd-11

Output 2:-

C-Program-to-check-number-is-even-or-odd-2

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

  • num = it holds the input integer number

In the next statement user will be prompted to enter an integer number to check is assigned to variable ‘num’. In the next statement we check number is even or odd using bitwis(&) operator i.e. if(num & 1 == 1) then the number is odd otherwise even. Now, we will be displaying the number is even or odd.

C program to add two numbers using function

In this tutorial, we will learn to create a program to add two integer numbers using function in C programming.

Prerequisites

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

  • C Data Types
  • C Variables
  • C Input Output
  • C Operators
  • C Function

Program to Add Two Numbers

In this program we will add two integer numbers entered by the user. We would first declared and initialized the required variables. Next, we would prompt user to input two integer numbers. Later in the program we will add the numbers using a user defined function and display the sum of the numbers.

Output:-

C-Program-to-Add-Two-Numbers

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

  • num1 = it holds the first integer number value
  • num2 = it holds the second integer number value
  • num3 = it holds the return value of the sum function

In the next statement user will be prompted to enter the two integer number values which will be assigned to variable ‘num1’ and ‘num2’ respectively. Next, we have called sum function and assigned its return value to ‘num3’. Now, we will be displaying the sum of two integer numbers using printf statement. Later in the last section we have defined a user defined function called sum(), which means to accept two integer numbers and return sum of the numbers passed as parameters.

C Program to Add Two Numbers

In this tutorial, we will learn to create a program to add two integer numbers using C programming language.

Prerequisites

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

  • C Data Types
  • C Variables
  • C Input Output
  • C Operators

Program to Add Two Numbers

In this program we will add two integer numbers entered by the user. We would first declared and initialized the required variables. Next, we would prompt user to input two integer numbers. Later in the program we will add the numbers and display the sum of the numbers.

Output:-

C-Program-to-Add-Two-Numbers

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

  • num1 = it holds the first integer number value
  • num2 = it holds the second integer number value
  • sum = it holds the sum of the number1 and number2

In the next statement user will be prompted to enter the two integer number values which will be assigned to variable ‘num1’ and ‘num2’ respectively. Next, we will perform addition of both integer numbers (num1 and num2) and assign result to sum. Now, we will be displaying the sum of two integer numbers using printf statement.