You are here: Home / Topics / Unit-5: Simple arithematic problems addition :BCA semester 1

# Unit-5: Simple arithematic problems addition :BCA semester 1

Filed under: BCA Study Material

```Here’s an example of adding two numbers in C:#include<studio.h>int main() {int a, b, sum;printf("Enter two numbers: ");scanf("%d%d", &a, &b);sum = a + b;printf("The sum of %d and %d is: %d\n", a, b, sum);return 0;This program first takes two numbers as input from the user using the scanf() function. Then, it calculates their sum and stores it in the variable sum. Finally, it prints the result using the printf() functionMultiplication of integersHere’s an example of multiplying two integers in C:    #include<studio.h>int main() {int a, b, product;12345678printf("Enter two integers: ");scanf("%d%d", &a, &b);product = a * b;printf("The product of %d and %d is: %d\n", a, b, product);return 0;Determining if a number is +ve / -veHere’s an example of determining if a number is positive or negative in C:#include<studio.h>int main() {int num;12345678910printf("Enter a number: ");scanf("%d", &num);if (num >= 0) {    printf("%d is a positive number.\n", num);} else {    printf("%d is a negative number.\n", num);}return 0;This program takes a number as input from the user using the scanf() function. Then, it uses an if-else statement to determine whether the number is positive or negative. If the number is greater than or equal to 0, it is considered positive. Otherwise, it is considered negative. The result is printed using the printf() function.determining if a number is even or oddHere’s an example of determining if a number is even or odd in C:#include<studio.h>int main() {int num;12345678910printf("Enter a number: ");scanf("%d", &num);if (num % 2 == 0) {    printf("%d is an even number.\n", num);} else {    printf("%d is an odd number.\n", num);}return 0;}This program takes a number as input from the user using the scanf() function. Then, it uses the modulo operator % to check if the number is divisible by 2. If the remainder is 0, the number is considered even. Otherwise, it is considered odd. The result is printed using the printf() functionMaximum of 2 numbers, 3 numbersHere’s an example of finding the maximum of two numbers in C:#include <stdio.h>

int main() {
int num1, num2, max;

printf("Enter two numbers: ");
scanf("%d%d", &num1, &num2);

max = (num1 > num2) ? num1 : num2;

printf("The maximum of %d and %d is: %d\n", num1, num2, max);

return 0;
}This program takes two numbers as input from the user using the scanf() function. Then, it uses a ternary operator ?: to determine the maximum of the two numbers. The result is stored in the variable max and is printed using the printf() function.Here’s an example of finding the maximum of three numbers in C:Here’s an example of finding the maximum of two numbers in C:#include <stdio.h>

int main() {
int num1, num2, max;

printf("Enter two numbers: ");
scanf("%d%d", &num1, &num2);

max = (num1 > num2) ? num1 : num2;

printf("The maximum of %d and %d is: %d\n", num1, num2, max);

return 0;
}
This program takes two numbers as input from the user using the scanf() function. Then, it uses a ternary operator ?: to determine the maximum of the two numbers. The result is stored in the variable max and is printed using the printf() function.Here’s an example of finding the maximum of three numbers in C:#include <stdio.h>

int main() {
int num1, num2, num3, max;

printf("Enter three numbers: ");
scanf("%d%d%d", &num1, &num2, &num3);

max = num1;
if (num2 > max) max = num2;
if (num3 > max) max = num3;

printf("The maximum of %d, %d and %d is: %d\n", num1, num2, num3, max);

return 0;
}This program takes three numbers as input from the user using the scanf() function. Then, it uses an if statement to determine the maximum of the three numbers. The result is stored in the variable max and is printed using the printf() function.This program takes three numbers as input from the user using the scanf() function. Then, it uses an if statement to determine the maximum of the three numbers. The result is stored in the variable max and is printed using the printf() function.Sum of first n numbersHere’s an example of finding the sum of the first n numbers in C:#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("The sum of the first %d numbers is: %d\n", n, sum);

return 0;
}This program takes a positive integer n as input from the user using the scanf() function. Then, it uses a for loop to calculate the sum of the first n numbers. The result is stored in the variable sum and is printed using the printf() functionsum of given n numbersHere’s an example of finding the sum of n numbers in C:#include <stdio.h>

int main() {
int n, i, num, sum = 0;

printf("Enter the number of numbers: ");
scanf("%d", &n);

for (i = 1; i <= n; i++) {
printf("Enter number %d: ", i);
scanf("%d", &num);
sum += num;
}

printf("The sum of the %d numbers is: %d\n", n, sum);

return 0;
}This program takes the number of numbers n as input from the user using the scanf() function. Then, it uses a for loop to take n numbers as input from the user and calculates the sum of the numbers. The result is stored in the variable sum and is printed using the printf() function.Integer divisionHere’s an example of integer division in C:#include <stdio.h>

int main() {
int num1, num2, result;

printf("Enter two numbers: ");
scanf("%d%d", &num1, &num2);

result = num1 / num2;

printf("The result of %d / %d is: %d\n", num1, num2, result);

return 0;
}This program takes two numbers as input from the user using the scanf() function. Then, it performs integer division of the two numbers and stores the result in the variable result. The result is printed using the printf() function.Note that integer division in C discards the fractional part and returns the largest integer that is less than or equal to the exact mathematical result. For example, 7 / 2 would give 3 as the result, not 3.5digit reversalDigit reversing, Table generation for n jn c with exampleHere’s an example of digit reversal in C:#include <stdio.h>

int main() {
int num, reversed = 0, remainder;

printf("Enter an integer: ");
scanf("%d", &num);

while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}

printf("The reversed number is: %d\n", reversed);

return 0;
}This program takes an integer as input from the user using the scanf() function. Then, it uses a while loop to reverse the digits of the number. In each iteration, the last digit of the number is obtained using the modulo operator (%) and is appended to the reversed number. The number is then divided by 10 to remove the last digit. The reversed number is stored in the variable reversed and is printed using the printf() function.Table generation for nHere’s an example of generating a multiplication table for a given number n in C:#include <stdio.h>

int main() {
int n, i;

printf("Enter a positive integer: ");
scanf("%d", &n);

for (i = 1; i <= 10; i++) {
printf("%d * %d = %d\n", n, i, n * i);
}

return 0;
}This program takes a positive integer n as input from the user using the scanf() function. Then, it uses a for loop to generate and print the multiplication table for n for the first 10 numbers. The result of each multiplication is calculated using n * i and is printed using the printf() functionfactorial of nHere’s an example of finding the factorial of a number in C:#include <stdio.h>

int main() {
int num, i, factorial = 1;

printf("Enter an integer: ");
scanf("%d", &num);

for (i = 1; i <= num; i++) {
factorial *= i;
}

printf("The factorial of %d is: %d\n", num, factorial);

return 0;
}This program takes an integer as input from the user using the scanf() function. Then, it uses a for loop to calculate the factorial of the number. The loop starts from 1 and continues till the number itself. In each iteration, the value of factorial is updated by multiplying it with the current value of i. The final value of factorial is the factorial of the number. The result is printed using the printf() function.sine seriesHere’s an example of finding the sine series representation of an angle in C:#include <math.h>
#include <stdio.h>

int main() {
double angle, radian, sum = 0, term;
int i, n, sign;

printf("Enter the angle in degrees: ");
scanf("%lf", &angle);

// Convert the angle from degrees to radians
radian = angle * M_PI / 180.0;

printf("Enter the number of terms: ");
scanf("%d", &n);

sign = 1;
for (i = 0; i < n; i++) {
term = sign * pow(radian, 2 * i + 1) / (2 * i + 1);
sum += term;
sign = -sign;
}

printf("The sine of %lf degrees is approximately %lf\n", angle, sum);

return 0;
}This program takes an angle in degrees as input from the user using the scanf() function. It then converts the angle to radians using the formula radian = angle * M_PI / 180.0. The number of terms in the sine series representation is also taken as input from the user.The for loop is used to calculate the sum of the sine series representation of the angle. In each iteration, the value of the term is calculated using the formula term = sign * pow(radian, 2 * i + 1) / (2 * i + 1) where sign is either 1 or -1 and is changed in each iteration to alternate between positive and negative terms. The value of term is added to the running sum in each iteration.The final value of sum is the sine series representation of the angle. The result is printed using the printf() function.cosine seriesHere’s an example of finding the cosine series representation of an angle in C:#include <math.h>
#include <stdio.h>

int main() {
double angle, radian, sum = 0, term;
int i, n;

printf("Enter the angle in degrees: ");
scanf("%lf", &angle);

// Convert the angle from degrees to radians
radian = angle * M_PI / 180.0;

printf("Enter the number of terms: ");
scanf("%d", &n);

for (i = 0; i < n; i++) {
term = pow(-1, i) * pow(radian, 2 * i) / (2 * i)!;
sum += term;
}

printf("The cosine of %lf degrees is approximately %lf\n", angle, sum);

return 0;
}This program takes an angle in degrees as input from the user using the scanf() function. It then converts the angle to radians using the formula radian = angle * M_PI / 180.0. The number of terms in the cosine series representation is also taken as input from the user.The for loop is used to calculate the sum of the cosine series representation of the angle. In each iteration, the value of the term is calculated using the formula term = pow(-1, i) * pow(radian, 2 * i) / (2 * i)! where (2 * i)! is the factorial of 2 * i. The value of term is added to the running sum in each iteration.The final value of sum is the cosine series representation of the angle. The result is printed using the printf() function.Here’s an example of finding the number of combinations (nCr) in C:#include <stdio.h>int factorial(int n) {
int i, result = 1;
for (i = 1; i <= n; i++) {
result *= i;
}
return result;
}

int nCr(int n, int r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}

int main() {
int n, r, result;

printf("Enter the values of n and r: ");
scanf("%d%d", &n, &r);

result = nCr(n, r);

printf("%dC%d = %d\n", n, r, result);

return 0;
}
This program takes the values of n and r as input from the user using the scanf() function. The function factorial(int n) calculates the factorial of n. The function nCr(int n, int r) calculates the number of combinations nCr using the formula nCr = n! / (r! * (n - r)!).The final value of result is the number of combinations. The result is printed using the printf() function.pascal triangleHere’s an example of printing Pascal’s Triangle in C:#include <stdio.h>

int factorial(int n) {
int i, result = 1;
for (i = 1; i <= n; i++) {
result *= i;
}
return result;
}

int nCr(int n, int r) {
return factorial(n) / (factorial(r) * factorial(n - r));
}

int main() {
int i, j, rows, value;

printf("Enter the number of rows: ");
scanf("%d", &rows);

for (i = 0; i < rows; i++) {
for (j = 0; j <= i; j++) {
value = nCr(i, j);
printf("%d ", value);
}
printf("\n");
}

return 0;
}
This program takes the number of rows as input from the user using the scanf() function. The function factorial(int n) calculates the factorial of n, and the function nCr(int n, int r) calculates the number of combinations nCr using the formula nCr = n! / (r! * (n - r)!).The for loop is used to print the Pascal’s Triangle. In each iteration of the outer for loop, a new row is printed. In each iteration of the inner for loop, the value of nCr(i, j) is calculated and printed, where i and j are the row number and the column number, respectively. The final output is a triangular pattern of numbers representing Pascal’s Triangle.Here’s an example of finding the prime numbers and factors of a number in C:#include <stdio.h>

int is_prime(int num) {
int i;
if (num <= 1) {
return 0;
}
for (i = 2; i < num; i++) {
if (num % i == 0) {
return 0;
}
}
return 1;
}

void find_factors(int num) {
int i;
printf("Factors of %d: ", num);
for (i = 1; i <= num; i++) {
if (num % i == 0) {
printf("%d ", i);
}
}
printf("\n");
}

int main() {
int num;

printf("Enter a number: ");
scanf("%d", &num);

if (is_prime(num)) {
printf("%d is a prime number\n", num);
} else {
printf("%d is not a prime number\n", num);
find_factors(num);
}

return 0;
}This program takes a number as input from the user using the scanf() function. The function is_prime(int num) checks if the number is prime or not. If the number is less than or equal to 1, it returns 0, which indicates that the number is not prime. If the number is greater than 1, the program checks if it is divisible by any number other than 1 and itself. If it is divisible, the function returns 0, which indicates that the number is not prime. Otherwise, it returns 1, which indicates that the number is prime.The function find_factors(int num) prints the factors of the number by checking if the number is divisible by each number from 1 to itself. If it is divisible, the divisor is printed.The final output is either that the number is a prime number or it is not a prime number along with its factors.perfect numberHere is an example of code in C that determines if a given number is a perfect number:#include <stdio.h>

int main()
{
int num, i, sum = 0;

printf("Enter a positive integer: ");
scanf("%d", &num);

for (i = 1; i < num; i++)
{
if (num % i == 0)
{
sum += i;
}
}

if (sum == num)
printf("%d is a perfect number", num);
else
printf("%d is not a perfect number", num);

return 0;
}
A perfect number is a positive integer that is equal to the sum of its positive divisors excluding itself. In this code, the sum of the positive divisors is calculated in a for loop and compared to the input number to determine if it is a perfect number or not.GCD numbersHere is an example of code in C that determines if a given number is a perfect number:#include <stdio.h>

int main()
{
int num, i, sum = 0;

printf("Enter a positive integer: ");
scanf("%d", &num);

for (i = 1; i < num; i++)
{
if (num % i == 0)
{
sum += i;
}
}

if (sum == num)
printf("%d is a perfect number", num);  else
printf("%d is not a perfect number", num);

return 0;
}
A perfect number is a positive integer that is equal to the sum of its positive divisors excluding itself. In this code, the sum of the positive divisors is calculated in a for loop and compared to the input number to determine if it is a perfect number or not.swappingSwapping in a C program means exchanging the values of two variables. Here’s an example:#include <stdio.h>

void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int x = 10, y = 20;
printf("Before swapping: x = %d, y = %d\n", x, y);

swap(&x, &y);

printf("After swapping: x = %d, y = %d\n", x, y);
return 0;
}
Output:Before swapping: x = 10, y = 20
After swapping: x = 20, y = 10In this example, the swap function takes two pointers to integers as input and swaps their values using a temporary variable. The main function initializes two variables x and y, prints their values, calls the swap function, and finally prints their values again to show that they have been swapped.Here’s a simple algorithm for swapping two values:Declare a temporary variable temp.Assign the value of the first variable to temp.Assign the value of the second variable to the first variable.Assign the value of temp to the second variable.Here’s a flowchart for the swapping algorithm:          +-----------------+
|   temp = A      |
+-----------------+
|
+-----------------+
|   A = B         |
+-----------------+
|
+-----------------+
|   B = temp      |
+-----------------+
Note that this algorithm can be applied to any two values, not just variables. In the flowchart, A and B represent the two values being swapped.```