Addition of two numbers in C The addition of two numbers in C language is performing the arithmetic operation of adding them and printing their sum on the screen. For example, if the input is 5, 6, the output will be 11. Addition program in C #include <stdio.h> int main() { int x, y, z; printf("Enter two numbers to add\n"); scanf("%d%d", &x, &y); z = x + y; printf("Sum of the numbers = %d\n", z); return 0; } Addition without using third variable #include<stdio.h> int main() { int a = 1, b = 2; /* Storing result of the addition in variable a */ a = a + b; printf("Sum of a and b = %d\n", a); return 0; } C program to add two numbers repeatedly #include <stdio.h> int main() { int a, b, c; char ch; while (1) { printf("Input two integers\n"); scanf("%d%d", &a, &b); getchar(); c = a + b; printf("(%d) + (%d) = (%d)\n", a, b, c); printf("Do you wish to add more numbers (y/n)\n"); scanf("%c", &ch); if (ch == 'y' || ch == 'Y') continue; else break; } return 0; } C program for addition of two numbers using a function We can calculate sum of two integers using a function. #include<stdio.h> long addition(long, long); int main() { long first, second, sum; scanf("%ld%ld", &first, &second); sum = addition(first, second); printf("%ld\n", sum); return 0; } long addition(long a, long b) { long result; result = a + b; return result; } Sum of Natural Numbers Using for Loop #include <stdio.h> int main() { int n, i, sum = 0; printf("Enter a positive integer: "); scanf("%d", &n); for (i = 1; i <= n; ++i) { sum += i; } printf("Sum = %d", sum); return 0; } C program to calculate sum of n numbers using an array #include <stdio.h> int main() { int n, sum = 0, c, array[100]; scanf("%d", &n); for (c = 0; c < n; c++) { scanf("%d", &array[c]); sum = sum + array[c]; } printf("Sum = %d\n", sum); return 0; } C program for addition of n numbers using recursion #include <stdio.h> long calculate_sum(int [], int); int main() { int n, c, array[100]; long result; scanf("%d", &n); for (c = 0; c < n; c++) scanf("%d", &array[c]); result = calculate_sum(array, n); printf("Sum = %ld\n", result); return 0; } long calculate_sum(int a[], int n) { static long sum = 0; if (n == 0) return sum; sum = sum + a[n-1]; return calculate_sum(a, --n); } C Program to check whether the given integer is positive or negative #include <stdio.h> void main() { int num; printf("Enter a number: \n"); scanf("%d", &num); if (num > 0) printf("%d is a positive number \n", num); else if (num < 0) printf("%d is a negative number \n", num); else printf("0 is neither positive nor negative"); } Program to Check Even or Odd #include <stdio.h> int main() { int num; printf("Enter an integer: "); scanf("%d", &num); // True if num is perfectly divisible by 2 if(num % 2 == 0) printf("%d is even.", num); else printf("%d is odd.", num); return 0; } C program to find HCF and LCM C program to find HCF and LCM: The code below find the highest common factor and the least common multiple of two integers. HCF is also known as the greatest common divisor (GCD) or the greatest common factor (GCF). C programming code #include <stdio.h> int main() { int a, b, x, y, t, gcd, lcm; printf("Enter two integers\n"); scanf("%d%d", &x, &y); a = x; b = y; while t = b = a = } (b != 0) { b; a % b; t; gcd = a; lcm = (x*y)/gcd; printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd); printf("Least common multiple of %d and %d = %d\n", x, y, lcm); return 0; } C Program for Sum the digits of a given number # include<stdio.h> /* Function to get sum of digits */ int getSum(int n) { int sum = 0; while (n != 0) { sum = sum + n % 10; n = n/10; } return sum; } int main() { int n = 687; printf(" %d ", getSum(n)); return 0; } Program to Check Prime Number #include <stdio.h> int main() { int n, i, flag = 0; printf("Enter a positive integer: "); scanf("%d", &n); for (i = 2; i <= n / 2; ++i) { // condition for non-prime if (n % i == 0) { flag = 1; break; } } if (n == 1) { printf("1 is neither prime nor composite."); } else { if (flag == 0) printf("%d is a prime number.", n); else printf("%d is not a prime number.", n); } return 0; }