Note: If only one statement is to be followed by the if or else condition then there is no need of parenthesis.

Similar documents
Computer Programming. Decision Making (2) Loops

Syntax of for loop is as follows: for (inite; terme; updatee) { block of statements executed if terme is true;

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Computer System and programming in C

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

BRANCHING if-else statements

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

PESIT Bangalore South Campus Hosur Road (1km before Electronic City), Bengaluru Department of Basic Science and Humanities

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

Control Structure: Selection

WARM UP LESSONS BARE BASICS

MODULE 2: Branching and Looping

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Decision Making and Loops

Eng. Mohammed S. Abdualal

Decision Making -Branching. Class Incharge: S. Sasirekha

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

The Hyderabad Public School, Begumpet, Hyderabad, A.P

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

H192 Midterm 1 Review. Tom Zajdel

EC 413 Computer Organization

Lecture 6. Statements

1.3b Type Conversion

Use of scanf. scanf("%d", &number);

DECISION CONTROL AND LOOPING STATEMENTS

Decision Making and Branching

LAB 4.1 Relational Operators and the if Statement

Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

printf( Please enter another number: ); scanf( %d, &num2);

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS DEPARTMENT OF SCIENCE AND HUMANITIES EVEN SEMESTER FEB 2017

AMCAT Automata Coding Sample Questions And Answers

Chapter 2 - Introduction to C Programming

Subject: PIC Chapter 2.

Data Type Fall 2014 Jinkyu Jeong

Fundamentals of Programming

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Physics 2660: Fundamentals of Scientific Computing. Lecture 5 Instructor: Prof. Chris Neu

SECTION A TRUE / FALSE QUESTIONS (10 MARKS) (INSTRUCTION: Please answer all 10 questions)

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Copy: IF THE PROGRAM or OUTPUT is Copied, then both will have grade zero.

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

UNIVERSITY OF WINDSOR Fall 2006 QUIZ # 1. Examiner:Ritu Chaturvedi Dated : Oct 3rd, Student Name: Student Number:

Other Loop Options EXAMPLE

Lecture 3: C Programm

Practice problems Set 2

Quiz1 Fall 2007 October 2 nd, UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated :October 2nd, 2007.

DECISION MAKING STATEMENTS

Lecture 8. Daily Puzzle

C: How to Program. Week /Mar/05

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Flow Chart. The diagrammatic representation shows a solution to a given problem.

Structured Programming. Dr. Mohamed Khedr Lecture 4

Day02 A. Young W. Lim Sat. Young W. Lim Day02 A Sat 1 / 12

Introduction to C An overview of the programming language C, syntax, data types and input/output

UNIVERSITY OF WINDSOR Winter 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated : Feb 7 th, Student Name: Student Number:

Data Types. Data Types. Integer Types. Signed Integers

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

2. Numbers In, Numbers Out

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

3. Types of Algorithmic and Program Instructions

Informatica e Sistemi in Tempo Reale

Chapter 5: Control Structures

A control structure refers to the way in which the Programmer specifies the order of executing the statements

Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

Chapter 3 Structured Program Development in C Part II

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

Module 4: Decision-making and forming loops

Conditionals. For exercises 1 to 27, indicate the output that will be produced. Assume the following declarations:

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Basic Assignment and Arithmetic Operators

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Programming Language A

QUIZ: What value is stored in a after this

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS)

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

COMP 208 Computers in Engineering

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

ET156 Introduction to C Programming

Dept. of CSE, IIT KGP

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

n Group of statements that are executed repeatedly while some condition remains true

LAB 11: METHODS. CPCS The Lab Note Lab 11 Page 1. Statement Purpose:

CSI33 Data Structures

2. Numbers In, Numbers Out

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Introduction to Programming

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

LAB 5: REPETITION STRUCTURE(LOOP)

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Transcription:

Birla Institute of Technology & Science, Pilani Computer Programming (CSF111) Lab-4 --------------------------------------------------------------------------------------------------------------------------------- Objectives Conditional control structure YOU SHOULD NOT COPY AND PASTE THE PROGRAMS. READ THE QUESTION, TRY TO WRITE A PROGRAM YOURSELF, AND TEST IT. IF YOU ARE UNABLE TO WRITE PROGRAM, THEN READ THE SOLUTION, UNDERSTAND IT AND THEN WRITE THE PROGRAM YOURSELF... WITH OUT LOOKING AT THE SOLUTION. THIS IS THE ONLY WAY YOU WILL LEARN... COPY PASTE WILL NOT HELP YOU OUT. --------------------------------------------------------------------------------------------------------------------------------- Conditional Control Structure Normally, program flows along line by line in the order in which it appears in your source code. But, it is sometimes required to execute a particular portion of code only if certain condition is true; or false i.e. you have to make decision in your program. There are three major decision making structures. The one way decision using if statement The if statement enables you to test for a condition (such as whether two variables are equal) and branch to different parts of your code, depending on the result or the conditions with relational and logical operators are also included. The simplest form is block of statements executed if condition is true; Two way decision using if- statement If statement is used when we want to take one branch if your condition is true, another if it is false. The form of if is as follows block of statements executed if condition is true; block of statements executed if condition is true; Multi-way Decision Multi-way decision statements use if--if nested ifs. They are used to evaluate a test expression that could have several possible values. It is often used to choose between ranges of values. The form of multi-way decision is as follows block of statements executed if condition is true; if(testexpr) block of statements executed if condition is true; block of statements executed if condition is true; Note: If only one statement is to be followed by the if or condition then there is no need of parenthesis. Ex-1 Write a program that inputs an integer and determine if it is even or odd.

int main() int i; printf( Enter Number\n ); scanf( %d,&i); if(i%2==0) printf( number is even\n ); printf( number is odd\n); return 0; Ex-2 Write a program which takes three sides of a triangle input and calculates its area,if these conditions are satisfied a+b>c, b+c>a, a+c>b, calculate area=(a+b+c)/2 int main() int side1,side2,side3; printf( \n enter the value of sides\n ); scanf( %d%d%d,&side1,&side2,&side3); if(side1+side2 > side3) if(side1+side3> side2) if(side2+side3> side1) printf( \n Area = %f\n, (a+b+c)\2); return 0; Ex-3 Write a program which takes a character input & checks if it is a vowel or consonant. int main() char c; Printf( enter character\n ); scanf( %c,&c); if(c == a c == e c== i c== o c== u ) printf( %c is vowel\n,c); printf( %c is consonant,c); Ex-4 Write a program (calc.c) that does the job of a simple calculator with three operations( addition, division and power).

#include <stdio.h> #include <math.h> int main(void) int num1, num2; float res; char op; printf ("Enter First Number\n"); scanf("%d",&num1); printf ("Enter Second Number\n"); scanf("%d",&num2); printf ("Enter the operator (+, /, ^)\n"); scanf (" %c",&op); if (op == '+') res = num1 + num2; /*Implicit Type Casting */ if (op == '/') res = (float)num1/(float)num2; /*Explicit Type Casting */ if (op == '^') res = pow(num1,num2); printf ("\n Invalid Operator"); printf ("\n num1 %c num2 = %f",op,res); [NOTE: Since you have included math.h library, you need to explicitly link it during compile time. Therefore for compiling the above program write gcc lm calc.c Also try the option gcc lm calc.c o mycalculator ]. Switch Statement The switch statement is provided by C to select one of several alternatives. The switch statement is especially useful when the selection is based on the value of a single variable or of a simple expression (also called the controlling expression). The value of this expression may be char or int but not double. Ex-5 Consider again the same program (calc.c) using switch-case conditional construct. Try it and understand the difference between the two versions of the same program. #include <stdio.h> #include <math.h> int main(void)

int num1, num2; float res; char op; printf ("Enter First Number\n"); scanf("%d",&num1); printf ("Enter Second Number\n"); scanf("%d",&num2); printf ("Enter the operator (+, /, ^)\n"); scanf (" %c",&op); switch (op) case '+': res = num1 + num2; case '/': res = (float)num1/(float)num2; case '^': res = pow(num1,num2); default: printf ("\n Invalid Operator"); printf ("\n num1 %c num2 = %f",op,res); Exercises 1) Rewrite the program Ex-4 (calc.c) using simple if statement. 2) Replace the statement res = (float)num1/(float)num2 in any version of the above programs by res = (float)num1/num2 and observe what happens. Does the output change? Why. 3) Remove all the break statements from Ex-5 (with switch-case construct) and try to execute the program with few inputs. Observe the difference. 4) Write a program to find whether an entered year of a twenty-first century is a leap year or not. Your program should give an error message if the year entered does not belong to twenty-first century. Hint: A year is a leap year if it is divisible by 4, except that any year divisible by 100 is a leap year only if it is divisible by 400. 5) Write a program that returns a letter grade based on a quiz score. The input will be the integer score from a ten point quiz. (Use nested if-) The letter grades are assigned by: 9-10 A 7-8 B 5-6 C 3-4 D < 3 F

6) Rewrite the program in Q.5 using switch statement. 7) Write a C program that takes month number as an input and displays the number of days in the month. If input is greater than 12 or less than 1 then program should display an error message.