Lab 2
Lab 2
Aim:
To write a C program to find whether the given year is leap year or not using
if statement.
Algorithm:
Program:
#include <stdio.h>
void main( )
{
int year;
printf("Enter a year: ");
scanf("%d", &year); if(year%4 = = 0)
{
if( year%100 = = 0)
{
// year is divisible by 400, hence the year is a leap year
if ( year%400 = = 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
getch( );
}
Output
Enter a year: 1900
1900 is not a leap year
Result:
Thus the C program using to find whether the given year is leap or not year
has been successfully verified and executed.
Ex.No: 4. Write a program to perform the Calculator operations, namely,
addition, subtraction, multiplication, division and square of a
number.
Aim:
To write a C program to perform calculator operation using the switch case.
Algorithm:
Program:
#include <stdio.h>
#include <conio.h>
void main( )
{
int num1,num2;
float result;
char ch; //to store operator choice
result=0;
switch(ch)
{
case '+': result=num1+num2;
break;
case '-':
result=num1-num2;
break;
case '*':
result=num1*num2;
break;
case 's':
result=num1*num1;
break;
default:
printf("Invalid operation.\n");
}
printf("Result: %d %c %d = %f\n",num1,ch,num2,result);
getch( );
}
Output
First run:
Enter first
number: 10
Enter second
number: 20
Choose operation to perform (+,-,*,/,%): + Result: 10 + 0 = 30.000000
Second run:
Enter first number: 10
Enter second number: 3
Choose operation to perform
(+,-,*,/,%): / Result: 10 / 3 =
3.333333
Third run:
Enter first
number: 10
Enter second
number: 3
Choose operation to perform (+,-,*,/,%): > Invalid operation.
Result:
Thus the C program using to perform calculator operation has been
successfully verified and executed.