- Click below to download Unit 2 programs in pdf file: -
UNIT
2
/* 1.Write a program to accept number of seconds and
display its
corresponding hours, minutes and seconds.*/
#include <stdio.h>
int main()
{
int
sec, h, m, s;
printf("Input
seconds: ");
scanf("%d",
&sec);
h =
(sec/3600);
m =
(sec -(3600*h))/60;
s =
(sec -(3600*h)-(m*60));
printf("H:M:S
- %d:%d:%d\n",h,m,s);
return
0;
}
/* 2. Write a C program to find the maximum from given
three
numbers (Using Nested IF).*/
#include <stdio.h>
int main() {
double n1, n2,
n3;
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if (n1 >=
n2 && n1 >= n3)
printf("%.2f is the largest number.", n1);
else if (n2
>= n1 && n2 >= n3)
printf("%.2f is the largest number.", n2);
else if (n3
>= n1 && n3 >= n2)
printf("%.2f is the largest number.", n3);
return 0;
}
/* 3. Write a C program to find that the accepted no
is Negative,
Positive or Zero.*/
#include<stdio.h>
int main()
{
int no;
printf("\n
Enter any number : ");
scanf("%d",&no);
if(no==0)
{
printf("\n Entered Number is Zero");
}
else
if(no>0)
{
printf("\n Entered Number is Positive");
}
else
{
printf("\n Entered Number is Negative");
}
return 0;
}
/* 4. Write a program to check given year is a Leap
year or not.*/
#include <stdio.h>
int main()
{
int y;
printf("Enter year: ");
scanf("%d",&y);
if(y % 4 ==
0)
{
if( y %
100 == 0)
{
if (
y % 400 == 0)
printf("%d is a Leap Year", y);
else
printf("%d is not a Leap Year", y);
}
else
printf("%d is a Leap Year", y );
}
else
printf("%d is not a Leap Year", y);
return 0;
}
/* 5. Write a C program to find minimum from given 3
numbers (Using
Conditional Operator).*/
#include<stdio.h>
void main()
{
int
a,b,c,small;
printf("Enter Three Number\n");
scanf("%d %d %d",&a,&b,&c);
small =
a<b?a<c?a:c:b<c?b:c;
printf("Smallest Among 3 Number is : %d",small);
}
/* 6. Write a C program to find the maximum from given
three
numbers (Without using Nested if, or Logical Operator,
Or
Conditional operators).*/
#include <stdio.h>
void main()
{
int a,b,c;
printf("enter the three numbers");
scanf("%d%d%d",&a,&b,&c);
printf("the biggest value is
%d",(a>b&&a>c?a:b>c?b:c));
}
/* 7. Take marks from the user and print grade
accordingly( >=75
marks – Distinction, <75 and >=60 marks – First,
<60 and >=50 –
Second, <50 and >=35 – Pass, <35 – Fail)
using if … else if….else
statement and also by using logical operators).*/
#include<stdio.h>
int main()
{
int marks;
printf("\n
Enter Marks between 0-100 :");
scanf("%d",&marks);
if(marks>100
|| marks <0)
{
printf("\n Your Input is out of Range");
}
else
if(marks>=75)
{
printf("\n You got Distinction");
}
else
if(marks<75 && marks>=60)
{
printf("\n You got First Class");
}
else
if(marks<60 && marks>=50)
{
printf("\n You got Second Class");
}
else
if(marks<50 && marks>=35)
{
printf("\n Pass");
}
else
{
printf("\n You got Fail");
}
return 0;
}
/* 8. Take 2 numbers from the user and print the
greater number
(Number can be equal).*/
#include <stdio.h>
int main()
{
int num1,
num2;
printf("Please Enter Two different values\n");
scanf("%d %d", &num1, &num2);
if(num1 >
num2)
{
printf("%d is Largest\n", num1);
}
else if
(num2 > num1)
{
printf("%d is Largest\n", num2);
}
else
{
printf("Both are Equal\n");
}
return 0;
}
/* 9. Write a program to check whether the blood donor
is eligible or
not for donating blood. The conditions laid down are
as under. Use
if statement.
a) Age should be above 18 yrs but not more than 55
yrs.*/
#include <stdio.h>
void main()
{
int
age,w;
printf("enter
age : ");
scanf("%d",&age);
if(age>18
&& age<55)
{
printf("eligible");
}
else
{
printf("Not eligible");
}
}
/* 10. Write a program to calculate bill of a job work
done as follows.
Use if else statement.
a) Rate of typing 3 Rs/page
b) Printing of 1st copy 5Rs/pages & later every
copy 3Rs/page.
The user should enter the number of pages and print
out copies
he/she wants.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int coppy,
pages, tycost, prcost, fprcost, oprcost, total;
printf("\n Enter Number Of Pages::");
scanf("%d",&pages);
printf("\n Enter Number Of
Coppy::");
scanf("%d",&coppy);
tycost=pages*3;
if(coppy==1)
{
prcost=5*pages;
}
else
{
fprcost=5*pages;
oprcost=(coppy-1)*pages*5;
prcost=fprcost+oprcost;
}
total=tycost+prcost;
printf("\n total cost of Job work is %d",total);
}
/* 11. The ABC Insurance Company Ltd. Offers the
following three
categories of car insurance policy to car owners:
• Category A, here the basic premium is calculated as
2% of the
car’s value.
• Category B, here the basic premium is calculated as
3% of the
car’s value.
• Category C, here the basic premium is calculated as
5% of the
car’s value. */
#include<stdio.h>
void main()
{
float value,
insu;
char type;
printf("\n Enter the Insurance type:");
scanf("%c",&type);
printf("\n Enter the value of car ::");
scanf("%f",&value);
if(type=='a')
{
insu=(value*2)/100;
}
else
if(type=='b')
{
insu=(value*3)/100;
}
else
if(type=='c')
{
insu=(value*5)/100;
}
printf("\n %f is The Insurance value",insu);
}
/* 12. Write a program to implement calculator using
switch case */
#include <stdio.h>
int main() {
char op;
double first,
second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);
switch (op)
{
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first +
second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first -
second);
break;
case '*':
printf("%.1lf
* %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first /
second);
break;
default:
printf("Error! operator is not correct");
}
}
0 Comments