You are on page 1of 8

Assignment - 3

Q1. WAP to practice math functions such as sin(), cos(), log(), pow(), sqrt() etc. by including header
file.

Solution:

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

int main()
{
float angle,lnum,num,pnum,snum;

printf("Enter angle in radians: ");


scanf("%f",&angle);

printf("Enter any number(greater than 0) to demonstrate log function: ");


scanf("%f",&lnum);

printf("Enter any number whose power is to be calculated");


scanf("%f",&num);

printf("To what power do you want to raise the above number? ");
scanf("%f",&pnum);

printf("Enter any number whose square root is to be calculated: ");


scanf("%f",&snum);

printf("sin(%f) = %f\n",angle,sin(angle));
printf("cos(%f) = %f\n",angle,cos(angle));

if (lnum>0)
{
printf("log(%f) = %f\n",lnum,log10(lnum));
}

else
{
printf("Number is less than zero\n");
}

printf("%f raised to the power of %f = %f\n",num,pnum,pow(num,pnum));

printf("Square root of %f = %f\n",snum,sqrt(snum));


return 0;
}
Output:

Q2. WAP to find roots of a quadratic equation (for D>=0 case).

Solution:

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

int main()
{
int a,b,c,d,r1,r2;

printf("For any equation of the form ax^2 + bx + c = 0 whose roots are to be found:\n");
printf("Enter the value of a: ");
scanf("%d",&a);
printf("Enter the value of b: ");
scanf("%d",&b);
printf("Enter the value of c: ");
scanf("%d",&c);

d = (b*b) - (4*a*c);

r1 = ((-b) + sqrt(d))/2*a;
r2 = ((-b) - sqrt(d))/2*a;

printf("The two roots of the given quadratic are %d and %d",r1,r2);

return 0;
}

Output:
Q3. WAP to format console output using '\n', '\t', '\b',’\’’,’\\’,’/””,’\r’,’\a’ within printf statement.

Solution:

#include <stdio.h>

int main()
{

printf("Hello\nWorld\n");
printf("Hello\tWorld\n");
printf("Hello\bWorld\n");
printf("Hello\"world\n");
printf("Hello\\World\n");
printf("Hello/""World\n");
printf("Hello\rWorld");
printf("\a");

return 0;
}

Output:
Q4. WAP to implement assignment operators such as += , -= , *=, /= %= etc.

Solution:

#include <stdio.h>

int main()
{
int a;

printf("Enter any number");


scanf("%d",&a);

printf("%d\n",a);
a+=1;
printf("%d\n",a);
a-=1;
printf("%d\n",a);
a*=3;
printf("%d\n",a);
a/=2;
printf("%d\n",a);
a%=2;
printf("%d\n",a);

return 0;
}

Output:
Q5. WAP to shift left and shift right operators (>> and <<).Ask the application of this operator to your
lab instructor.

Solution:

#include<stdio.h>

int main()
{
int a,b;

printf("Enter any number:");


scanf("%d",&a);

printf("Enter any number:");


scanf("%d",&b);

printf("%d<<2 = %d\n",a,a<<2);
printf("%d>>2 = %d",b,b>>2);

return 0;
}

Output:
Q6. WAP to utilize ternary operator (?:). (For example: To find whether the given number is odd or
even)

Solution:

#include<stdio.h>

int main()
{
int num;

printf("Enter any number: ");


scanf("%d",&num);

(num%2 == 0) ? printf("The inputted number is even")


: printf("The inputted number is odd");

return 0;
}

Output:

You might also like