C Notes Module-2
C Notes Module-2
Module-2
Operators
Operators are the basic components of C programming. They are symbols that represent some kind
of operation, such as mathematical, relational, bitwise, conditional, or logical computations, which
are to be performed on values or variables. The values and variables used with operators are called
operands.
Syntax:
if (condition)
Statements-X;
In the above syntax, the given condition will be evaluated for TRUE or
FALSE by machine, if it is True, then the true block statements are executed;
otherwise, no.
#include<stdio.h>
void main()
{
int n1,n2;
printf(“Enter any two numbers”);
scanf(“%d%d”,&n1,&n2);
if (n2>n1)
{
if (n1>n2)
{
More Examples:
else
Statement-X;
In the above syntax, the given condition will be evaluated for TRUE or FALSE by
machine, if it is True, then the True Block of Statements are executed;
otherwise, False Block of Statements are executed.
Example1:C program to find largest of two numbers using simple if … else statement.
#include<stdio.h>
void main()
{
int n1,n2;
clrscr();
printf(“Enter any two numbers”);
scanf(“%d%d”,&n1,&n2);
if (n1>n2)
{
else
{
/*C program to find area of circle if the user name is your First Name and password is 999*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
else
{
char ch;
clrscr();
printf("\n Entera character:");
scanf("%c",&ch);
if (ch>='A' && ch<='Z')
printf("\n %c is uppercase letter",ch);
else
printf("\n %c is not uppercase letter",ch);
Syntax:
if (exprn-1)
{
if (exprn-2)
{
Statement1;
}
else
{
Statement2;
}
}
else
{
Statement3;
}
Statement-X;
Example: To find largest of three numbers by using nested if statement
# include <stdio.h>
void main( )
{
int a, b, c, large;
clrscr();
printf(“Enter three numbers”);
scanf(“%d%d%d”, &a, &b, &c);
if (a>b)
{
if (a>c)
large=a;
else
large=c;
}
else
{
if (b>c)
large=b;
else
large=c;
}
printf(“\n %d is large number”, large);
}
4. The else .. if ladder or cascaded if statement: It is another multi way selection statement
available in C to select one from many alternative. It helps the programmer to use another if
else statement only within the else part of the previous if else statement. So, it is called else if
ladder or cascaded if statement. The syntax, flowchart and examples are as follow.
Syntax:
if (expression-1)
statement1;
else if (expression -2)
statement2;
else if (expression -3)
statement3;
else if (expression -n)
statement-n;
else
default statement;
statement-X;
In the above syntax, the expression-1 will be evaluated by machine for True or False,
if it is True then Statement-1 executes followed by statement-x. Otherwise; other expressions
will be evaluated for True/False to execute concerned block for the True condition. If, all the
expressions are False then default statement will be executed by machine.
Example: C program to display the grades obtained by student based on the average marks
scored.
Average Marks Grade
80 to 100 Honors
60 to 79 First Division
50 to 59 Second Division
40 to 49 Third Division
0 to 39 Fail
# include <stdio.h>
void main( )
{
float avg;
printf(“\n Enter average marks scored by student”);
scanf(“%f”, &avg);
if (avg>=80)
printf(“\n Honors!!”);
else if (avg>=60)
printf(“\n First Division”);
else if (avg>=50)
printf(“\n Second Division”);
else if (avg>=40)
printf(“\n Third Division”);
else
printf(“\n Fail”);
}
d=(b*b)-(4*a*c);
if(d>0)
{
else if (d = =0)
{
else
{
5. The Switch Statement: It is another multi way statement. It helps the programmer to
select one from many alternatives. It is mainly used by programmers to provide menu
options for the end users of a program to select one from displayed menu options.
Syntax:
Switch (value)
{
case value1:
statements block1;
break;
case value2:
statements block2;
break;
case value3:
statements block3;
break;
…
…
…
default:
default statement;
break;
}
Statement-X;
In the above syntax, the passed value will be compared against all the case values one after
another from top to bottom, wherever it matches that associated block of statements executes
; otherwise, default statement executes.
Limitations of using switch statement:
Example:
int ch;
clrscr();
printf("\n Enter your choice [1 2 3]?:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n You have selected first choice");
break;
case 2:
printf("\n You have selected second choice");
break;
case 3:
printf("\n You have selected third choice");
break;
default:
printf("\a Invalid choice!");
break;
}
getch();
}
Output 1:
Output 2:
Write a c program to display the color names like Red, Green, Blue by reading first
character of color as input.
Output:
1.
Enter first character of color [R/G/B]? : b
BLUE
2.
Enter first character of color [R/G/B]? : G
GREEN
3.
Enter first character of color [R/G/B]? : i Invalid choice
int digit;
printf("\n Enter a digit (0-9):");
scanf("%d",&digit);
switch(digit)
{
case 0:
printf("Zero");
break;
case 1:
printf("One");
break;
case 3:
printf("Three");
break;
/* Here write the code for remaining cases 4, 5, 6,7,8*/
case 9:
printf("Nine");
break;
default:
printf("\a Invalid input!");
break;
} }
char ch;
printf("\n Enter a character :");
scanf("%c",&ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("Vowel");
break;
default:
printf("\a Not Vowel");
break;
}
Write a c program to find area of circle, area of triangle and area of rectangle by using switch
statement to display menu options for the users to select any one to work accordingly.
*To find area of geometrical figures*/
#include<stdio.h>
#include<conio.h>
void main()
{
int ch;
float r,l,b,breadth,height,area;
printf("\n 1. Area of Circle");
printf("\n 2. Area of Circle");
printf("\n 3. Area of Circle");
printf("\n Enter your choice[1 2 3]?:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter radius of circle");
scanf("%f",&r);
area=3.142*r*r;
printf("\n Area of circle is %5.2f",area);
break;
case 2:
printf("Enter breadth and height of triangle");
scanf("%f%f",&breadth,&height);
area=0.5*breadth*height;
printf("\n Area of triangle is %5.2f",area);
break;
case 3:
printf("Enter length and breadth of rectangle");
scanf("%f%f",&l,&b);
area=l*b;
printf("\n Area of rectangle is %5.2f",area);
break;
default:
printf("\a Invalid choice!");
break;
}
1. The while loop: It is one of the entry controlled loop statement. It repeats the
given true-block of statements repeatedly till the given condition is true.
Whenever, the condition becomes false then loop terminates.
The while loop is also called as pre-test loop or event controlled loop.
The syntax, flowchart and examples are as follow.
Syntax:
while (expression)
{
true - block loop
true - block loop statements;
statements
}
Statements-X;
Statement-X
In the above syntax, the given expression will be checked for True or
False. If, it is True, then loop statements are executed repeatedly till the
condition becomes False.
Examples:
To display 1 to 10 numbers.
/* Example for while loop */
#include<stdio.h>
#include<conio.h>
void main()
{
O/p:
int i; 1
clrscr(); 2
i=1; 3
while (i<=10) .
.
{
10
printf(“\n %d”,i);
i=i+1;
}
getch();
}
Write a C program to find sum of odd numbers, even numbers and average of all numbers
between 1 to n.
/*To find sum of even, odd and all nos with average of 1 to n numbers*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,esum=0,osum=0,sum=0;
float avg;
clrscr();
printf("Enter n:");
scanf("%d",&n);
i=1;
while (i<=n)
{
if (i%2==0)
esum=esum+i; O/p:
else Enter the value of n: 5
osum=osum+i; Even Sum =6 Odd Sum=9 Sum=15 Average=3.00
sum=sum+i;
i++;
}
avg=(float)sum/n;
printf("\n Even Sum=%d Odd Sum=%d Sum=%d Average=%f", esum, osum, sum, avg);
getch();
}
/* To find factorial of n. */
#include<stdio.h>
#include<conio.h>
void main() O/p:
{ Enter the value of n: 5
int i, n, prod=1; Factorial of 5 is 120
clrscr();
printf(“Enter the value of n: ”);
scanf(“%d”, &n);
i=1;
while (i<=n)
{
prod=prod*i;
i=i+1;
}
printf(“\n Factorial of %d is %d”, n, prod);
getch();
}
O/p:
Enter any two numbers m and n: 12 24
GCD=12 LCM=24
2) The do…while loop: It is exit controlled loop statement available in C. It helps the
programmer to repeat certain block of true statements repeatedly till the given condition is
True. Whenever, the given condition becomes False then the loop terminates.
The major difference between while and do…while is that, if the given condition is
False for the first time, then the body of the do while works at least once, whereas no
execution of while loop.
The do while is also called as post test or event controlled loop.
The syntax, flowchart and examples are as follow.
Syntax
do
} while (expression);
true block of loop
statement-X; statements
In the above syntax, the statements from
true block of loop statements are executed repeatedly
till the given expression is true.
To display 1 to n numbers.
/* To display 1 to n numbers */ #include<stdio.h> #include<conio.h> Statement-X
void main()
{
int i,n; clrscr();
printf(“Enter the value of n: ”); scanf(“%d”,&n);
i=1;
do
{
printf(“\n %d”,i); i=i+1;
} while (i<=n);
getch();
}
O/p:
Enter the value of n: 100 1
2
3
.
. 100
O/p:
Enter the value of n: 5 Sum is 15 Average=3.00
/* To find factorial of n. */
#include<stdio.h>
#include<conio.h>
void main()
O/p:
{
Enter the value of n: 5
int i, n, prod=1;
Factorial of 5 is 120
clrscr();
printf(―Enter the value of n: ‖);
scanf(―%d‖, &n);
i=1;
do
{
prod=prod*i;
i=i+1;
}while (i<=n);
printf(―\n Factorial of %d is %d‖, n, prod);
getch();
}
Example: Example:
program to find factorial of n program to find factorial of n
i=1,prod=1; i=1,prod=1;
clrscr(); clrscr();
printf(― Enter n:‖); printf(― Enter n:‖);
scanf(―%d‖,&n); scanf(―%d‖,&n);
5
while(i<=n) do
{ {
prod=prod*i; prod=prod*i;
i++; i++;
} }while(i<=n);
printf(―\n factorial is %d‖, prod); printf(―\n factorial is %d‖, prod);
It is also called as pre test or event It is also called as post test or event
6
controlled loop. controlled loop.
2) The for loop: It is counter controlled loop statement available in C. It helps the
programmer to repeat given true bock of loop statements repeatedly till the given expression
is True. Whenever, the given condition becomes False then loop gets terminated. If the
number of iterations well known by programmers then they prefer the use of for loop
statement.
Syntax:
Entry
for (initialization; expression; increment/decrement)
{
for i = 1 to n step 1 False
True block of loop statements;
True
}
Statement-x;
Statements-X
Where,
Initialization Counter will be assigned with initial value. e.g. i=1,i=0,etc.
Expression The conditional expression on which number of iterations depends. E.g. i<n,
i<=n, etc.
increment/decrement increment or decrement expression for the counter. E.g. i++, j++, i--,
j--,etc.
To display 1 to 10 numbers. O/p:
#include<stdio.h> 1
#include<conio.h> 2
void main() 3
.
{
.
int i; 10
clrscr();
i=1;
for(i=1;i<=10;i++)
{
printf(―\n %d‖,i);
}
getch();
}
To display 1 to n numbers.
/* To display 1 to n numbers */ Enter the value of n: 100
#include<stdio.h> 1
#include<conio.h> 2
void main() 3
{ .
int i,n; .
clrscr(); 100
printf(―Enter the value of n: ‖);
scanf(―%d‖,&n);
i=1;
for(i=1;i<=n;i++)
{
printf(―\n %d‖,i);
}
getch();
}
/* To find factorial of n. */
#include<stdio.h>
#include<conio.h>
void main()
O/p:
{
Enter the value of n: 5
int i, n, prod=1;
Factorial of 5 is 120
clrscr();
printf(―Enter the value of n: ‖);
scanf(―%d‖, &n);
i=1;
for(i=1;i<=n;i++)
{
prod=prod*i;
}
printf(―\n Factorial of %d is %d‖, n, prod);
getch();
}
Nested loops: The programmer can use one loop statement within another loop statement and
is called nested loop. It informs the machine to do repetitive work repeatedly. It is used by
programmer to solve complex problems that include complex repetitive steps. The ANSI C
supports 32 times of nesting of loops.
Examples
i=1;
while (i<=5) /* outer loop works for 5 times */
{
j=1;
while (j<=10) /*every time inner loop works for 10 times*/
{
printf(―\t VTU, Belagavi‖);
j++;
}
i++;
printf(―\n‖);
}
Output: The given message ―VTU, Belagavi‖ will be displayed for 5 X 10 i.e. 50 times.
Know it :
As we know, the loop control statements like while, do while and for are
used by programmers to repeat certain block of statements repeatedly till the
given condition is True. However, it is necessary for the programmers to exit
from loop body before the condition satisfies or to skip certain statements from
loop body based on certain conditions. This is possible by making the use of
break and continue statements available in C. These are also called as loop
interrupt statements.
In the given syntax, whenever the given if (condition) becomes True then
loop terminates followed by statement-x.
Examples:
Write a c program to check whether the unknown number ‗n‘ is prime or not
prime.
/* To check for prime number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,flag=1;
clrscr(); O/p:
printf("\n Enter n:");
scanf("%d",&n); Enter n: 7
for(i=2;i<=n/2;i++) 7 is prime number
{
if(n%i==0)
{ Enter n: 20
flag=0; 20 is not prime number
break;
}
}
if(flag = =1)
printf("\n %d is prime number",n);
else
printf("\n %d is not prime number",n);
getch();
}
In the above syntax, whenever the given if (condition) becomes True then
statements bock-2 are not going to execute.
Examples:
Syntax:
Statements; Statements;
label: if (expr-n)
statements; goto label;
if (expr-n) statements;
goto label; label:
statements; statements;
if (n%2==0)
printf("\n %d is even number",n);
else
printf("\n %d is odd number",n);
getch();
}
INTRODUCTION TO C PROGRAMMING
Don‘t compare yourself with anyone in this world, if you do so, you are
insulting yourself. –Alen Strike
Don‘t compare yourselves with others, instead compare yourself with your last
performance- Bill Gate
There is no use of running fast, when you are on the wrong road. So, first
choose the correct way in your life. - German Proverb
Subject / Course is difficult for the student or he/she has no interest in it. It was
forced on them by others.
―Success is the sum of small efforts, repeated day in and day out.‖ ~ Robert
Collier
―Learn from mistakes‖- Thomas Edison tried two thousand different materials
in search of a filament for the light bulb. When none worked satisfactorily, his
assistant complained, ―All our work is in vain. We have learned
nothing.‖Edison replied very confidently, ―Oh, we have come a long way and
we have learned a lot. We know that there are two thousand elements which we
cannot use to make a good light bulb.‖
INTRODUCTION TO C PROGRAMMING
When you are not practicing, then remember that, someone somewhere is
practicing for the same and when you will meet him/her, then he/she will be the
winner. – By Ed Macauley
Striving for success without hard work is like trying to harvest where you have
not planted. – By David Bly
The roots of education are bitter, but the fruits are sweat- Aristotle.
―If you try and lose then it isn't your fault. But if you don't try and we lose, then
it's all your fault.‖ ― Orson Scott Card, Ender's Game
―Yesterday is gone. Tomorrow has not yet come. We have only today. Let us
begin.‖ ― Mother Teresa
―The future belongs to those who believe in the beauty of their dreams.‖
— Eleanor Roosevelt
Dear Student,
You are talented student, only you need to do is that read more to
understand unknown things.