1. Write a C program to find the roots of a quadratic equation ax2+bx+c=0
#include <stdio.h>
#include <stdlib.h>
int main()
{
float a,b,c,x1,x2,d;
printf("Enter the coeffients for a,b,c\n");
scanf("%f%f%f", &a,&b,&c);
if(a*b*c==0)
{
printf("\nRoots cant be find\n");
exit(0);
}
d=b*b-4*a*c;
if(d==0)
{
x1=x2= -b/(2*a);
printf("\nThe roots are real and equal\n");
printf("\nx1=%f\n x2=%f\n", x1,x2);
}
else if(d>0)
{
x1= (-b + sqrt(d))/(2*a);
x2= (-b - sqrt(d))/(2*a);
printf("\nThe roots are real and distinct\n"); printf(" \nx1=%f\n x2=%f\n", x1,x2);
}
else
{
x1= -b/(2*a);
x2=sqrt(fabs(d))/ (2*a);
printf("\nThe roots are real and imaginary\n"); printf("\n(x1+ix2)= %f+i%f\n",x1,x2); printf("\n(x1-ix2)= %f-i%f\n", x1,x2);
}
return 0;
}2. Write a C program to find the sum of all the digits and occurrence of a digit in the number.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, digit, rem,sum=0,temp, count=0; printf("Enter the Number\n"); scanf("%d",&num);
printf("Enter the digit to be find in the Number\n");
scanf("%d", &digit);
temp=num;
while(num!=0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
if(rem==digit)
count++;
}
printf("The Sum of all the digits of %d is %d\n", temp,sum); printf("The digit %d is occurred for %d times\n", digit, count); return 0;
}3. Write a C program to find the GCD and LCM of given two numbers using
Euclid’s method.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int m,n,rem,gcd,lcm,temp1,temp2;
printf("Enter the value of m and n ");
scanf("%d%d",&m,&n);
temp1=m;
temp2=n;
while(n!=0)
{
rem=m%n;
m=n;
n=rem;
}
gcd=m;
lcm=(temp1*temp2)/gcd;
printf("the gcd %d and %d is %d \n",temp1,temp2,gcd);
printf("the lcm %d and %d is %d \n",temp1,temp2,lcm);
return 0;
}4. Write a C program to print the prime numbers in a given range.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int sr,er,flag,i,j, c=0;
printf("enter the starting and ending range\n"); scanf("%d%d",&sr,&er); printf("prime numbers are\n");
for(i=sr;i<=er;i++)
{
flag=0;
for(j=2;j<=(i/2);j++)
{
if(i%j==0)
{
flag=1;
break;
} }
if(flag==0)
{
c++;
printf("%d\t",i);
}
}
if(c==0)
printf("\n\n NULL\n\nThere is no prime number with in the given range\n"); else
printf("\n\nThere are %d prime numbers within the given range\n", c); return 0;
}
5.Write a C program to find if a given string is a palindrome or not.
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char str1[100],str2[100];
int i,n;
printf("Enter the string1\n");
gets(str1);
n=strlen(str1);
for(i=0;i<n;i++)
str2[n-i-1]=str1[i];
str2[i]='\0';
if(strcmp(str1,str2)==0)
printf("The given string is a palindrome\n"); else
printf("The given message is not a palindrome \n"); return 0;
}6. Write a C program to input N real numbers in 1-D array. Compute mean, variance and Standard Deviation. Mean= sum/N, Variance = Σ (Xi-mean)2/N, STD Deviation= √variance.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float a[10],sum=0,sumv=0,mean,var,std;
int i,n;
printf("Enter the number\n");
scanf("%d",&n);
printf("Enter the numbers\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
sum=sum+a[i];
}
mean=sum/n;
for(i=0;i<n;i++)
sumv=sumv+pow(a[i]-mean,2); //sumv=sumv+(a[i]-mean)*(a[i]-mean);
printf("sumv=%f\n",sumv);
var=sumv/n;
std=sqrt(var);
printf("\nthe mean= %f\n the variance=%f\n standard deviation=%f\n", mean,var,std);
return 0;
}7. Write a C program to read N integers into an array A and find the sum of elements using pointers
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[100],n,I,sum=0;
int *p;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter the numbers\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
p=a;
for(i=0;i<n;i++)
{
sum=sum+*p;
p=p+1;
}
printf("\nThe sum is %d\n", sum); return 0;
}