0% found this document useful (0 votes)
13 views12 pages

C Programs for Mathematical Calculations

The document contains multiple C programming exercises covering various topics such as expression evaluation, arithmetic operations, prime number checks, palindrome checks, Fibonacci sequence generation, and quadratic equation roots. Each section includes a description of the task, the corresponding C code, and the expected output. The exercises are designed to enhance programming skills and understanding of fundamental concepts in C programming.

Uploaded by

n5355383charitha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views12 pages

C Programs for Mathematical Calculations

The document contains multiple C programming exercises covering various topics such as expression evaluation, arithmetic operations, prime number checks, palindrome checks, Fibonacci sequence generation, and quadratic equation roots. Each section includes a description of the task, the corresponding C code, and the expected output. The exercises are designed to enhance programming skills and understanding of fundamental concepts in C programming.

Uploaded by

n5355383charitha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

3.

Expression Evaluation

a. A building has 10 floors with a floor height of 3 meters each. A ball is dropped
from the top of the building. Find the time taken by the ball to reach each floor. (Use
the formula s = ut+(1/2)at^2 where u and a are the initial velocity in m/sec (= 0) and
acceleration in m/sec^2 (= 9.8 m/s^2)).

#include<stdio.h>
#include<math.h>
int main()
{
int s=30, u=0, r, t;
float a=9.8;
r = 2*s/a;
t = sqrt(r);
printf(“Time taken is=%d”, t);
}
Output :
b. Write a C program, which takes two integer operands and one operator from the
user, performs the operation and then prints the result. (Consider the operators +,-,*,
/, % and use Switch Statement)

#include <stdio.h>
void main()
{
int a, b, c;
char ch;
printf("Enter your operator +, -, /, *, % \n");
scanf("%c", &ch);
printf("Enter the values of a and b");
scanf("%d %d", &a, &b);
switch(ch)
{
case '+' : c = a + b;
printf("Addition of two numbers is= %d", c);
break;
case '-' : c = a - b;
printf("Subtraction of two numbers is =%d", c);
break;
case '*' : c = a * b;
printf("multiplication of two numbers is= %d", c);
break;
case '/' : c = a / b;
printf("Division of two numbers is= %d", c);
break;
case '%' : c = a % b;
printf("Remainder of two numbers is = %d", c);
break;
default : printf("Invalid operator");
break;
}
}
Output:
c. Write a program that finds if a given number is a prime number.

#include <stdio.h>

int main() {

int n, i, flag = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

// 0 and 1 are not prime numbers

// change flag to 1 for non-prime number

if (n == 0 || n == 1)

flag = 1;

for (i = 2; i <= n / 2; ++i)

// if n is divisible by i, then n is not prime

// change flag to 1 for non-prime number

if (n % i == 0) {

flag = 1;

break;

// flag is 0 for prime numbers

if (flag == 0)

printf("%d is a prime number.", n);


else

printf("%d is not a prime number.", n);

return 0;

Output:
d. Write a C program to find the sum of individual digits of a positive integer and
test given number is palindrome.

#include <stdio.h>
int main() {
int number, originalNumber, remainder, sum = 0, reversedNumber = 0;
// Input the positive integer
printf("Enter a positive integer: ");
scanf("%d", &number);
originalNumber = number; // Store original number for palindrome check
// Calculate the sum of digits and reverse the number
while (number != 0) {
remainder = number % 10; // Get the last digit
sum += remainder; // Add it to sum
reversedNumber = reversedNumber * 10 + remainder; // Build the reversed
number
number /= 10; // Remove the last digit
}
// Output the sum of the digits
printf("Sum of the digits: %d\n", sum);
// Check if the number is a palindrome
if (originalNumber == reversedNumber) {
printf("%d is a palindrome.\n", originalNumber);
}
else {
printf("%d is not a palindrome.\n", originalNumber);
}
return 0;
}
Output:

e. A Fibonacci sequence is defined as follows: the first and second terms in the
sequence are 0 and 1. Subsequent terms are found by adding the preceding two terms
in the sequence. Write a C program to generate the first n terms of the sequence.

#include<stdio.h>

int main()

int n1=0,n2=1,n3,i,number;

printf("Enter the number of elements:");

scanf("%d",&number);

printf("\n%d %d",n1,n2); //printing 0 and 1

for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are already printed

n3=n1+n2;

printf(" %d",n3);

n1=n2;

n2=n3;

return 0;

Output :
f. Write a C program to generate all the prime numbers between 1 and n, where n is
a value supplied by the user.

#include<stdio.h>

void main(){

int i, num, n, count;

printf("Enter the range: ");

scanf("%d", &n);

printf("The prime numbers in between the range 1 to %d:",n);

for(num = 1;num<=n;num++){

count = 0;

for(i=2;i<=num/2;i++){

if(num%i==0){

count++;

break;

if(count==0 && num!= 1)

printf("%d ",num);

Output:
g. Write a C program to find the roots of a Quadratic equation.

/* C Program to Find the Roots of the Quadratic Equation */

#include<stdio.h>

#include<math.h> // it is used for math calculation

#include<conio.h>

void main()

float x, y, z, det, root1, root2, real, img;

printf("\n Enter the value of coefficient x, y and z: \n ");

scanf("%f %f %f", &x, &y, &z);

// define the quadratic formula of the nature of the root

det = y * y - 4 * x * z;

// defines the conditions for real and different roots of the quadratic equation

if (det > 0)

root1 = (-y + sqrt(det)) / (2 * x);

root2 = (-y + sqrt(det)) / (2 * x);

printf("\n Value of root1 = %.2f and value of root2 = %.2f", root1, root2);

// elseif condition defines both roots ( real and equal root) are equal in the
quadratic equation

else if (det == 0)

{
root1 = root2 = -y / (2 * x); // both roots are equal;

printf("\n Value of root1 = %.2f and Value of root2 = %.2f", root1, root2);

// if det < 0, means both roots are real and imaginary in the quadratic equation.

else {

real = -y / (2 * x);

img = sqrt(-det) / (2 * x);

printf("\n value of root1 = %.2f + %.2fi and value of root2 = %.2f - %.2fi ",
real, img, real, img);

getch();

Output:

h. Write a C program to calculate the following, where x is a fractional value. 1-x/2


+x^2/4-x^3/6

#include<stdio.h>

#include<math.h>

int main() {

int i = 0, limit = 4;

double sum = 0, x = 0;

printf("enter the value of x");

scanf("%lf", &x);

for(i = 0; i <= limit; i++) {


sum += pow(-x, i);

printf("sum is %f\n", sum);

return 0;

Output:

i. Write a C program to read in two numbers, x and n, and then compute the sum of
this geometric progression: 1+x+x^2+x^3+………….+x^n. For example: if n is 3
and x is 5, then the program computes 1+5+25+125.

#include <stdio.h>

#include <math.h>

void main()

int n, x, i, sum = 0;
printf("Enter the limit");

scanf("%d", &n);

printf("Enter the value of x");

scanf("%d", &x);

if(x < 0 || n < 0)

printf("illegal value");

else

for (i = 0; i <= n; i++)

sum=sum + pow (x, i);

printf("sum=%d", sum);

Output:

You might also like