Simple Arithmetic Problem
simple arithmetic problems
Addition is one of the most basic arithmetic operations in C programming. To
add two integers, we use the +
operator.
Here's a simple program that takes two integers as input from the user and prints their sum:
#include <stdio.h>
void main() {
int a, b, sum;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
sum = a + b;
printf("Sum = %d", sum);
}
Explanation:
- int a, b, sum;
: Declares three integer variables
- scanf()
: Takes user input
- sum = a + b;
: Adds the two numbers
- printf()
: Displays the result
✖️ Multiplication of Two Integers in C
Multiplication in C is performed using the *
operator. This
program asks the user for two numbers and multiplies them.
#include <stdio.h>
void main() {
int a, b, product;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
product = a * b;
printf("Product = %d", product);
}
Explanation:
- int a, b, product;
: declares variables
- scanf()
: takes two integers as input
- product = a * b;
: multiplies them
- printf()
: prints the result
🔍 Check if a Number is Positive or Negative
In C language, we can determine whether a number is positive, negative, or
zero using conditional statements like if
, else if
, and else
.
#include <stdio.h>
void main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("The number is Positive.");
} else if (num < 0) {
printf("The number is Negative.");
} else {
printf("The number is Zero.");
}
}
Explanation:
- Input is taken using scanf()
.
- if
checks for positive,
- else if
checks for negative,
- else
handles the case when the number is zero.
🧮 Determine Even or Odd Number in C
Even numbers are exactly divisible by 2, while odd
numbers leave a remainder of 1 when divided by 2. In C, we use the modulus operator
%
to find the remainder.
#include <stdio.h>
void main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("The number is Even.");
} else {
printf("The number is Odd.");
}
}
Explanation:
- num % 2
checks the remainder when divided by 2
- If the result is 0, it’s even; otherwise, it’s odd
🔸 Find the Maximum of Two Numbers
To find the maximum of two numbers in C, we use a simple if-else
condition to compare them.
#include <stdio.h>
void main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a > b) {
printf("%d is greater.", a);
} else if (b > a) {
printf("%d is greater.", b);
} else {
printf("Both numbers are equal.");
}
}
Explanation: The program compares the two inputs and prints which one is larger or if they are equal.
🔺 Find the Maximum of Three Numbers in C
To find the maximum of three numbers in C language, we use a series of
if-else
conditions to compare each number.
#include <stdio.h>
void main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c) {
printf("%d is the greatest.", a);
} else if (b >= a && b >= c) {
printf("%d is the greatest.", b);
} else {
printf("%d is the greatest.", c);
}
}
Explanation:
- First, compare a
with both b
and c
- If not the greatest, check if b
is greater than or equal to both
- Otherwise, c
is the greatest
Sum of First N Natural Numbers
The sum of the first n natural numbers can be calculated using a loop or a
mathematical formula. In this C program, we use a for
loop to add all numbers from 1 to
n
.
#include <stdio.h>
void main() {
int n, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
sum += i;
}
printf("Sum of first %d natural numbers is %d", n, sum);
}
Explanation:
- We read the number n
from the user
- Use a loop to add numbers from 1
to n
- Display the result using printf()
➗ Integer Division in C
In C language, when you divide one integer by another using the
/
operator, the result is also an integer. Any decimal or fractional part is discarded.
For example:
-: 7 / 2 = 3 (not 3.5)
-: 10 / 3 = 3 (not 3.333)
#include <stdio.h>
void main() {
int dividend, divisor, quotient;
printf("Enter dividend: ");
scanf("%d", ÷nd);
printf("Enter divisor: ");
scanf("%d", &divisor);
if (divisor != 0) {
quotient = dividend / divisor;
printf("Quotient = %d", quotient);
} else {
printf("Division by zero is not allowed.");
}
}
Note: Always check that the divisor is not zero
to avoid runtime
errors.
🔁 Digit Reversing in C
In digit reversing, we take an integer like 1234
and reverse its
digits to get 4321
. We do this by extracting digits using modulus and rebuilding the number in
reverse.
#include <stdio.h>
void main() {
int num, reversed = 0, digit;
printf("Enter an integer: ");
scanf("%d", &num);
while (num != 0) {
digit = num % 10;
reversed = reversed * 10 + digit;
num = num / 10;
}
printf("Reversed number = %d", reversed);
}
Explanation:
- % 10
gives the last digit
- * 10 + digit
appends it in reverse order
- / 10
removes the last digit from the original number
📋 Table Generation for a Number in C
A multiplication table is a list of multiples of a number. In C, we can
generate it using a for
loop from 1 to 10.
#include <stdio.h>
void main() {
int n;
printf("Enter a number to generate its table: ");
scanf("%d", &n);
printf("Multiplication Table of %d:\n", n);
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", n, i, n * i);
}
}
Explanation:
- User enters a number n
- The loop runs from 1 to 10
- Each line displays n × i = result
🔢 Power Calculation (ab) in C
To calculate a raised to the power b in C, we multiply a
by
itself b
times using a loop.
#include <stdio.h>
void main() {
int a, b;
long long result = 1;
printf("Enter base (a): ");
scanf("%d", &a);
printf("Enter exponent (b): ");
scanf("%d", &b);
for (int i = 1; i <= b; i++) {
result *= a;
}
printf("%d raised to the power %d is %lld", a, b, result);
}
Explanation:
- User inputs base a
and exponent b
- A loop runs b
times and multiplies result
by a
- The result is printed using %lld
for long long type
🧮 Factorial Program in C
The factorial of a number n
is the product of all positive
integers less than or equal to n
. It is represented as n!
.
#include <stdio.h>
void main() {
int n;
long long fact = 1;
printf("Enter a non-negative integer: ");
scanf("%d", &n);
if (n < 0) {
printf("Factorial is not defined for negative numbers.");
} else {
for (int i = 1; i <= n; i++) {
fact *= i;
}
printf("Factorial of %d = %lld", n, fact);
}
}
Explanation:
- Input number n
is read
- If n
is negative, factorial is not calculated
- Otherwise, use loop to multiply numbers from 1 to n
- Output is shown using %lld
for large values
Sine Series in C
The Sine series expansion is:
sin(x) = x - x³/3! + x⁵/5! - x⁷/7! + ...
#include <stdio.h>
#include <math.h>
void main() {
float x, term, sum;
int i, n;
int sign = 1;
int power;
printf("Enter the angle in degrees: ");
scanf("%f", &x);
x = x * M_PI / 180;
printf("Enter number of terms: ");
scanf("%d", &n);
sum = 0;
for (i = 1; i <= n; i++) {
power = 2 * i - 1;
term = sign * pow(x, power) / tgamma(power + 1);
sum += term;
sign = -sign;
}
printf("sin(x) = %.6f\\n", sum);
}
Cosine Series in C
The Cosine series expansion is:
cos(x) = 1 - x²/2! + x⁴/4! - x⁶/6! + ...
#include <stdio.h>
#include <math.h>
void main() {
float x, term, sum;
int i, n;
int sign = 1;
int power;
printf("Enter the angle in degrees: ");
scanf("%f", &x);
x = x * M_PI / 180;
printf("Enter number of terms: ");
scanf("%d", &n);
sum = 0;
for (i = 0; i < n; i++) {
power = 2 * i;
term = sign * pow(x, power) / tgamma(power + 1);
sum += term;
sign = -sign;
}
printf("cos(x) = %.6f\\n", sum);
}
Combination (nCr) Program in C
nCr represents the number of ways to choose r
elements from a
group of n
. It is calculated using the formula:
nCr = n! / (r! * (n - r)!)
#include <stdio.h>
long long factorial(int n) {
long long fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
void main() {
int n, r;
long long nCr;
printf("Enter value of n: ");
scanf("%d", &n);
printf("Enter value of r: ");
scanf("%d", &r);
if (r > n || n < 0 || r < 0) {
printf("Invalid input: r must be ≤ n and both non-negative.");
} else {
nCr = factorial(n) / (factorial(r) * factorial(n - r));
printf("nCr = %lld", nCr);
}
}
Explanation:
- A user-defined factorial()
function is used
- Checks ensure valid input values for n
and r
- Result is printed using %lld
to support large values
🔺 Pascal’s Triangle in C
Pascal’s Triangle is a triangle of numbers in which each number is the sum of the two directly above it.
#include <stdio.h>
long long factorial(int n) {
long long fact = 1;
for (int i = 1; i <= n; i++)
fact *= i;
return fact;
}
long long nCr(int n, int r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}
void main() {
int rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
for (int i = 0; i < rows; i++) {
for (int space = 0; space < rows - i - 1; space++)
printf(" ");
for (int j = 0; j <= i; j++)
printf("%4lld", nCr(i, j));
printf("\n");
}
}
🔢 Prime Number Check in C
A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers.
#include <stdio.h>
void main() {
int n, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &n);
if (n <= 1) {
isPrime = 0;
} else {
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
isPrime = 0;
break;
}
}
}
if (isPrime)
printf("%d is a prime number.\\n", n);
else
printf("%d is not a prime number.\\n", n);
}
🎯 Perfect Number in C
A Perfect Number is a positive number equal to the sum of all its proper
divisors (excluding itself).
Examples: 6, 28, 496
#include <stdio.h>
void main() {
int num, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = 1; i < num; i++) {
if (num % i == 0)
sum += i;
}
if (sum == num)
printf("%d is a Perfect Number.\\n", num);
else
printf("%d is not a Perfect Number.\\n", num);
}
Explanation:
- Use a loop to find all divisors of num
- Sum them up
- If sum == num → it's a perfect number
🔗 GCD (Greatest Common Divisor) in C
The GCD of two integers is the largest number that divides both of them without leaving a remainder. It is also known as HCF (Highest Common Factor).
Example: GCD of 24 and 36 is 12.
#include <stdio.h>
void main() {
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
while (a != b) {
if (a > b)
a = a - b;
else
b = b - a;
}
printf("GCD = %d", a);
}
Explanation:
- Subtract smaller number from larger until both become equal.
- That common value is the GCD.
🔄 Swapping
The temporary variable method uses an extra variable to hold a value during swap.
#include <stdio.h>
void main() {
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d", a, b);
return 0;
}
Hey buddy ! all codes you can copy and run in your favorite compilers
Comments
Post a Comment
write your complements and complaints :)