You are on page 1of 7

Assignment 2

1)Write a program to find quadratic equation. (Use If


Else).

#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c,d,e;
float root1,root2,discriminant;
printf("Enter coefficient of x^2:");
scanf("%d",&a);
printf("Enter coefficient of x:");
scanf("%d",&b);
printf("Enter the constant:");
scanf("%d",&c);
discriminant=(b*b-4*a*c);
root1=(-b-sqrt(b*b-4*a*c))/(2*a);
root2=(-b+sqrt(b*b-4*a*c))/(2*a);
if(discriminant<0)
{
printf("\n The roots are imaginary.");
}else if(discriminant>=0)
{
printf("\n The roots are real.");
}
printf("\n the value of first root is=%f",root1);
printf("\n the value of second root is=%f",root2);
return 0;
}
Output:
Enter coefficient of x^2:1
Enter coefficient of x:5
Enter the constant:6

The roots are real.


the value of first root is=-3.000000
the value of second root is=-2.000000

2) Write a program to find maximum between three


numbers (Use If Else).

#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter first no:");
scanf("%d",&a);
printf("Enter second no:");
scanf("%d",&b);
printf("Enter third no:");
scanf("%d",&c);
if(a>b)
{
if(a>c)
{
printf("a is the greatest.");
}
}else if(b>c)
{
if(b>a)
{
printf("b is the largest.");
}
} else if(c>a)
{
if(c>b)
{
printf("c is the largest.");
}
}
return 0;
}
Output:
Enter first no:10
Enter second no:20
Enter third no:30
c is the largest.
3) Write a program to display the multiplication table of a
given integer (Use Loop).

#include<stdio.h>
int main()
{
int n,i,count,n2;
printf("Enter number whose table is to be found:");
scanf("%d",&n);
printf("Enter number upto which table should be found:");
scanf("%d",&n2);
while(i<=n2)
{
count=n*i;
printf("\n %d * %d = %d ",n,i,count);
i++;
}
return 0;
}

Output:
Enter number whose table is to be found:2
Enter number upto which table should be found:10

2*0=0
2*1=2
2*2=4
2*3=6
2*4=8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20

4) Write a program to check if a number is palindrome or


not. (Use Loop).

#include<stdio.h>
int main()
{
int num,reverse=0,rem,i,j;
printf("Enter the number:");
scanf("%d",&num);
i=num;
while(num>0)
{
rem=num%10;
reverse=reverse*10+rem;
num=num/10;
}
printf("Reverse number=%d",reverse);
;
if (i==reverse)
{
printf("\n Number is a palindrome.");
}else
{
printf("\n Number is not palindrome.");
}
return 0;
}
Output:
Enter the number:121
Reverse number=121
Number is a palindrome.

5) Write a program to find number is prime or not. (Use


loop)

#include <stdio.h>
int main()
{
int i, num, p = 0;
printf("Please enter a number: \n");
scanf("%d", &num);
for(i=1; i<=num; i++)
{
if(num%i==0)
{
p++;
}
}
if(p==2)
{
printf("Entered number is %d and it is a prime number.",num);
}
else
{
printf("Entered number is %d and it is not a prime number.",num);
}
}
Output:
Please enter a number:
13
Entered number is 13 and it is a prime number.

Please enter a number:


4
Entered number is 4 and it is not a prime number.

You might also like