You are on page 1of 9

Green University of Bangladesh

Department of Electrical and Electronic Engineering

CSE-102
Computer Programming Laboratory

Student ID 221010005

Student Name Ansarul Haque


Section EA

Name of the Program EEE

Name of the Department EEE(Weekend)

1
Name : Sahiduzzaman
ID :221010020
Experiment No: 04
Name of the Experiment: Understanding the decision statements in C
Objective(s):

To understand the programming knowledge using Decision Statements (if, if-else, if-else if ladder,
switch and GOTO)
Learning outcome(s):
Students will be able to perform conditional and logical operations in C. They will be able to write
programme using conditional expressions.

Problem
Problem 1

/*Write a program to find the largest and smallest among three entered numbers and also display
whether
the identified largest/smallest number is even or odd.*/

#include<stdio.h>
#include<stdlib.h>

int main(){

int a,b,c,min,max;

printf("Enter Three number : ");

scanf("%d%d%d",&a,&b,&c);

if(a < b&& a < c){


min = a;
}
else if (b < a && b < c){
min = b;
}
else{
min = c;
}
if(a > b && a > c){
max = a;
}
else if ( b > a && b > c){
max = b;
}
else{
max = c;
}
printf("\nThe smallest number is : %d",min);

if(min%2 == 0){
printf("\n The smallest number is even");

2
Name : Sahiduzzaman
ID :221010020
else{
printf("\n The smallest number is odd");

}
printf("\nThe largest number is : %d",max);

if(max%2 == 0){
printf("\n The largest number is even");
}
else{

printf("\n The largest number is odd");

}
return 0;
}

Output :

Problem 2

/*
Write a program to check whether input alphabet is vowel or not using if-else and switch
statement.
*/
#include <stdio.h>
int main() {
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
3
Name : Sahiduzzaman
ID :221010020
scanf("%c", &c);

// evaluates to 1 if variable c is a lowercase vowel


lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

// evaluates to 1 if variable c is a uppercase vowel


uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

// evaluates to 1 (true) if c is a vowel


if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}

Output :

Problem 3

/*Write a program to get input of two or higher digit integer number and display in reverse order.*/

#include <stdio.h>

int main()
{
int n, r = 0;

printf("Enter a number to reverse\n");


scanf("%d", &n);

while (n != 0)
{
r = r * 10;
r = r + n%10;
n = n/10;
}

printf("Reverse of the number = %d\n", r);


4
Name : Sahiduzzaman
ID :221010020
return 0;
}

Output :

Problem 4

/*
Write a program that asks a number and test the number whether it is multiple of 5 or not, divisible by
7 but not by eleven.
*/
#include<stdio.h>

int main(){
int num;

// Asking for Input


printf("Enter an number: ");
scanf("%d", &num);

if ((num % 5) == 0)
{
printf(" Number %d is multiple of 5\n", num);
}
else
{
printf(" Number %d is not multiple of 5\n", num);
}
if ((num % 7 == 0) && (num % 11 != 0)){
printf("%d is divisible by 7 but not by 11.\n", num);
}
else{

5
Name : Sahiduzzaman
ID :221010020
printf("%d is not divisible by 7 but not by 11.\n", num);
}
return 0;
}

Output :

Problem 5

/*
Write a program to check whether the entered year is leap year or not (a year is leap if it is divisible
by 4 and divisible by 100 or 400.)
*/

#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);

// leap year if perfectly divisible by 400


if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
6
Name : Sahiduzzaman
ID :221010020
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}

return 0;
}

Output :

Problem 6

/* Write a program to read the values of coefficients a, b and c of a quadratic equation


ax2+bx+c=0 and find roots of the equation.
*/

#include <stdio.h>
#include <math.h>

int main()
{
int a, b, c, d;
double root1, root2;

printf("Enter a, b and c where a*x*x + b*x + c = 0\n");


scanf("%d%d%d", &a, &b, &c);

d = b*b - 4*a*c;

7
Name : Sahiduzzaman
ID :221010020
if (d < 0) { // complex roots, i is for iota (√-1, square root of -1)
printf("First root = %.2lf + i%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a));
printf("Second root = %.2lf - i%.2lf\n", -b/(double)(2*a), sqrt(-d)/(2*a));
}
else { // real roots
root1 = (-b + sqrt(d))/(2*a);
root2 = (-b - sqrt(d))/(2*a);

printf("First root = %.2lf\n", root1);


printf("Second root = %.2lf\n", root2);
}

return 0;
}

Output :

8
Name : Sahiduzzaman
ID :221010020
9
Name : Sahiduzzaman
ID :221010020

You might also like