0% found this document useful (0 votes)
57 views30 pages

ME 172 - Lecture2

The document discusses conditional statements in C programming language. It provides examples of using if and if-else statements to check conditions. The if statement executes code if a condition is true, while if-else executes one block of code if the condition is true and another block if it is false. Operators like ==, !=, <, >, <=, >= are used to check conditions in if and if-else statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views30 pages

ME 172 - Lecture2

The document discusses conditional statements in C programming language. It provides examples of using if and if-else statements to check conditions. The if statement executes code if a condition is true, while if-else executes one block of code if the condition is true and another block if it is false. Operators like ==, !=, <, >, <=, >= are used to check conditions in if and if-else statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

ME 172

Computer Programming Language Sessional


Lecture 2: Operators and Conditional Statements

Mantaka Taimullah
Lecturer
Md. Kamruzzaman
Assistant Professor
Dept of Mechanical Engineering, BUET

1
Performance Test 1
29765 apples are to be divided into 51 buyers. Write a C
program that will calculate how many apples each buyer
gets and how many apples are left and then display the
results.

Time: 5 minutes!!

2
ANSWER
#include <stdio.h>

int main()
{
int a,b,c,d,e,f;
a=29765;
b=51;
c=29765/51;
d=29765%51;
printf("%d and %d",c,d);
return 0;
}

3
Arithmetic operator(Contd...)
• ++a/a++ is equivalent to a=a+1
• --a/a-- is equivalent to a=a-1
++m and m++

Prefix operator Postfix operator

The difference for built-in types is:


• ++a first increments the value of a and then returns a value referring to a, so if the
value of a is used then it will be the incremented value.
• a++ first returns a value whose value is a, that is, the old value, and then increments a
at an unspecified time before the next full-expression (i.e., "before the semicolon").

4
Arithmetic operators(Contd...)

 x=x*a++ is equivalent to x=x*a ; a=a+1


 x=x*++a is equivalent to a=a+1 ; x=x*a

 y=y*b-- is equivalent to y=y*b ; b=b-1


 y=y*--b is equivalent to b=b-1 ; y=y*b

5
Arithmetic Operators(Contd...)

• Write the following program: For x=a*++b output:


210
#include<stdio.h> & for x=a*b++ output:
200
int main()
{
int a=10,b=20,x;
x=a*++b;
printf("\n The value of x is: %d”,x);
return 0;
}

Replace the line x=a*++b with x=a*b++


6
Arithmetic Operator(Contd...)
Shorthand Notation
Operator Description Example
+= Add AND assignment operator C += A is equivalent to
C = C +A

-= Subtract AND assignment operator C -= A is equivalent to


C = C -A

*= Multiply AND assignment operator C *= A is equivalent to


C = C *A

/= Divide AND assignment operator C /= A is equivalent to


C = C /A

7
Math.h (header file)
Most of the mathematical functions are placed in math.h header
Some are specified in the stdlib.h header
Some common mathematical functions:

Function Name Description


exp(x) returns e raised to the given power (ex)
sqrt(x) computes square root (√x)
cos(x) computes cosine (cos(x))
pow(x,y) raises a number to the given power (xy) [pow(x.y)]
sinh(x) computes hyperbolic sine (sinh(x))
erf(x) error function
And so on……. tan(x), abs(x), fabs(x), log10(x)….etc
The outputs of the functions are of the double format.

8
Math.h header file
Math Constants:

Constant Name Description


M_E The base of natural logarithms (e).
M_LOG2E The base-2 logarithm of e.
M_PI 3.141593
M_SQRT2 The positive square root of 2.
M_SQRT1_2 The positive square root of 1/2.
And so on…….

9
Practice Example

#include<stdio.h>
#include<math.h>
int main()
{
output:
double pi; -1.000000
pi=M_PI; //sets pi = 3.1416
double sum;
sum=cos(pi);
//here in cos(x) , x is radian value, so input should be radian
printf(“%lf”,sum);
return 0;
}

10
Class Performance 2

• Write a program that takes two numbers as input.


• Find the square root of the first number and the
resulting output will be the radius of a cylinder.
• Raise the second input number to a power of 5. The
resulting output will be the height of the cylinder.
• Find the volume of the cylinder by using the saved
value of pi in the header file.

• Remember to use the math.h file.

11
ANSWER
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{ int a,b;
double r,h,pi,V;
pi= M_PI;
printf(“Enter the first number: \n"); scanf("%d",&a);
printf(“Enter the second number: \n"); scanf("%d",&b);
r= sqrt(a);
h= pow(b,5);
V=pi*r*r*h;
printf("The volume is %lf",V);
return 0;
}
12
The getch() function
The getch( ) function reads a single character the instant it’s typed
without waiting for ENTER.
get means it gets something i.e. it’s an input function
ch means it gets a character

The getche() function


The getche( ) function also reads a single character the instant
it’s typed without waiting for ENTER and also echoes it.
get means it gets something i.e. it’s an input function
ch means it gets a character
e means it echoes the character to the screen
when you type it.

13
Example
#include <stdio.h>
void main(void)
{
char test;
printf(“Type any character: ”);
test = getch();
printf(“\nThe character you typed was: %c”,test);
}

Replace getch() with getche()


14
Conditional
Statements

15
16

The if statement

General form:

if (condition)
{
statement;
}

Conditions:
1. Using relational or conditional operators
2. Using logical operators
17

Multiple statements within if

General form:
if (condition)
{
statement 1;
statement 2;
-----------;
statement n;
}
18

Example of if statement
Program to display a number if user enters negative number

#include <stdio.h> Enter an integer: -3


int main() You entered a negative number
{ You entered -3
int number;
printf("Enter an integer: ");
scanf("%d", &number); Enter an integer: 5
if (number < 0) You entered 5
{
printf("You entered a negative number");
}
printf(“\nYou entered %d”, number);
return 0;
}
Conditions
Comparison operators
It performs tests on their operands. They return he
Boolean value . Such as:
•1if the statement is successful (true)
•0 otherwise

Example Name Result

a == b Equal TRUE if a is equal to b.


a != b Not Equal TRUE if a is not equal to b.
a<b less than TRUE if a is strictly less than b.
a>b greater than TRUE if a is strictly greater than b.
a<=b less than or equal to TRUE if a is less than or equal to b.
a>=b greater than or equal to TRUE if a is greater than or equal to b.
19
20
General form

if-else if-else if-else


if (condition) if (condition)
{ {
statement 1; statement 1;
statement 2; statement 2;
} }

else else if (condition)


{ {
statement 1; statement 1;
statement 2; statement 2;
} }

else
{
statement 1;
Note: else is optional statement 2;
}
21

Example of if-else statement


Program to check whether an integer entered by the user is odd or even

#include <stdio.h>
int main()
{
Enter an integer: 7
int number;
7 is an odd integer.
printf("Enter an integer: ");
scanf("%d",&number);
if( number%2 == 0 )
printf("%d is an even integer.", number); Enter an integer: 18
else 18 is a even integer.
printf("%d is an odd integer.", number);
return 0;
}
An alternate to if-else
The Conditional operator (? :)
General form:
Conditional expression? Expression1 : Expression2

Example
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
(number % 2 == 0) ? printf("%d is even.", number) : printf("%d is odd.", number);
return 0;
}

22
23

Example of if- else if- else statement


Program to compare two user given number
#include <stdio.h>
int main()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
if(number1 == number2)
{ Enter two integers: 12
printf(“The numbers are equal"); 23
} The first number is smaller
else if (number1 > number2)
{
printf(“The first number is larger");
}
else
{
printf(“The first number is smaller");
}
return 0;
}
24

Nested if-else statements


General form Another form

if (condition) if (condition)
{ {
statement; if (condition)
} {
statement;
else }
{ else
if (condition) {
{ statement;
statement; }
} }

else else
{ {
statement; statement;
}
} }
25

Example of Nested if-else statements


Program to compare two user given number
#include <stdio.h>
int main()
{
int number;
printf(“Enter an integer: “);
scanf(“%d”, &number);

if (number>0){
if (number%2==0){
printf(“You entered an even positive number”);
}
else{
printf(“You entered an odd positive number”);
}
}
else{
printf(“The number you entered is not positive”);
}
return 0;
}
Use of more than one conditions in one if statement

Operator Meaning Example


&& And if(num>0&&num%2==0) Will be executed if
both statements are
true
|| Or if(num>0||num%2==0) Will be executed if any
of the conditions are
true

a b a && b a || b
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1

26
Exercise #

Write a Program to check that whether a number is


positive or negative.

Instructions:
Number must be taken as input.
Number may be integer or float type.
You can use nested if-else or if-else if-else statement.

27
ANSWER

Nested if-else statement if-else if-else statement

28
Assignments:

1. Write a C program to find the smallest of 3 integers taken


as input using nested if-else statement .

2. Write a C program to find that whether a Year is leap or


not.

29
Thank you

30

You might also like