Lab Assignment - 3
Lab 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("To what power do you want to raise the above number? ");
scanf("%f",&pnum);
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");
}
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;
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("%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("%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;
return 0;
}
Output: