Skip to main content

Arithmetic Operations in C language

Arithmetic operations are addition, subtraction, multiplication ,division and modulus. All these operations can be performed in the C programming using arithmetic operators. Arithmetic operators are ( + , - , * , / ,%).Using these operators we can perform different types of operations.





Program To add two numbers.
    #include <stdio.h>
    int main()
    {
    int a,b,c;
    printf("Enter the value of a :");
    scanf("%d",&a);
    printf("Enter the value of b :");
    scanf("%d",&b);
    c=a+b;
    printf("Addition of a and b is %d.",c);
    return 0;
    }
    


Output :
    Enter the value of a :5
    Enter the value of b :2
    Addition of a and b is 7.
    



In these program the first line "#include <stdio.h>" is the header file which is used for basic input and output in the program."int main()" here int is an return type which will return a integer value, "{" is the starting of the main function block. Then we take three variable as a,b,c which is of integer type, if you want decimal number replace "int" by "float". 'a' will store the first integer value, 'b' will store second integer value and 'c' will store the result of a + b. "printf" will display the message on the output screen and "scanf" will read the value given by the user. c=a+b is the operation to be performed and then printf will print the result on the output screen.

Similarly we can perform the operations like subtraction, multiplication, division, modulus in C as -
Program To subtract two numbers.
    #include <stdio.h>
    int main()
    {
    int a,b,c;
    printf("Enter the value of a :");
    scanf("%d",&a);
    printf("Enter the value of b :");
    scanf("%d",&b);
    c=a-b;
    printf("Subtraction of a and b is %d.",c);
    return 0;
    }
    


Output :
    Enter the value of a :8
    Enter the value of b :4
    Subtraction of a and b is 4.
    


Comments

Popular posts from this blog

Loops in C programming.

Loops are very important concept in C language and any other programming language. Loops are important to convert the larger program to smaller ones. Loops are 'the sequence of statement which continues to execute until the desired condition is met.' There are 3 types of loops in C language : 1. for loop. 2. while loop. 3. do while loop. 1. for loop The syntax of for loop is " for(initialization;expression;increment/decrement)". For loop is also called as entry controlled loop , where we initialize statement, give condition, and update statement in a single line. The for loop statement will be executed until the condition evaluation is true. Whenever the condition fails the control skip the for loop. Flowchart of for loop. Simple program to print number from 1 to 10 using 'for loop'. #include <stdio.h> void main() { int i; for(i=0...

Basics of C language

Basics of C language C language is the most commonly used programming language. C language is invened by Dennis Ritchie. It is used in wide range of aaplications. Before C language there ware Assembly level language and Basic compiling programming language(BCPL). Assembly level language code is leanthy as compared to C language code, therefore C language is preffered over BCPL and Assembly level language. Basic Structure of C language Documentation Section Link Section Defination Section Global Declaration Section Main function Section Subprogram Section C program always starts with a include file. SYNTAX : #include filename where filename may be stdio.h, conio.h, math.h, string.h, etc. depending upon the operation to be performed in the program. Most commonly used is stdio.h , which stands for standard input output header file. math.h is used to perform mathematical operations in the program, similarly other...