• Post author:
  • Reading time:2 mins read
  • Post category:C Programs

In this example, you will learn how to divide two integers.

In order to understand this programming example you should have some knowledge on:

Divide Two Integers Entered by the User in C Programming

Methodology

  • Input 2 integers.
  • Check if the divisor is zero if the divisor is zero then exit.
  • Use the division operator ( /) to divide two numbers and store the result in a variable.
  • Print the result.

Program

#include<stdio.h>
int main()
{
  int result,var1,var2;
  printf("Enter the values for var1 and var2 \n");
  scanf("%d %d",&var1,&var2);
  if(var2==0){
   printf("The value for var2 cannot be zero\n");
    return 0;
   }
  result=var1/var2;
  printf("The result is %d \n",result);
  return 0;
}

Output

C program to divide two integers

Code explanation

First, declare two integer variables to store the user inputted values. Then using printf() function print “Enter the values for var1 and var2”. After that read the input values entered by the user using scanf(). These 2 values are stored in var1 and var2 respectively. Those two values are divided using the ‘/’ operator. In order to avoid dividing by zero error, if statement is used. The result will be stored in a variable named result. This result will be displayed on the console using print().