Ex.No: 3.
Write a program to find whether the given year is leap year or
Not? (Hint: not every centurion year is a leap. For example
1700, 1800 and 1900 is not a leap year)
Aim:
To write a C program to find whether the given year is leap year or not using
if statement.
Algorithm:
1. Start the program
2. Get the year to find whether it’s a leap year
3. Check if the year % 4 = = 0 and year % 100 = = 0 and year %400 = = 0 for the leap
year, else it’s not a leap year
4. Display the result
5. Stop the program.
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:
1. Start the program
2. Get the first and second number
3. Choose the choice for addition, subtraction, multiplication, division and
square of a number using switch case.
4. If the choice is addition add operation is performed and other
operations are performed respectively till square of the number.
5. If the choice is default it shows “invalid operation”.
5. Display the result
6. Stop the program.
Program:
#include <stdio.h>
#include <conio.h>
void main( )
{
int num1,num2;
float result;
char ch; //to store operator choice
printf("Enter first number: ");
scanf("%d",&num1);
printf("Enter second number: ");
scanf("%d",&num2);
printf("Choose operation to perform (+,-,*,/,%): ");
scanf(" %c",&ch);
result=0;
switch(ch)
{
case '+': result=num1+num2;
break;
case '-':
result=num1-num2;
break;
case '*':
result=num1*num2;
break;
case '/': result=(float)num1/(float)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: 10 > 3 = 0.000000
Result:
Thus the C program using to perform calculator operation has been
successfully verified and executed.