0% found this document useful (0 votes)
26 views

Decision Making and Branching: Lab Assignment-3

This document contains 9 C programming questions and their solutions involving concepts like switch case, if-else, functions, operators, trigonometric functions. Each question tests a different concept and the solutions provided implement the required logic to solve the problem.

Uploaded by

Om Subham Reso
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Decision Making and Branching: Lab Assignment-3

This document contains 9 C programming questions and their solutions involving concepts like switch case, if-else, functions, operators, trigonometric functions. Each question tests a different concept and the solutions provided implement the required logic to solve the problem.

Uploaded by

Om Subham Reso
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab Assignment-3

Decision Making and Branching


NAME : Om Subham Pati
ROLL NO: 21075062
DEPART : computer science and engineering

Q1. Write a C program to implement a calculator which performs +, -, \, * operations using switch
case.
#include <stdio.h>
int main()
{
float n1,n2;
printf("enter two numbers:");
scanf("%f %f",&n1,&n2);
int a;
printf("enter the operator code(1:+,2:-,3:*,4:/) :");
scanf("%d",&a);
switch (a)
{
case 1: printf("sum of the numbers is : %f",n1+n2);
break;
case 2: printf("difference of the numbers is : %f",n1-n2);
break;
case 3: printf("product of the numbers is : %f",n1*n2);
break;
case 4: printf("division of the two number is: %f",(float)n1/n2);
break;
default: printf("invalid input");
break;
}
return 0;
}

Q2. Write a C program to get marks from the user and assign the following grades and print it.
#include<stdio.h>

int main()
{
int a;
printf("marks of student : ");
scanf("%d",&a);

if (a>90&&a<=100)
{
printf("your marks is %d and grade is A\n",a);
}
else if (a>70&&a<=90)
{
printf("your marks is %d and grade is B\n",a);
}
else if (a>50&&a<=70)
{
printf("your marks is %d and grade is C\n",a);
}
else if (a>=0&&a<=50)
{
printf("your marks is %d and grade is E\n",a);
}
else
{ printf("entered marks is invalid\n");}
return 0;
}

Q3. Write a C program to print if a number is odd or even.


#include <stdio.h>
int main() {
long long int a;
printf("enter a number : ");
scanf("%d",&a);
if (a%2==0){printf("even");}
else{printf("odd");}
return 0;
}

Q4. Write a C program to find the roots of a quadratic equation. You should print the roots even if
they are complex.

#include <stdio.h>
#include <math.h>
int main() {
float a,b,c;
printf("GIVEN A QUADRATIC EQUATION OF FORM : ax^2 + bx + c = 0 \nenter a,b,c : ");
scanf("%f %f %f",&a,&b,&c);
float d;
d=(b*b)-4*a*c;
if(d>=0){
float rootd,x1,x2;
rootd=sqrt(d);
x1=(-b+rootd)/(2*a);x2=(-b-rootd)/(2*a);
printf("roots are %f & %f\n",x1,x2);
}
else{
float rootd,x1,x2;
rootd=sqrt(d*-1);
x1=(-b+rootd)/(2*a);x2=(-b-rootd)/(2*a);
printf("roots are %f + %f i & %f - %f i\n",-b/(2*a),(rootd)/(2*a),-b/(2*a),(rootd)/(2*a));
}
return 0;
}

Q5. Write a C program to implement the modulus function without using modulo operator.
Function1: to find absolute value
#include <stdio.h>
int main() {
float a;
printf("enter a number: ");
scanf("%f",&a);
if(a>=0){
printf("modulo of %f is %f",a,a);
}
else {
printf("modulo of %f is %f",a,-a);
}
return 0;
}
Function 2: to find the remainder when two numbers are divided
#include <stdio.h>
int main()
{
int a,b,q,r;
printf("when a number :");
scanf("%d",&a);
printf("is divided by :");
scanf("%d",&b);
q=a/b;
r=a-(q*b);
printf("rem is :%d\n",r);
return 0;
}

6. Write a C program to test if the numbers entered can form sides of a triangle or not.
#include <stdio.h>
int main()
{
float a,b,c,s;
printf("enter three numbers :");
scanf("%f %f %f",&a,&b,&c);
// for triangle sum of two sides should be greater than third side
//we can also say that all three sides should be less than semiperimeter
s=(a+b+c)/2;//s is semiperimeter
if (a<s&&b<s&&c<s){
printf("it is sides of a valid triangle\n");
}
else{
printf("invalid trianle sides\n");
}

return 0;
}

7. Write a C program to print the name given numeric value of a month. For example it should print
November if 11 is entered and error if any number less than 1 or greater than 12 is entered
#include <stdio.h>
int main()
{
int a;
printf("enter month code :");
scanf("%d",&a);

switch (a)
{
case 1: printf("january");
break;
case 2: printf("february");
break;
case 3: printf("march");
break;
case 4: printf("april");
break;
case 5: printf("may");
break;
case 6: printf("june");
break;
case 7: printf("july");
break;
case 8: printf("august");
break;
case 9: printf("september");
break;
case 10: printf("october");
break;
case 11: printf("november");
break;
case 12: printf("december");
break;
default: printf("invalid input");
break;
}
return 0;
}

8. Write a C program to find if a given year is leap year or not.

#include <stdio.h>
int main() {
int year;
printf("enter the year to be checked: ");
scanf("%d",&year);
if ((year%4)==0&&(year%100)!=0)
{
printf("it is a leap year");
}
else if ((year%400)==0)
{
printf("it is a leap year");
}
else
{
printf("it is not a leap year");
}

return 0;
}

9. Write a C program to print the sine of the value entered if choice is 1 or cosine if choice is 2. If any
other value is entered, then it should return tan of the value.
#include <stdio.h>
#include <math.h>
int main() {
int n;
printf("for sine enter 1\nfor cosine enter 2\nenter the required operation code:");
scanf("%d",&n);
float a;
float s,c,t;
printf("enter the angle(in degrees): ");
scanf("%f",&a);
if(n==1){
s=sin(a*3.14159265358979/180);
printf("value of sin(%.2f) is %f",a,s);

}
else if(n==2){
c=cos(a*3.14159265358979/180);
printf("value of cos(%.2f) is %f",a,c);

}
else{
t=tan(a*3.14159265358979/180),
printf("value of tan(%.2f) is %f",a,t);
}
return 0;
}

You might also like