c programming slide c03

44
C-PROGRAMMING SLIDE-3 PREPARED BY:- PRADEEP DWIVEDI(pur. B.TECH-IT) FROM HINDUSTAN COLLEGE OF SCIENCE &TECHNOLOGY MOB-+919027843806 [email protected] 5/11/22 PRADEEP DWIVEDI (pur.B.TECH-IT) 1

Upload: pradeep-dwivedi

Post on 19-May-2015

2.366 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

C-PROGRAMMING SLIDE-3PREPARED BY:-

PRADEEP DWIVEDI(pur. B.TECH-IT)

FROM

HINDUSTAN COLLEGE OF SCIENCE &TECHNOLOGY

MOB-+919027843806

[email protected]

1

Page 2: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

C-3

2

TOPIC:-Operator and expressionConditional or decisional construct.

Page 3: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

OPERATOR

3

An operator is a symbol that tells the computer to perform certain mathematical and logical manipulation.

In c we have three classes of operators.

Unary operator binary operator turnery operator

(single operand) (two operand) (three operand)

Page 4: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

OPERATOR

4

We have following type of operator1. Arithmetic operators2. Relational operators3. Logical operators4. Assignment operator5. Increment and decrement operator6. Conditional operator7. Bitwise operator8. Special operator.

Page 5: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

ARITHMETIC OPERATOR

5

OPERATOR MEANING

+ Addition (unary plus)

- Subtraction (unary minus)

* Multiplication

/ Division (quotient)

% Modulus (remainder)

Page 6: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

ARITHMETIC OPERATOR

6

Integer arithmetic:-Let a=14,b=4;Then a-b=10; a+b=18; a*b=56; a/b=3;(quotient) a%b=2(remainder)

Page 7: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

ARITHMETIC OPERATOR

7

Note:-when we have lesser numerator then the denominator that time in the case of division operator answer is always 0. and in case of remainder operator answer is same numerator.

6/7=0; -6/-7=0; 6%7=6; -6%-7=-6;Note:-during modulo division the sign of

result is always the sign of first operand (the divident)

-14%3=-2; -14%-3=-2; 14%-3=2;

Page 8: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

ARITHMETIC OPERATOR

8

Real arithmetic:- x=6.0/7.0=.05743; y=1.0/3.0=0.333333; z=-2.0/3.0=-0.666667Note:- the operator % can not be use real operands.Mixed mode arithmetic:-When one of the operand is real and the other is integer

the expression is called a mixed mode arithmetic expression.

If either of operand is of the real type than the only real operation is performed and the result is always a real number.

15/10.0=1.5;

Page 9: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

PRECEDENCE OF ARITHMETIC OPERATOR

9

Higher priority (*,/%)Lower priority (+,-)When we have more than one operator of

same priority that time the priority does not matter and associativity comes in picture. And it usually work left to right.

Page 10: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

Prog5:-

10

/* write a program to convert a temp farenhiet to celcius*/ #include<stdio.h> #include<conio.h> void main() { int fa; float c; clrscr(); printf("Enter the farenhite value"); scanf("%d",&fa); c=(fa-32)*5.0/9.0; printf("celcius=%f",c); getch(); }

Page 11: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

Prog6:-

11

/*write a program to convert a given number of days into months and days*/

#include<stdio.h> #include<conio.h> void main() { int months,days; printf("Enter days\n"); scanf("%d",&days); months=days/30; days=days%30; printf("months=%d days=%d",months,days); getch(); }

Page 12: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

RELATIONAL OPRATOR

12

OPRATORS MEANINGS

< Is less than

<= Is less than or equal to

> Is greater than

>= Is greater than or equal to

== Is equal to

!= Is not equal to

Page 13: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

RELATIONAL OPRATOR

13

Eg:4.5<=10 (true)4.5<-10 (false)-35>=0 (false)10<7+5 (true)

Page 14: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

RELATIONAL OPERATOR COMLEMENT

14

ACTUAL ONE SIMPLIFIED ONE

!(x<y) X>=y

!(x>y) X<=y

!(x!=y) X==y

!(x<=y) X>y

!<x>=y) X<y

!(x==y) X!=y

Page 15: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

LOGICAL OPERATOR

15

We have three logical operatorsLogical AND(&&)Logical OR(!!)Logical NOT(!)In the case of and operator the answer will

be true if both condition’s are true. Otherwise the answer will always be false.

In the case of or operator the answer will be false if the both condition are false otherwise the answer will always be true.

Page 16: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

NOTE

16

Relative precedence of the relational and logical operands is as follows.

Highest ! > >= < <= == != &&Lowest !!It is important to remember this when we

use these operators is compound expressions.

Page 17: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

ASSIGNMENT OPERATOR

17

Assignment operator are use to assign the result of an expression to a variable.

We have seen the usual assignment operator ‘=‘ . In addition c has a set of ‘shorthand’ assignment operator of the form-

Is equivalent to- v= v op (exp);

V op =exp;

Page 18: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

TABLE- SHORT HAND ASSIGNMENT OPERATOR

18

STATEMENT WITH SIMPLE ASSIGNMENT OPERATOR

STAEMENT WITH SHORT HAND OPERTOR

a=a+1 a+=1

a=a-1 a-=1

a=a*(n+1) a*=n+1

a=a/(n+1) a/=n+1

a=a%b a%=b

Page 19: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

CONDITIONAL/DECISIONAL CONSTRUCT

19

When we want a selective execution of a program or want to make some decision that time we used conditional or decisional construct.

C supports following types of decisional construct:-

1. if statement2. switch statement3. conditional operator statement4. go to statement

Page 20: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

THE IF STATEMENT

20

The if statement may be implemented in different forms depending on the complexity of conditions to be tested.

the different forms are-i. simple if statement.ii. if…………..else statement.iii. nested if…….else statement.iv. else if ladder.

Page 21: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

SIMPLE if STATEMENT

21

SYNTAX:-if(test-expression)

{

statement-block;

}statement-x;

Page 22: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

SIMPLE if STATEMENT(flow chart)

22

Page 23: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

THE if………else STATEMENT

23

SYNTAX:-if(test-expression){true-block statement(s)}else{false-block statement(s)}statement-x;

Page 24: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

THE if………else STATEMENT(flow chart)

24

Page 25: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

NEXTING OF if….else STATEMENTS

25

SYNTAX:-

if(test condition-1)

{

if(test condition-2)

{

statement-1;

}

else

{

statement-2;

}

}

else

{

statement-3;

}

statement-x;

Page 26: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

NEXTING OF if….else STATEMENTS(flow chart)

26

Page 27: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

THE else if LADDER

27

Syntax:-If(condition-1)

statetement-1;else if(condition-2)

statement-2;else if(condition-3) statement-3;

else if(condition-n)statement-n;

elsedefault -statement;

statement-x;

Page 28: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

THE else if LADDER(flow chart)

28

Page 29: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

SOME POINTS

29

When we have only one condition to check that time we should use the if ……else construct.

but if we have multiple condition to check that time we can also use the if ….else but that form is known as nested if…else.

when we have the true condition that time if block will execute otherwise only else block will execute.

if we have only one statement inside the if or else the curly braces are optional otherwise mandatory.

if we don’t want that anything should be happen in false condition that time else is also optional.

Page 30: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

Prog 7

30

/*write a program to compare two number*/#include<stdio.h>#include<conio.h>main(){int a,b;clrscr();printf("Enter two number\n");scanf("%d%d",&a,&b);if(a==b){printf("both numbers are equal");}else{printf("both numbers are not equal");}getch();return 0;}

Page 31: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

Prog 8

31

/*(use of else if ladder) write a prog to check the entered character is vowel or consonant*/#include<stdio.h>#include<conio.h>void main(){char chr;clrscr();printf("enter a character \n");scanf("%c",&chr);if(chr=='a')printf("the vowel is: %c",chr);else if(chr=='e')printf("the vowel is: %c",chr);else if(chr=='i')printf("the vowel is: %c",chr);else if(chr=='o')printf("the vowel is: %c",chr);else if(chr=='u')printf("the vowel is: %c",chr);elseprintf("the consonant is: %c",chr);getch();}

Page 32: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

Prog9

32

/*write a program to check the member is voter or not*/#include<stdio.h>#include<conio.h>void main(){int age;clrscr();printf("Enter the age of member");scanf("%d",&age);if(age<18)printf("member is not a voter");else printf("member is voter");getch();}

Page 33: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

THE switch STATEMENT

33

switch case is used when we have multiple condition to check.

the switch statement tests the value of a given variable(or expression) against a list of case values and when a match is found, a block of statements associated with that case is executed.

the syntax of switch statement is as shown in next page.

Page 34: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

THE switch STATEMENT

34

switch(expression){case values-1:block-1;break;case value-2:block-2;break;default:default-block;break;}statement-x;

Page 35: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

THE switch STATEMENT

35

the expression is an integer expression or characters.

value-1,value-2,……are constant or constant expressions and are known as case labels.

break is a keyword with the help of it we move out from the switch statement.

default is an optional case. when present , it will be executed if the value of the expression does not match with any of the case values. if not present, no action takes place if all matches fail and the control goes to the statement-x.

Page 36: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

FLOW CHART-selection process of switch statement

36

Page 37: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

RULES FOR SWITCH STATEMENT

37

the switch expression must be an integral type.case labels must be unique. no two labels can have

the same value.case labels must end with colon.the break statement transfers the control out of the

switch statement.the default label is optional . if present , it will be

executed when the expression does not find a matching case labels.

there can be at most one default label.the default may be placed anywhere but usually

placed at the end.it is permitted to nest switch statement.

Page 38: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

prog10

38

/*write a program by using switch case to make a calculator*/

#include<stdio.h>#include<conio.h>void main(){int a,b,c,option;clrscr();printf("Enter two numbers\n");scanf("%d%d",&a,&b);printf("Enter the option which do you

wants\n");printf("1.Addition\n");printf("2.subtraction\n");printf("3.multiplication\n");printf("4.division\n");scanf("%d",&option);switch(option)

/*write a program by using switch case to make a calculator*/

#include<stdio.h> #include<conio.h> void main() { int a,b,c,option; clrscr(); printf("Enter two numbers\n"); scanf("%d%d",&a,&b); printf("Enter the option which do

you wants\n"); printf("1.Addition\n"); printf("2.subtraction\n"); printf("3.multiplication\n"); printf("4.division\n"); scanf("%d",&option); switch(option)

Page 39: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

TURNARY OPERATORY

39

This is also termed as conditional operator or

The ?:operator.SYNTAX:-

The conditional expression is evaluated first. If the result is non-zero, expression is evaluated and is returned as the value of conditional expression.

Otherwise, expression2 is evaluated and its value is returned.

conditional expression ? expression1:expression2

Page 40: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

prog11

40

#include<stdio.h>#include<conio.h>void main(){int num1,num2,max;clrscr();printf("please insert the value of num1 and num2\n");scanf("%d%d",&num1,&num2);max=(num1>num2)?num1:num2;printf("the maximum number is: %d",max);getch();}

Page 41: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

UNARY OPERATOR

41

To work on single operand.1. Increment operator(++)2. Decrement operator(--) Unary operator can be represented on

following ways:-i. Postfix increment (m++)ii. Prefix increment (++m)iii. Post fix decrement (m--)iv. Prefix decrement (--m)

Page 42: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

UNARY OPERATOR

42

Postfix:- If we use postfix increment or decrement

that time we first assign or print the value and then increment the value.

Prefix:-In prefix increment or decrement we first

increment or decrement the value and then assign or print the value.

Note:-The precedence and associatively of ++

and -- operators are the same as those of unary + and unary -

Page 43: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)

prog12

43

/* Demo for unary operators */#include<stdio.h>#include<conio.h>void main(){int a,b;clrscr();a=10;b=a++;printf("the value of a and b: %d %d",a,b);getch();}

Page 44: C programming slide c03

Wednesday, April 12, 2023

PRADEEP DWIVEDI (pur.B.TECH-IT)44

THANK YOU