C – Programs using Arithmetic operators

Program to add 2 numbers:

#include <stdio.h>
int main() {
            int a, b, c;
            printf(“Enter two numbers : \n”);
            scanf(“%d%d”, &a, &b);
            c = a+b;
            printf(“Sum is : %d \n”, c);
            return 0;
}

Program to find the sum and average of 3 numbers:

#include <stdio.h>
int main(){
            int a, b, c, sum, average;
            printf(“Enter two numbers : \n”);
            scanf(“%d%d%d”, &a, &b, &c);
            sum = a+b+c;
            average = sum/3;
            printf(“Sum is : %d \n”, sum);
            printf(“Average is : %d \n”, average);
            return 0;
}

Program to find the sum of First N numbers:

int main() {
            int n, sum;
            printf(“Enter n value : “);
            scanf(“%d”, &n);
            sum = n*(n+1)/2;
            printf(“Sum of first %d numbers is : %d \n”, n, sum);
            return 0;
}

Program to swap two numbers:

int main(){
            int a, b, c;
            printf(“Enter 2 numbers : \n”);
            scanf(“%d%d”, &a, &b);
            printf(“Before swap : %d \t %d \n”, a, b);
            c = a;
            a = b;
            b = c;
            printf(“After swap : %d \t %d \n”, a, b);
            return 0;
}

Program to display the last digit of given number:

int main(){
            int n, last;
            printf(“Enter number : “);
            scanf(“%d”, &n);
            last = n%10;
            printf(“Last Digit is : %d \n”, last);
            return 0;
}

Program to remove the last digit of given number:

int main()
{
            int n;
            printf(“Enter number : “);
            scanf(“%d”, &n);
            n = n/10;
            printf(“Removed last digit : %d \n”, n);
            return 0;
}

Program to swap 2 numbers without using 3rd variable:

#include <stdio.h>
int main(){
            int a, b;
            printf(“Enter 2 numbers : \n”);
            scanf(“%d%d”, &a, &b);
            printf(“Before swap : %d \t %d \n”, a, b);
            a = a+b;
            b = a-b;
            a = a-b;
            printf(“After swap : %d \t %d \n”, a, b);
            return 0;
}

Calculate total amount to pay based on quantity of fruits purchased:

#include <stdio.h>
int main(){
            float quantity, price, amount;
            printf(“Enter quantity of fruits : “);
            scanf(“%f”, &quantity);
            printf(“Enter price per dozen : “);
            scanf(“%f”, &price);
            price = (quantity/12) * price;
            printf(“Total amount to pay : %f \n”, price);
            return 0;
}

Program to display total salary for given basic salary:

#include <stdio.h>
int main(){
            float basic, ta, da, hra, total;
            printf(“Enter basic salary : “);
            scanf(“%f”, &basic);
            ta = 0.2*basic;
            da = 0.15*basic;
            hra = 0.25*basic;
            total = basic + ta + da + hra;
            printf(“Total salary : %f \n”, total);
            return 0;
}
Scroll to Top