Click here to load reader
Upload
turjo987
View
17
Download
1
Embed Size (px)
DESCRIPTION
East West University ETE lecture for all.
Citation preview
Objectives:
The main objective of this lab experiment to guide you through a series of C programming
problems, using the control/branching statements i.e. if, if-else, conditional operator.
Exercise 1: Write a C program to check whether two numbers entered by the user are
equal or not.
Exercise 2: Write a C program to check whether an integer number entered by the user is
even or odd.
Exercise 3: Write a C program to check whether a year entered by the user is leap year of
not.
Definition of leap year:
Rule 1: A year is called leap year if it is divisible by 400.
For example: 1600, 2000 etc leap year while 1500, 1700 are not leap year.
Rule 2: If year is not divisible by 400 as well as 100 but it is divisible by 4 then that year is also
leap year.
For example: 2004, 2008, 1012 are leap year.
Lab Assignment: Write a C program that reads in two numbers from the user along with
an operator to perform addition, subtraction, multiplication or division. It calculates the
results depending on the operator and displays the result. If the user enters any invalid
operators, the program notifies the user.
A sample output is given below:
Fall 2014 Semester
ETE 105 LAB [Section:1]
Experiment 3: C Programs using branching Statements
Name: ID:
Enter two numbers and an operator in the format: number1 operator number2
Note that valid operators are:
addition --> +
subtraction --> -
multiplication --> *
division --> /
15.1+6.2
The result is 21.299999
Solutions
Exercise 1:
#include <stdio.h>
int main()
{
int num1, num2;
printf("Enter two numbers\n");
scanf("%d %d",&num1, &num2);
if(num1==num2)
printf("%d and %d are equal", num1, num2);
else
printf("%d and %d are not equal", num1, num2);
return 0;
}
Exercise 2:
#include <stdio.h>
int main()
{
int num;
printf("Enter an integer you want to check.\n");
scanf("%d",&num);
if((num%2)==0)
printf("%d is even",num);
else
printf("%d is odd",num);
return 0;
}
Exercise 3:
Code 1:
#include<stdio.h>
int main()
{
int year;
printf("Enter any year: ");
scanf("%d",&year);
if((year%400==0)||((year%4==0)&&(year%100!=0)))
printf("%d is a leap year",year);
else
printf("%d is not a leap year",year);
return 0;
}
Code 2:
#include <stdio.h>
int main()
{
int year;
printf("Enter any year: ");
scanf("%d",&year);
if(year%400==0)
printf("%d is a leap year", year);
else
if((year%100!=0)&&(year%4==0))
printf("%d is a leap year", year);
else
printf("%d is not a leap year", year);
return 0;
}
Lab Assignment:
#include <stdio.h>
main()
{
char o;
float num1, num2, result;
printf("Enter two numbers and an operator in the format: number1
operator number2\n\n");
printf("Note that valid operators are\n");
printf("addition --> +\n");
printf("subtraction --> -\n");
printf("multiplication --> *\n");
printf("division --> /\n\n");
scanf("%f %c %f", &num1, &o, &num2);
if(o=='+')
result = num1+num2;
else if(o=='-')
result = num1-num2;
else if(o=='*')
result = num1*num2;
else if(o=='/')
result = num1/num2;
else
printf("Invalid operator!\n");
printf("The result is %f\n", result);
}