2
Computer Basics & Programming Sessional ©Dept. of ETE, CUET Prepared by Dr. Md. Azad Hossain & Piyas Chowdhury Supervised by Dr. Quazi Delwar Hossain CHITTAGONG UNIVERSITY OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF ELECTRONICS AND TELECOMMUNICATION ENGINEERING CHITTAGONG-4349, BANGLADESH. COURSE NO.: CSE 182 Experiment No. 3 C PROGRAMS USING if STATEMENT PRELAB WORK: Read this laboratory manual carefully before coming to the laboratory class, so that you know what is required. Try to follow the lecture notes of CSE 181. DON’T copy others blindly!!! Submit your lab report before the roll call. OBJECTIVE: To familiarize with if statement. To solve different type of conditional problems using if statement. GENERAL FORMAT OF if STATEMENT: if (test expression) { statement-block; } statement-x; If the test expression is true, the statement-block will be executed; otherwise the statement-block will be skipped and the execution will jump to the statement-x. When the condition is true both statement-block and the statement-x are executed in sequence. PROBLEM: Write a C program that will evaluate the ratio of (a + b) to (c d) and will print the result, if c d is not equal to zero. The four values a, b, c and d should be taken from the output terminal.

CSE Exp-3

Embed Size (px)

DESCRIPTION

C lab

Citation preview

Page 1: CSE Exp-3

Computer Basics & Programming Sessional ©Dept. of ETE, CUET

Prepared by – Dr. Md. Azad Hossain & Piyas Chowdhury Supervised by – Dr. Quazi Delwar Hossain

CHITTAGONG UNIVERSITY OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF ELECTRONICS AND TELECOMMUNICATION ENGINEERING

CHITTAGONG-4349, BANGLADESH.

COURSE NO.: CSE 182

Experiment No. 3

C PROGRAMS USING if STATEMENT

PRELAB WORK:

Read this laboratory manual carefully before coming to the

laboratory class, so that you know what is required.

Try to follow the lecture notes of CSE 181.

DON’T copy others blindly!!!

Submit your lab report before the roll call. OBJECTIVE:

To familiarize with if statement.

To solve different type of conditional problems using if statement.

GENERAL FORMAT OF if STATEMENT:

if (test expression) { statement-block; } statement-x;

If the test expression is true, the statement-block will be executed; otherwise the

statement-block will be skipped and the execution will jump to the statement-x.

When the condition is true both statement-block and the statement-x are executed in

sequence.

PROBLEM:

Write a C program that will evaluate the ratio of (a + b) to (c – d) and will print the

result, if c – d is not equal to zero. The four values a, b, c and d should be taken from the

output terminal.

Page 2: CSE Exp-3

Computer Basics & Programming Sessional ©Dept. of ETE, CUET

Prepared by – Dr. Md. Azad Hossain & Piyas Chowdhury Supervised by – Dr. Quazi Delwar Hossain

Program Code:

#include<stdio.h>

#include<conio.h>

void main( )

{

int a, b, c, d;

float ratio;

clrscr( );

printf(“Enter four integer values\n”);

scanf(“%d %d %d %d”, &a, &b, &c, &d);

if (c – d != 0)

{

ratio = (a + b) / (c – d);

printf(“Ratio = %f\n”, ratio);

}

getch( );

}

LAB ASSIGNMENT:

Write a simple C program using if statement to convert a given number of days into

years, months and days. ( 1 year = 365 days)

HOME TASK:

1. Write a simple C program to convert a given number of days into months and days.

2. Write down the comparison between lab assignment and home task 1.

3. Write a C program using nested if statements that will read the value of x from output

terminal and evaluate the following function:

y = 𝟏 𝐟𝐨𝐫 𝐱 > 0 𝟎 𝐟𝐨𝐫 𝐱 = 𝟎−𝟏 𝐟𝐨𝐫 𝐱 < 0