Operators:
The variables which are declared and defined are the operands, operated upon by operators. Operators specify what operations are to be performed on the operands.
Assignment operators:
=(equal) sign is used for assigning a value to another.
e.g. x = 10;
y = x;
Arithmetic operators:
In C there are Unary Arithmetic and Binary arithmetic operators. Unary operators operates on single operand and binary operates on two operands.
Unary Arithmetic operators (Negation):
Used to negate value of a variable, also used to specify negative number, -(minus) signed is prefixed to the number.
e.g. int x =5;
y = -x;
C does not offer unary + operator.
Increment and Decrement Operator:
There are two unary operators for incrementing and decrementing variables. These are ++ and – operators and called as increment and decrement operators. These operators increase or decrease the value of a variable on which they are operated, by 1. These two may be used as prefix or postfix and their meaning changes depending upon their usage when used as prefix, value of variable incremented/decremented before using in the expression but when used as postfix it’s value is first used in the expression and then value incremented or decremented.
e.g. --x;
x--;
int a = 0, b = 10;
a = ++b;
a = b++;
Unary operators having higher precedence than binary arithmetic operators.
Binary Arithmetic Operators:
There are five binary arithmetic operators:-
‘+’ addition
‘*’ multiplication
‘-‘ subtraction
‘/’ division
‘%’ modulus
All these operators required two operands.
e.g. z = y+x;
f = 10/3;
f = 10%3;
x =a+b-c/d*c;
x = ((a+(b-c)/d)*e;
Compound Assignment Operators:
In C compound assignment operators also available. They are +=, -=, *=, /=, and %=. Hence x =x+5; can also be written as
x +=5; or x = x*(z+y-x); is equivalent to x *= z+y-x;
Relational Operators:
Are used to make comparisons between two values. These operators require two operands. These operators are:
== equal to e.g. total == 1000;
! = not equal to e.g. ch != ’$’;
> greater than e.g. no > 15;
>= greater than equal to e.g. num1 >= 5;
< less than e.g. sum < 150;
<= less than equal to e.g. x <= 100;
Logical Operators:
Are useful in combining one or more conditions. They are: -
&& And e.g. x < 0 && y > 10 (True if both the conditions are true).
|| Or e.g. x<=0 ||z = = 0 (True if either or both conditions are true).
! Not e.g. !(x= =0) (evaluates true if the conditions is false).
Type Casting:
Explicit type conversions may be used in mixed mode expressions by type-casting a value of a particular type into desired type by using (type operator).
e.g. type expression
where expression is converted to given type.
e.g. float a = 10.0, b = 3.0, c;
c = a/b;
If an application requires int. division of two floating point numbers than following expression with type cast can be used:
c = ( int ) a / ( int ) b; or
c = ( int ) ( a/b )
Control Flow:
Are used when it is required that the flow of the program is to be changed after taking some decision or test. It specifies the order in which computations are carried out. Control flow statements are divided into those used for branching and for looping.
Branching Statements:
Control flow statements used for branching are : if-else, else –if, switch.
If-else and else-if : Are used to make decisions. The syntax of the statement is: if (expression 1)
statement 1
else
statement 2;
Expressions should always be enclosed in parenthesis. If expr 1 is true then statement1 executed and control passes to next after if construct otherwise statement 2 is executed and control passes to next statement. Statements could be simple or compound i.e. a block as:
If(ch<’0’||ch>’9’)
printf(“not a number”);
else
printf(“A number”);
}
A multi way decision making segment can be written by using if statements inside else part using else-if construct. As
if (exp1)
statement1
else if (exp2)
statement2
else if (exp3)
statement3
else statement
Thus in above example if first expression is true the whole chain is terminated and will be continued if first expression is found false. At any stage if an expression is true remaining chain is terminated.
e.g.
if (option ==1)
printf(“no one is entered”);
else if(option ==2)
printf(“no two is entered”);
else if(option ==3)
printf(“no 3 is entered”);
else
printf(“no invalid”);
if-else constructs:
Can be nested as follows:
e.g.
if(pcode ==1)
{
if (amount>1000)
discount_rate =10.0;
else
discount_rate=5.0;
}
/*program*/
# include<stdio.h>
main( )
{
int num;
scanf(“Enter number %d”,&num);
(num%2)? Printf (“Odd”):printf(“EVEN”);
}
The switch Construct:
Is a multi way decision making construct that tests whether an expression matches one of a number of constant values, and branches accordingly. The switch statement is a neater alternative to using else-if statements where multi way decision making is involved.
Switch(expression)
{
case value 1: statements;
break;
case value 2: statements;
break;
default : statements
Expressions must enclosed in parenthesis and body of the switch statements must be in braces. There may be single or multiple statements with or without braces. Values with case should be constants. The expression is evaluated and resultant is compared with each of values given with case. If any of the values matches the statements following that case, till break are executed and if none of the case matches the expressions then the statements following default are executed. Break keyword is used to delimit the scope of the statements under a particular case.
e.g.
switch (option)
{
case 1 : printf(“option 1”);
break;
case 2 : printf(“option 2”);
break;
default : printf(“invalid”);
break statement is essential for correct logical realizations of switch structure. It causes exit from switch structure after correct case statements are executed.
Looping Statements:
Looping statements in C are for, do while, while.
For loop : is a controlled from of loop. Its general format is
for (initialize; test;update)
{
statements;
- - - - - -
- - - - - -
}
The initialize part is executed only once before entering the loop. Then text is evaluated if it is false, the next statement after the for loop is executed. If test evaluates to true, after execution of the statement in the loop the update part is executed. Test evaluated again and whole process is repeated till test remains true.
E.g.
include<stdio.h>
main( )
{
int num, count,total=0;
for(count=1;count<=10;++count)
{
printf(“Enter no %d”,num);
scanf(“%d”,num);
total+=num;
}
printf(“total is %d”,total);
}
Expressions in for loop are optional but semi colons are necessary.
As for example ( ; ; )
printf(“hello”);
or int x=1;
for ( ; x<<=10; )
printf(“ %d”,x++);
While loop:
For loop is a form of a controlled loop when number of iterations performed are not known beforehand while loop can be used.
While(condition)
{
statements
……
}
statements in while loop are executed if the condition is true and till it remains true.
e.g. int x =0;
while(x<=10){
printf(“%d”,x++);
}
do while loop:
The General format is
do
{
statements
……
}
while((condition)
In while loop test is done before entering the loop but in do while loop test is done after statement in the loop are executed. Hence in do while loop statements in do –while loop are executed at least once and loop is executed as long as the test conditions are remains true.
e.g.
int num,total=0;
do
{
printf(“enter no (0 to quit)”);
scanf(“%d”,&num);
if(num==0);
break;
if(num<0)
continue;
total+=num;
}
while(i);
printf(“total of positive numbers %d”,total);
In the above example break statement is used to transfer the control to the next statement, after the loop, to provide an early exit from the loop. And continue statement is used for next iteration of enclosing loop to begin.
Programs for Chapter 2
Example1 (Finding Average Age)
#include<conio.h>
#include<stdio.h>
void main( ){
clrscr( );
int age[5];
float sum=0;
for(int i=0;i< 5;i++)
{
printf("\n Enter person : “, i+1 " age :");
scanf("%d",&age[i]);
}
for(i=0;i< 5;i++)
{
sum +=age[i];
}
printf("\n Average age : "sum/5);
getch( );
}
Example2(Finding the days and seconds from the year)
#include<stdio.h>
#include<conio.h>
void main( ){
clrscr( );
float years,secs;
printf("\nEnter your age in years :");
scanf("%d",&years);
if(years<0)
{
printf( "\nI am sorry!");
printf("\nAge can never be negative ");
}
else
{
secs=years *365 *24*60*60;
printf("\nYou are lived for :"secs);
}
getch( );
}
Example3(Big Number)#include<conio.h>
void main( )
{
clrscr( );
float a,b,c,big;
printf("\n Enter a number :");
scanf(“%d”,&a);
printf("\n Enter Second Number :");
scanf(“%d”&b);
printf("\n Enter Third Number :");
scanf(“%d”&c);
big=a;
if(b>big)
{
big=b;
}
if(c>big)
{
big=c;
}
printf("\n The largest of three numbers is : " ,big);
getch( );
}
Example 4(Switch Statement)
#include<stdio.h>
#include<conio.h>
void main(void){
clrscr( );
char ch;
printf("Enter a Character \n");
scanf(“%c”,ch);
switch(ch){
case 'A':
case 'a':
printf("A is a Vowel \n");
break;
case 'E':
case 'e':
printf("E is a Vowel \n");
break;
case 'I':
case 'i':
printf("I is a Vowel \n");
break;
case 'O':
case 'o':
printf("O is a Vowel \n");
break;
case 'U':
case 'u':
printf("U is a Vowel \n");
break;
default:
printf("Not a Vowel \n");
break;
}
getch( );
}
Example 5(Displaying Accepted Integers)
#include<stdio.h>
#include<conio.h>
void main( ){
clrscr( );
int n;
printf("\nHow many integers to be displayed :");
scanf(“%d”,&n);
for(int i=0;i<n;i++){
printf(“\n%d ”,i);
}
getch( );
}
Example 6(Do Loop)
#include<stdio.h>
#include<conio.h>
void main( ){
clrscr( );
int i=0,n;
printf("\n How many Numbers to Display :");
scanf(“%d”,&n);
do{
printf(“%d\n”,i);
i++;
}
while(i<n);
getch( );
}
Example 7(While Loop)
//Program to find Gross Common Divider (gcd)
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main( ){
clrscr( );
int a,b,x;
printf("\n Enter values of a :");
scanf(“%d”,&a);
printf("\n Enter values of b :");
scanf(“%d”,&b);
while(a%b!=0)
{
x =a%b;
a=b;
b=x;
}
printf("\n GCD of a and b :%d",b);
getch( );
}
Example 8(While Loop)
#include<stdio.h>
#include<conio.h>
void main( ){
clrscr( );
char inchar;
do {
printf("\n Enter Your Gender (M/F):");
scanf(“%c”,&inchar);
}
while(inchar!='M'&& inchar !='F');
if(inchar=='M')
printf("\n So You are Male ");
else
printf("\n So You are Female ");
getch( );
}
Example 9(Deciding whether the given year is leap year or not)
#include<conio.h>
#include<stdio.h>
void main( ){
clrscr( );
int year;
printf("\n Enter Any Year :- ");
scanf(“%d”,&year);
if((year%4==0 && year/100 !=0)||(year%400==0))
{
printf("\n" %d “,year);
printf(“\t is a Leap Year. ");
}
else{
printf("\n" %d “,year);
printf(“\t is not a Leap Year. ");
}
getch( );
}
Example10(Do While Loop)
#include<stdio.h>
#include<conio.h>
main( ) {
char name[15];
int j=0, i=0;
clrscr( );
printf("\n Enter your name ");
scanf("%s",name);
while (i<=15)
{
if((name[i]=='a'||name[i]=='A'))
j +=1;
i +=1;
}
printf("The Number of A's in your name is %d ",j);
getch( );
}
Example11 (Do While Loop)
#include<stdio.h>
#include<conio.h>
main( ){
int num=1;
do{
printf("\n%d",num);
num++;
}
while (num<=50);
getch( );
}
No comments:
Post a Comment