Successfully reported this slideshow.
Upcoming SlideShare
×

# C programming slide c03

3,080 views

Published on

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

### C programming slide c03

1. 1. C-PROGRAMMING SLIDE-3<br /> PREPARED BY:-<br />PRADEEP DWIVEDI(pur. B.TECH-IT)<br /> FROM <br />HINDUSTAN COLLEGE OF SCIENCE &TECHNOLOGY<br />MOB-+919027843806<br />E-MAIL-pradeep.it74@gmail.com<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />1<br />
2. 2. C-3<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />2<br />TOPIC:-<br />Operator and expression<br />Conditional or decisional construct.<br />
3. 3. OPERATOR<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />3<br />An operator is a symbol that tells the computer to perform certain mathematical and logical manipulation.<br />In c we have three classes of operators. <br />Unary operator binary operator turnery operator<br />(single operand) (two operand) (three operand) <br />
4. 4. OPERATOR<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />4<br />We have following type of operator<br />Arithmetic operators<br />Relational operators<br />Logical operators<br />Assignment operator<br />Increment and decrement operator<br />Conditional operator<br />Bitwise operator<br />Special operator.<br />
5. 5. ARITHMETIC OPERATOR<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />5<br />
6. 6. ARITHMETIC OPERATOR<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />6<br />Integer arithmetic:-<br />Let a=14,b=4;<br />Then a-b=10;<br />a+b=18;<br /> a*b=56;<br /> a/b=3;(quotient)<br />a%b=2(remainder)<br />
7. 7. ARITHMETIC OPERATOR<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />7<br />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.<br /> 6/7=0; -6/-7=0;<br /> 6%7=6; -6%-7=-6;<br />Note:-during modulo division the sign of result is always the sign of first operand (the divident)<br /> -14%3=-2;<br /> -14%-3=-2;<br /> 14%-3=2;<br />
8. 8. ARITHMETIC OPERATOR<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />8<br />Real arithmetic:-<br /> x=6.0/7.0=.05743;<br /> y=1.0/3.0=0.333333;<br /> z=-2.0/3.0=-0.666667<br />Note:- the operator % can not be use real operands.<br />Mixed mode arithmetic:-<br />When one of the operand is real and the other is integer the expression is called a mixed mode arithmetic expression.<br />If either of operand is of the real type than the only real operation is performed and the result is always a real number.<br />15/10.0=1.5;<br />
9. 9. PRECEDENCE OF ARITHMETIC OPERATOR<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />9<br />Higher priority (*,/%)<br />Lower priority (+,-)<br />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.<br />
10. 10. Prog5:-<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />10<br />/* write a program to convert a temp farenhiet to celcius*/<br /> #include<stdio.h><br /> #include<conio.h><br /> void main()<br /> {<br />intfa;<br /> float c;<br />clrscr();<br />printf("Enter the farenhite value");<br />scanf("%d",&fa);<br /> c=(fa-32)*5.0/9.0;<br />printf("celcius=%f",c);<br />getch();<br />}<br />
11. 11. Prog6:-<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />11<br />/*write a program to convert a given number of days into months and days*/<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br />intmonths,days;<br />printf("Enter days ");<br />scanf("%d",&days);<br />months=days/30;<br />days=days%30;<br />printf("months=%d days=%d",months,days);<br />getch();<br />}<br />
12. 12. RELATIONAL OPRATOR<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />12<br />
13. 13. RELATIONAL OPRATOR<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />13<br />Eg:<br />4.5<=10 (true)<br />4.5<-10 (false)<br />-35>=0 (false)<br />10<7+5 (true)<br />
14. 14. RELATIONAL OPERATOR COMLEMENT<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />14<br />
15. 15. LOGICAL OPERATOR<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />15<br />We have three logical operators<br />Logical AND(&&)<br />Logical OR(!!)<br />Logical NOT(!)<br />In the case of and operator the answer will be true if both condition’s are true. Otherwise the answer will always be false.<br />In the case of or operator the answer will be false if the both condition are false otherwise the answer will always be true. <br />
16. 16. NOTE<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />16<br />Relative precedence of the relational and logical operands is as follows.<br />Highest !<br /> > >= < <=<br /> == !=<br /> &&<br />Lowest !!<br />It is important to remember this when we use these operators is compound expressions.<br />
17. 17. ASSIGNMENT OPERATOR<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />17<br />Assignment operator are use to assign the result of an expression to a variable.<br />We have seen the usual assignment operator ‘=‘ . In addition c has a set of ‘shorthand’ assignment operator of the form-<br />Is equivalent to-<br /> v= v op (exp);<br />V op =exp;<br />
18. 18. TABLE- SHORT HAND ASSIGNMENT OPERATOR<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />18<br />
19. 19. CONDITIONAL/DECISIONAL CONSTRUCT<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />19<br />When we want a selective execution of a program or want to make some decision that time we used conditional or decisional construct.<br />C supports following types of decisional construct:-<br />if statement<br />switch statement<br />conditional operator statement<br />go to statement<br />
20. 20. THE IF STATEMENT<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />20<br />The if statement may be implemented in different forms depending on the complexity of conditions to be tested.<br />the different forms are-<br />simple if statement.<br />if…………..else statement.<br />nested if…….else statement.<br />else if ladder.<br />
21. 21. SIMPLE if STATEMENT<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />21<br />SYNTAX:-<br /> if(test-expression)<br />{<br />statement-block;<br />}<br />statement-x;<br />
22. 22. SIMPLE if STATEMENT(flow chart)<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />22<br />
23. 23. THE if………else STATEMENT<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />23<br />SYNTAX:-<br />if(test-expression)<br />{<br />true-block statement(s)<br />}<br />else<br />{<br />false-block statement(s)<br />}<br />statement-x;<br />
24. 24. THE if………else STATEMENT(flow chart)<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />24<br />
25. 25. NEXTING OF if….else STATEMENTS<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />25<br />SYNTAX:-<br />if(test condition-1)<br />{<br /> if(test condition-2)<br /> {<br /> statement-1;<br /> }<br /> else<br /> {<br /> statement-2;<br /> }<br /> }<br /> else <br /> {<br /> statement-3;<br /> }<br /> statement-x;<br />
26. 26. NEXTING OF if….else STATEMENTS(flow chart)<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />26<br />
27. 27. THE else if LADDER<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />27<br />Syntax:-<br />If(condition-1)<br /> statetement-1;<br /> else if(condition-2)<br /> statement-2;<br /> else if(condition-3)<br /> statement-3;<br /> else if(condition-n)<br /> statement-n;<br /> else<br /> default -statement;<br />statement-x;<br />
28. 28. THE else if LADDER(flow chart)<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />28<br />
29. 29. SOME POINTS<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />29<br />When we have only one condition to check that time we should use the if ……else construct.<br />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.<br />when we have the true condition that time if block will execute otherwise only else block will execute.<br />if we have only one statement inside the if or else the curly braces are optional otherwise mandatory.<br />if we don’t want that anything should be happen in false condition that time else is also optional.<br />
30. 30. Prog 7<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />30<br />/*write a program to compare two number*/<br />#include<stdio.h><br />#include<conio.h><br />main()<br />{<br />inta,b;<br />clrscr();<br />printf("Enter two number ");<br />scanf("%d%d",&a,&b);<br />if(a==b)<br />{<br />printf("both numbers are equal");<br />}<br />else<br />{<br />printf("both numbers are not equal");<br />}<br />getch();<br />return 0;<br />}<br />
31. 31. Prog 8<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />31<br />/*(use of else if ladder) write a prog to check the entered character is vowel or consonant*/<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br />char chr;<br />clrscr();<br />printf("enter a character ");<br />scanf("%c",&chr);<br />if(chr=='a')<br />printf("the vowel is: %c",chr);<br />else if(chr=='e')<br />printf("the vowel is: %c",chr);<br />else if(chr=='i')<br />printf("the vowel is: %c",chr);<br />else if(chr=='o')<br />printf("the vowel is: %c",chr);<br />else if(chr=='u')<br />printf("the vowel is: %c",chr);<br />else<br />printf("the consonant is: %c",chr);<br />getch();<br />}<br />
32. 32. Prog9 <br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />32<br />/*write a program to check the member is voter or not*/<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br />int age;<br />clrscr();<br />printf("Enter the age of member");<br />scanf("%d",&age);<br />if(age<18)<br />printf("member is not a voter");<br />else <br />printf("member is voter");<br />getch();<br />}<br />
33. 33. THE switch STATEMENT<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />33<br />switch case is used when we have multiple condition to check.<br />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.<br />the syntax of switch statement is as shown in next page. <br />
34. 34. THE switch STATEMENT<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />34<br />switch(expression)<br />{<br />case values-1:<br />block-1;<br />break;<br />case value-2:<br />block-2;<br />break;<br />default:<br />default-block;<br />break;<br />}<br />statement-x;<br />
35. 35. THE switch STATEMENT<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />35<br />the expression is an integer expression or characters.<br />value-1,value-2,……are constant or constant expressions and are known as case labels.<br />break is a keyword with the help of it we move out from the switch statement.<br />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.<br />
36. 36. FLOW CHART-selection process of switch statement<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />36<br />
37. 37. RULES FOR SWITCH STATEMENT<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />37<br />the switch expression must be an integral type.<br />case labels must be unique. no two labels can have the same value.<br />case labels must end with colon.<br />the break statement transfers the control out of the switch statement.<br />the default label is optional . if present , it will be executed when the expression does not find a matching case labels.<br />there can be at most one default label.<br />the default may be placed anywhere but usually placed at the end.<br />it is permitted to nest switch statement. <br />
38. 38. prog10<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />38<br />/*write a program by using switch case to make a calculator*/<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br />inta,b,c,option;<br />clrscr();<br />printf("Enter two numbers ");<br />scanf("%d%d",&a,&b);<br />printf("Enter the option which do you wants ");<br />printf("1.Addition ");<br />printf("2.subtraction ");<br />printf("3.multiplication ");<br />printf("4.division ");<br />scanf("%d",&option);<br />switch(option)<br />/*write a program by using switch case to make a calculator*/<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br />inta,b,c,option;<br />clrscr();<br />printf("Enter two numbers ");<br />scanf("%d%d",&a,&b);<br />printf("Enter the option which do you wants ");<br />printf("1.Addition ");<br />printf("2.subtraction ");<br />printf("3.multiplication ");<br />printf("4.division ");<br />scanf("%d",&option);<br />switch(option)<br />
39. 39. TURNARY OPERATORY<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />39<br />This is also termed as conditional operator or<br />The ?:operator.<br />SYNTAX:-<br />The conditional expression is evaluated first. If the result is non-zero, expression is evaluated and is returned as the value of conditional expression.<br />Otherwise, expression2 is evaluated and its value is returned.<br />conditional expression ? expression1:expression2<br />
40. 40. prog11<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />40<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br />int num1,num2,max;<br />clrscr();<br />printf("please insert the value of num1 and num2 ");<br />scanf("%d%d",&num1,&num2);<br />max=(num1>num2)?num1:num2;<br />printf("the maximum number is: %d",max);<br />getch();<br />}<br />
41. 41. UNARY OPERATOR<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />41<br />To work on single operand.<br />Increment operator(++)<br />Decrement operator(--)<br />Unary operator can be represented on following ways:-<br />Postfix increment (m++)<br />Prefix increment (++m)<br />Post fix decrement (m--)<br />Prefix decrement (--m)<br />
42. 42. UNARY OPERATOR<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />42<br />Postfix:- <br />If we use postfix increment or decrement that time we first assign or print the value and then increment the value.<br />Prefix:-<br />In prefix increment or decrement we first increment or decrement the value and then assign or print the value.<br />Note:-<br />The precedence and associatively of ++ and -- operators are the same as those of unary + and unary - <br />
43. 43. prog12<br />Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />43<br />/* Demo for unary operators */<br />#include<stdio.h><br />#include<conio.h><br />void main()<br />{<br />inta,b;<br />clrscr();<br />a=10;<br />b=a++;<br />printf("the value of a and b: %d %d",a,b);<br />getch();<br />}<br />
44. 44. Wednesday, August 25, 2010<br />PRADEEP DWIVEDI (pur.B.TECH-IT)<br />44<br /> THANK YOU<br />