Solutions Cse
Solutions Cse
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:
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:
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:
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:
FLOWCHART: