0% found this document useful (0 votes)
14 views11 pages

Solutions Cse

The document contains 5 code snippets and algorithms to solve various problems in C programming: 1) A program to find the sum of digits of a positive integer. 2) A program to generate the first n terms of the Fibonacci sequence. 3) A program to generate all prime numbers between 1 and n. 4) A program to find the roots of a quadratic equation. 5) A program to calculate the sum of an alternating series.

Uploaded by

Jeswanth Selva
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)
14 views11 pages

Solutions Cse

The document contains 5 code snippets and algorithms to solve various problems in C programming: 1) A program to find the sum of digits of a positive integer. 2) A program to generate the first n terms of the Fibonacci sequence. 3) A program to generate all prime numbers between 1 and n. 4) A program to find the roots of a quadratic equation. 5) A program to calculate the sum of an alternating series.

Uploaded by

Jeswanth Selva
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
You are on page 1/ 11

a) Write a C program to find the sum of individual digits of a positive

integer.

CODE:
#include <stdio.h>

int main()
{
int number, digit, sum=0;
printf("Enter the number: ");
scanf("%d",&number);
while (number!=0)
{
digit = number % 10;
number /= 10;
sum += digit;
}
printf("The sum of the digits of the given number = %d",sum);
return 0;
}

ALGORITHM:

1.take the number as


input 2.while(a!=0)
3.d=a%10;
4.a=a/10;
5.sum=sum+d;
6.print the sum of the
digits
FLOWCHART:
b) 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.

CODE:
#include <stdio.h>
int main()
{
int i,n;
int t1 = 0, t2 = 1;
int nextTerm = t1+t2;
printf("Enter the number of terms in the series: ");
scanf("%d",&n);
printf("%d, %d ,",t1,t2);
for (i = 3; i <= n; i++)
{
printf("%d",nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}

ALGORITHM:

1.take the value of n as input


2.print the first term of the
series 3.run the loop from 2 to
n 4.c=a+b
5.a=b
6.b=c
7.print c
FLOWCHART:
c) Write a C program to generate all the prime numbers between 1 and n, where n
is a value supplied by the user.

CODE:

#include <stdio.h>

int main()
{
int n,i,j;
int flag;
printf("Enter the number of terms: ");
scanf("%d",&n);
for(i=2;i<=n;i++)
{
flag=0;
for(j=2;j<i;j++)
{
if (i%j==0)
flag = 1;
}
if (flag==0)
printf("%d ",i);
}
return 0;
}

ALGORITHM:

1.take the value of n as input


2.run the first loop i from 2 to n
3.run the nested loop j from 2 to i
4.check for the factors of i by using the j loop
5.if the number i has 0 factors apart from itself and 1,then print i as one of the prime
numbers
FLOWCHART:

CONTINUE PRINT i

END
d) Write a C program to find the roots of a quadratic

equation.

CODE:

#include <stdio.h>
#include <math.h>

int main()
{
int a,b,c;
int d,D;
int x1,x2;
printf("Enter the of coefficient of x^2: ");
scanf("%d",a);
printf("Enter the vale of coefficient of x: ");
scanf("%d",b);
printf("Enter the value of constant: ");
scanf("%d",c);
d = (b*b)-(4*a*c);
D = pow(d,0.5);
x1 = ((-b)-D)/(2*a);
x2 = ((-b)+D)/(2*a);
if (d==0)
{
printf("D = 0, roots are real and equal (coincident). \n");
printf("The roots are %d and %d",x1,x2);
}
else if (d>0)
{
printf("D > 0, roots are real and distinct (unequal). \n");
printf("The roots are %d and %d",x1,x2);
}
else
{
printf("D < 0, roots are imaginary and unequal. \n");
}
return 0;
}
ALGORITHM:

1.take the values of the coefficients of x^2,x and the constant c as inputs
find the value of pow(b,2)-4*a*c and store it in n
2.find the roots of the equation by substituting in the appropriate
equation 3.if the value of n is 0 print roots are real and equal
4.if the value of n is greater than 0 print roots are real and distinct
5.if the value of n is lesser than 0 print roots are imaginary and distinct
FLOWCHART:

END
e) Write a C program to calculate the following
Sum Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10!

CODE:

#include <stdio.h>
#include <math.h>

int main()
{
int x;
int sum = 1;
int f,i,j;
int sign = -1;
printf("Enter the value: ");
scanf("%d",&x);
for(i=2;i<=10;i+=2)
{
f=1;
for(j=1;j<=i;j++)
{
f = f*j;
}
sum += (sign*pow(x,i))/f;
sign = sign*-1;
}
printf("The sum of the series = %d",sum);
return 0;
}
ALGORITHM:

1.take the value of x as the input


2.construct a nested loop
3.run the i loop from 2 to 10 with a stepper of 2
4.run the j loop to find the factorial of i
5.calculate the sum in the i loop
6.print sum

FLOWCHART:

You might also like