Performing Arithmetic Operations in C: A Beginner's Guide


The Arithmetic operators are some of the C Programming Operator, which are used to perform arithmetic operations includes operators like Addition, Subtraction, Multiplication, Division and Modulus.All these Arithmetic operators in C are binary operators which means they operate on two operands.

ARITHMETIC OPERATORSOPERATIONEXAMPLE
+Addition 10 + 2 = 12
Subtraction 10 – 2 = 8
*Multiplication 10 * 2 = 20
/Division 10 / 2 = 5
%Modulus – It returns the remainder after the division 10 % 2 = 0 (Here remainder is zero). If it is 10 % 3 then it will be 1.

The code is a C program that prompts the user to input two integers, performs various arithmetic operations on them, and then prints the results to the console. Here is an explanation of each line of the code:

  • int a,b,c; declares three variables a, b, and c of type int.
  • float x; declares variable x of type float.
  • printf("\nEnter 2 Nos : "); prints a prompt asking the user to enter two numbers.
  • scanf("%d%d",&a,&b); uses the scanf() function to read two integers from the user and assigns them to the variables a and b. The %d is a format specifier that tells scanf() to expect an integer input.
  • c=a+b; performs addition of a and b and assigns the value to c.
  • printf("\nTotal : %d",c); prints the value of c, which is the sum of a and b.
  • c=a-b; performs subtraction of a and b and assigns the value to c.
  • printf("\nDifference : %d",c); prints the value of c, which is the difference between a and b.
  • c=a*b; performs multiplication of a and b and assigns the value to c.
  • printf("\nMul : %d",c); prints the value of c, which is the product of a and b.
  • x=(float)a/(float)b; performs division of a and b and assigns the value to x, also, it converts both a and b to float before performing division operation.
  • printf("\nDiv : %0.2f",x); prints the value of x, which is the quotient of a and b. The %0.2f is a format specifier that tells printf() to print a floating-point number with 2 decimal places.
  • c=a%b; performs modulus operation of a and b and assigns the value to c.
  • printf("\nMod : %d",c); prints the value of c, which is the remainder of a and b.
  • return 0; The return 0; statement is used to indicate that the main function has completed successfully. The value 0 is returned

Source Code

#include<stdio.h>
int main()
{
    int a,b,c;
    float x;
    printf("\nEnter 2 Nos : ");
    scanf("%d%d",&a,&b);
    c=a+b;
    printf("\nTotal : %d",c);
    c=a-b;
    printf("\nDifference : %d",c);
    c=a*b;
    printf("\nMul : %d",c);
    x=(float)a/(float)b;
    printf("\nDiv : %0.2f",x);
    c=a%b;
    printf("\nMod : %d",c);
    return 0;
}
To download raw file Click Here

Output

Enter 2 Nos : 25 10
Total : 35
Difference : 15
Mul : 250
Div : 2.50
Mod : 5

Question-1 :Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.

Source Code

#include<stdio.h>
int main()
{
    float bs,da,hra,gs;
    printf("\nEnter Your Basic Salary : ");
    scanf("%f",&bs);
    da=bs*0.4;
    hra=bs*0.2;
    gs=bs+da+hra;
    printf("\nBasic Salary : %0.2f",bs);
    printf("\nDA : %0.2f",da);
    printf("\nHRA : %0.2f",hra);
    printf("\nGross Salary : %0.2f",gs);
    return 0;
}
To download raw file Click Here

Output

Enter Your Basic Salary : 10000

Basic Salary : 10000.00
DA : 4000.00
HRA : 2000.00
Gross Salary : 16000.00

Question-2 :The distance between two cities (in km.) is input through the keyboard. Write a program to convert and print this distance in meters, feet, inches and centimeters.

Source Code

#include<stdio.h>
int main()
{
    float km,m,cm,ft,i;
    printf("Enter The Kilometer : ");
    scanf("%f",&km);
    m=km*1000;
    cm=m*100;
    i=cm/2.54;
    ft=i/12;
    printf("\n KM : %0.2f",km);
    printf("\n M  : %0.2f",m);
    printf("\n CM : %0.2f",cm);
    printf("\n IN : %0.2f",i);
    printf("\n FT : %0.2f",ft);
    return 0;
}
To download raw file Click Here

Output

Enter The Kilometer : 30

 KM : 30.00
 M  : 30000.00
 CM : 3000000.00
 IN : 1181102.38
 FT : 98425.20

Question-3 :If the marks obtained by a student in five different subjects are input through the keyboard, find out the aggregate marks and percentage marks obtained by the student. Assume that the maximum marks that can be obtained by a student in each subject is 100.

Source Code

#include<stdio.h>
int main()
{
    int m1,m2,m3,m4,m5,total;
    float avg;
    printf("\nEnter Five Marks : ");
    scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
    total=m1+m2+m3+m4+m5;
    avg=total/5.0;
    printf("\nTotal : %d",total);
    printf("\nAverage : %0.2f",avg);
    return 0;
}
To download raw file Click Here

Output

Enter Five Marks : 75
78
98
85
99
Total : 435
Average : 87.00

Question-4 :Temperature of a city in Fahrenheit degrees is input through the keyboard. Write a program to convert this temperature into Centigrade degrees.

Source Code

/*
(32°F - 32)*5/9
*/
#include<stdio.h>
int main()
{
    float f,c;
    printf("\nEnter The Temperature : ");
    scanf("%f",&f);
    c=(f-32)*(5.0/9.0);
    printf("\nResult : %f",c);

    return 0;
}
To download raw file Click Here

Output

Enter The Temperature : 100
Result : 37.777779

Question-5 :The length & breadth of a rectangle and radius of a circle are input through the keyboard. Write a program to calculate the area & perimeter of the rectangle, and the area & circumference of the circle

Source Code

#include<stdio.h>
int main()
{
    float l,b,r_area,r_peri,r,c_area,cir;
    printf("\nEnter Length & Breadth :");
    scanf("%f%f",&l,&b);
    printf("\nEnter Radius :");
    scanf("%f",&r);
    r_area=l*b;
    r_peri=(2*(l+b));
    c_area=3.14*r*r;
    cir=2*3.14*r;
    printf("\nArea of Rectangle : %0.2f",r_area);
    printf("\nPerimeter of Rectangle : %0.2f",r_peri);
    printf("\nArea of Circle : %0.2f",c_area);
    printf("\nCircumference of Circle : %0.2f",cir);

    return 0;
}
To download raw file Click Here

Output

Enter Length & Breadth :10 20
Enter Radius :5
Area of Rectangle : 200.00
Perimeter of Rectangle : 60.00
Area of Circle : 78.50
Circumference of Circle : 31.40

Question-6 :Two numbers are input through the keyboard into two locations C and D. Write a program to interchange the contents of C and D.

Source Code

#include<stdio.h>
int main()
{
    int a,b;
    printf("\nEnter 2 Nos : ");
    scanf("%d%d",&a,&b);//5 10
    printf("\n A : %d   B : %d",a,b);
    a=a+b;//15
    b=a-b;//5
    a=a-b;//10
    printf("\n A : %d   B : %d",a,b);
    return 0;
}
/*
a=a*b;
b=a/b;
a=a/b;
*/
To download raw file Click Here

Output

Enter 2 Nos : 10 15
 A : 10   B : 15
 A : 15   B : 10

Question-7 :If a five-digit number is input through the keyboard, write a program to calculate the sum of its digits. (Hint: Use the modulus operator ‘%’)

Source Code

#include<stdio.h>
int main()
{
    int a,b,d,f,sum=0;
    printf("\nEnter 5 Digit No : ");
    scanf("%d",&a);//12345
    b=a/10;//1234
    sum+=a%10;//5
    d=b/10;//123
    sum+=b%10;//4
    f=d/10;//12
    sum+=d%10;//3
    sum+=f/10;//1
    sum+=f%10;//2
    printf("\nTotal : %d",sum);
    return 0;
}

/*
 int a,b,c,d,e,f,g,h,i,sum=0;
    printf("\nEnter 5 Digit No : ");
    scanf("%d",&a);//12345
    b=a/10;//1234
    c=a%10;//5
    sum+=c;//sum=sum+5
    d=b/10;//123
    e=b%10;//4
    sum+=e;
    f=d/10;//12
    g=d%10;//3
    sum+=g;
    h=f/10;//1
    sum+=h;
    i=f%10;//2
    sum+=i;
    printf("\nTotal : %d",sum);
    return 0;
*/
To download raw file Click Here

Output

Enter 5 Digit No : 12345
Total : 15

Question-8 :If a five-digit number is input through the keyboard, write a program to reverse the number

Source Code

#include<stdio.h>
int main()
{
    int a,b,c,d,e,f,g,h,i,result;
    printf("\nEnter 5 Digit No : ");
    scanf("%d",&a);//12345
    b=a/10;//1234
    c=a%10;//5
    d=b/10;//123
    e=b%10;//4
    f=d/10;//12
    g=d%10;//3
    h=f/10;//1
    i=f%10;//2
    result=(c*10000)+(e*1000)+(g*100)+(i*10)+h;
    printf("\nReverse of 5 Digit No %d is  : %d",a,result);

    return 0;
}
To download raw file Click Here

Output

Enter 5 Digit No : 12345
Reverse of 5 Digit No 12345 is  : 54321

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C