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

CP lab-week 3

The document contains multiple C programs demonstrating various programming concepts such as calculating square roots, finding the largest and smallest numbers among given inputs, calculating total and average marks, determining leap years, and basic arithmetic operations using switch statements. Each program includes user input and output statements to interact with the user. Overall, these examples serve as practical applications of fundamental C programming techniques.

Uploaded by

anusha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

CP lab-week 3

The document contains multiple C programs demonstrating various programming concepts such as calculating square roots, finding the largest and smallest numbers among given inputs, calculating total and average marks, determining leap years, and basic arithmetic operations using switch statements. Each program includes user input and output statements to interact with the user. Overall, these examples serve as practical applications of fundamental C programming techniques.

Uploaded by

anusha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

#include <math.

h>

#include <stdio.h>

int main()

int number, squareRoot;

printf("Enter a number: ");

scanf("%d", &number);

squareRoot = sqrt(number);

printf("Square root of the number=%d",squareRoot);

return 0;

2. C Program To Find The Largest of Two Numbers Using


Ternary Operator?
#include<stdio.h>

void main()
{
// Variable declaration
int a,b,larg;
printf("Enter two number\n");
scanf("%d %d", &a,&b);

// Largest among a and b


larg = a>b?a:b;

//Display largest number


printf("Largest Number is : %d" ,larg);

}
3. C Program to take 5 subject marks in integer and find the total, average and
percentage in float?

#include <stdio.h>
int main()
{
int eng, phy, chem, math, comp;
float total, average;
/* Input marks of all five subjects */
printf("Enter marks of five subjects: \n");
scanf("%d%d%d%d%d",&eng,&phy,&chem,&math,&comp);
/* Calculate total, average and percentage */
total = eng + phy + chem + math + comp;
average = total / 5.0;
/* Print all results */
printf("Total marks=%f\n", total);
printf("Average marks=%f\n", average);
return 0;
}

4. C Program to find the given year is a leap year or not?


#include <stdio.h>
main()
{
int year;
printf("enter the year");
scanf("%d",&year);
if(year%4==0)
{
printf("leap year");
}
else
{
printf("not leap year");
}
}

5. write a program using the C language to find the


greatest number among four numbers?
#include<stdio.h>
main()
{
int num1, num2, num3, num4;
printf("Enter 4 numbers here...\n");
scanf("%d %d %d %d", &num1,&num2,&num3,&num4);
if(num1>num2&&num1>num3&&num1>num4)
{
printf("number 1 is the greatest:%d",num1);

}
else if(num2>num3&&num2>num4)
{
printf("number 2 is greatest:%d",num2);

else if(num3>num4)
{
printf("number 3 is greatest:%d",num3);
}
else
{
printf("number 4 is greatest:%d",num4);

}
}
6. C program to find the largest number among three numbers?
#include <stdio.h>
int main()
{
int a,b,c;
printf("enter three numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
{
printf("a is largest:%d",a);
}
else if(b>a && b>c)
{
printf(" b is largest:%d",b);
}
else
{
printf(" c is largest:%d",c);
}
}
7. C program to find the smallest number among three numbers?
#include <stdio.h>
int main()
{
int a,b,c;
printf("enter three numbers");
scanf("%d%d%d",&a,&b,&c);
if(a<b && a<c)
{
printf("a is smallest:%d",a);
}
else if(b<a && b<c)
{
printf(" b is smallest:%d",b);
}
else
{
printf(" c smallest:%d",c);
}
}
8. C Program to find the smallest number among four numbers?
#include <stdio.h>
int main()
{
int a,b,c,d;
printf("enter three numbers");
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a<b && a<c && a<d)
{
printf("a is smallest:%d",a);
}
else if(b<c && b<d)
{
printf("b is smallest:%d",b);
}
else if(c<d)
{
printf("c smallest:%d",c);
}
else
{
printf("d is smallest:%d",d);
}
}
9.
#include<stdio.h>
int main()
{
int a, b;
char choice;
printf("Enter your choice\n");
printf("a. Addition\nb. Subtraction\nc. Multiplication\nd. Division\n");
scanf("%c", &choice);
printf("Enter 2 integer numbers\n");
scanf("%d %d", &a, &b);
switch(choice)
{
case 'a': printf("addition=%d",(a+b));
break;

case 'b': printf("subtraction=%d",(a-b));


break;

case 'c': printf("multiplication=%d",(a*b));


break;
case 'd': if( b != 0)
printf("divison=%d",(a/b));
else
printf("Number can't be divided by 0\n");
break;

default: printf("You entered wrong choice\n");


break;
}

return 0;
}

#include <stdio.h>
int main()
{
int day;
printf("enter the day\n");
scanf("%d",&day);
switch(day)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Thursday");
break;
case 6:
printf("Thursday");
break;
case 7:
printf("Thursday");
break;
default:
printf("Invalid Input");
break;
}
}

You might also like