0% found this document useful (0 votes)
2 views27 pages

BPOPS103_Module-2

The document provides an overview of operators in C programming, detailing various types such as arithmetic, relational, logical, assignment, bitwise, unary, ternary, and special operators. It also covers type conversion, operator precedence, and decision control statements including if, if-else, and nested if statements. Additionally, it explains the structure and usage of these operators and statements with examples to illustrate their functionality.

Uploaded by

prathamesh427869
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)
2 views27 pages

BPOPS103_Module-2

The document provides an overview of operators in C programming, detailing various types such as arithmetic, relational, logical, assignment, bitwise, unary, ternary, and special operators. It also covers type conversion, operator precedence, and decision control statements including if, if-else, and nested if statements. Additionally, it explains the structure and usage of these operators and statements with examples to illustrate their functionality.

Uploaded by

prathamesh427869
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/ 27

Principles Of Programming Using C BPOPS203 Module 2

Operators in C
An operator is a symbol that tells the compiler to perform specific mathematical and logical
functions.The different operators supported in „C‟ are:

1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Bitwise Operators
6. Unary Operators → Increment and Decrement
7. Ternary/ Conditional Operator
8. Special Operators

1. Arithmetic Operators: The different arithmetic operators are:

Operator Name Result Syntax Example (b=5, c=2)


+ Addition Sum a=b+c a=7
- Subtraction Difference a=b-c a=3
* Multiplication Product a=b*c a = 10
/ Division Quotient a=b/c a=2
% Modulus Remainder a=b%c a=1

2. Relational Operators: These are used to compare two quantities. The output will be either 0 (False)
or 1 (True). The different relational operators are:

Operator Name Syntax Example (b=5, c=2)


< Lesser than a=b<c a = 0 (False)
> Greater than a=b>c a = 1 (True)
<= Lesser than or Equal to a = b <= c a = 0 (False)
>= Greater than or Equal to a = b >= c a = 1 (True)
== Equal to a = b == c a = 0 (False)
!= Not equal to a = b != c a = 1 (True)

3. Logical Operators: These are used to test more than one condition and make decision. The different
logical operators are: NOT, AND, OR

✓ Logical NOT (!) The output is true when input is false and vice versa. It accepts only one
input.

Input Output
X !X
0 1
1 0

1
Principles Of Programming Using C BPOPS203 Module 2

✓ Logical AND (&&) The output is true only if both inputs are true. It accepts two or more
inputs.
Input Output
X Y X && Y
0 0 0
0 1 0
1 0 0
1 1 1

✓ Logical OR (||) The output is true onlyif any of its input is true. It accepts two or more inputs.

Input Output
X Y X || Y
0 0 0
0 1 1
1 0 1
1 1 1

4. Assignment Operators: These are used to assign the result or values to a variable. The different types
of assignment operators are:

Simple Assignment a = 10
Shorthand Assignment a += 10 → a = a + 10
Multiple Assignment a = b = c = 10

5. Bitwise Operators: These works on bits and performs bit by bit operations. The different types of
bitwise operators are:
i. Bitwise NOT (~)
ii. Bitwise AND (&)
iii. Bitwise OR (|)
iv. Bitwise XOR (^) → Output is True when odd number of 1‟s are present.
v. Bitwise left shift (<<)
vi. Bitwise right shift (>>)

✓ Bitwise NOT (~)

X ~X
0 1
1 0

2
Principles Of Programming Using C BPOPS203 Module 2

✓ Bitwise AND (&), Bitwise OR (|),Bitwise XOR (^)

X Y X&Y X|Y X^Y


0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

✓ Bitwise Left Shift (<<) → Shift specified number of bits to left side.

X 0 1 0 0 0 1 1 0
X<<2 0 0 0 1 1 0 0 0

✓ Bitwise Right Shift (>>) → Shift specified number of bits to right side.

X 0 1 0 0 0 1 1 0
X>>2 0 0 0 1 0 0 0 1

6. Unary Operators: There are 4 types:

✓ Unary Plus Operator


➔Determines Sign
✓ Unary Minus Operator
✓ Increment (++)
✓ Decrement (--)

✓ Increment (++): It adds one to the operand.

Pre-increment Post-increment
First value of the operand is incremented First value of the operand is used for evaluation
(added) by 1 then, it is used for evaluation. then, it is incremented (added) by 1.
Ex: ++a Ex: a++

✓ Decrement (--): It subtracts one from the operand.

Pre-decrement Post- decrement


First value of the operand is decremented First value of the operand is used for evaluation
(subtracted) by 1 then, it is used for then, it is decremented (subtracted) by 1.
evaluation.
Ex: - -a Ex: a- -

3
Principles Of Programming Using C BPOPS203 Module 2
7. Conditional Operator/ Ternary Operator (?:)

✓ C Language has an unusual operator, useful for making two way decisions.
✓ This operator is combination of two tokens ? and : and takes three operands.
✓ This operator is known as ternary operator or conditional operator.
Syntax:
Expression1 ? Expression2 : Expression3;
Where,
Expression1 is Condition
Expression2 is Statement Followed if Condition is True
Expression3 is Statement Followed if Condition is False
Meaning of Syntax:
1. Expression1 is nothing but Boolean Condition i.e. it results into either TRUE or FALSE
2. If result of expression1 is TRUE then expression2 is executed
3. Expression1 is said to be TRUE if its result is NON-ZERO
4. If result of expression1 is FALSE then expression3 is executed
5. Expression1 is said to be FALSE if its result is ZERO

Example: Check whether Number is Odd or Even

#include<stdio.h>
void main()
{
int num;
printf("Enter the Number : ");
scanf("%d",&num);
flag = ((num%2==0)?1:0);
if(flag==0)
printf("\nEven");
else
printf("\nOdd");
}

8. Special Operator/ Special Symbols

✓ Comma Operator: It can be used as operator in expression and as separator in declaring


variables.
✓ Sizeof Operator: It is used to determine the size of variable or value in bytes.
✓ Address Operator: It is used to find the address of the operators.

Type Conversion and Typecasting


✓ Converting the value of one data type to another type is called as Type Conversion.
✓ It occurs when mixed data occurs.
✓ There are two types:

4
Principles Of Programming Using C BPOPS203 Module 2
i. Automatic Type Conversion (Implicit)

✓ Here, the operand/ variables of smaller data type is automatically converted to data type of larger
size.
✓ char → int → long int → float → double → long double
✓ Ex: int a = 5;
float b = 25, c; c a b c
= a / b; 5 25.0 0.25
printf(“%f” , c);

ii. Manual Type Conversion (Explicit)

✓ It is a forced conversion used to convert operand/ variables of larger data type to smaller size or
vice versa.
✓ Ex: int a = 7, c; a b c
float b = 4.0; 7 4.0 3
b = a % (int) b; b → Converted into int & evaluated
printf(“%d” , c);

Operator Precedence

✓ It is combination of operands (variables, constants) and operators.


✓ Precedence: The order in which operators are evaluated is based on the priority value.
✓ Associativity: It is the parsing direction used to evaluate an expression. It can be left to right or right
to left.
✓ Evaluation of expressions: Expressions are evaluated using an assignment statement.
✓ Ex: variable = expression
sum = a + b
✓ Following table provides the Precedence and Associativity of operators:

5
Principles of Programming using C BPOPS103 Module 2
Operator Description Associativity Precedence(Rank)
() Function call
Left to right 1
[] Array element reference
+ Unary plus
- Unary minus
++ Increment
-- Decrement
! Logical negation
Right to left 2
~ Ones complement
* Pointer to reference
& Address
Sizeof Size of an object
(type) Type cast (conversion)

* Multiplication
/ Division Left to right 3
% Modulus
+ Addition
Left to right 4
- Subtraction
<< Left shift
Left to right 5
>> Right Shift
< Less than
<= Less than or equal to
Left to right 6
> Greater than
>= Greater than or equal to

== Equality
Left to right 7
|= Inequality
& Bitwise AND Left to right 8
^ Bitwise XOR Left to right 9
| Bitwise OR Left to right 10
&& Logical AND Left to right 11
|| Logical OR Left to right 12
?: Conditional expression Right to left 13

6
Principles of Programming using C BPOPS103 Module 2

Decision Control and Looping Statements

Introduction to Decision Control


In some cases we want to only select statements to be executed. It allows the programmers to build the
programs that determine which statements of the code should be executed and which should be ignored.
➢ C support two types of decision control statements that can alter the flow of sequence of instructions.
➢ These include conditional branch statements and unconditional branch statements

Conditional Branching Statements


➢ C language possesses such decision making capabilities by supporting the following statements:
1. If statement
2. If-else statement
3. if-else-if statement
4. else-if ladder statement
5. Switch statement
✓ These statements are popularly known as decision making statements. Also known as control statements

The basic decision statement in the computer is the two way selection.
✓ The decision is described to the computer as conditional statement that can be answered TRUE or FALSE.
✓ If the answer is TRUE, one or more action statements are executed.
✓ If answer is FALSE, the different action or set of actions are executed.
✓ Regardless of which set of actions is executed, the program continues with next statement.

1. if statement: The general form of simple if statements is shown below.

if (Expression)
{
Statement1;
}
Statement2;

8
Principles of Programming using C BPOPS103 Module 2
✓ The Expression is evaluated first, if the value of Expression is true (or non zero) then Statement1 will
be executed; otherwise if it is false (or zero), then Statement1 will be skipped and the execution will
jump to the Statement2.
✓ Remember when condition is true, both the Statement1 and Statement2 are executed in sequence. This
is illustrated in Figure

Note: Statement1 can be single statement or group of statements.

Expression True

False Statement1

Statement2

Figure 1: Flow chart of if statement

Example:
#include<stdio.h>
void main( )
{
int a=20, b=11;
if (a >b)
{
printf(“A is greater\n”);
}
}

Output: A is greater

2. if..else statement: The if..else statement is an extension of simple if statement.

if (Expression)
{
Statement1; → true-block
}
else
{
Statement2; →true-block
}
Statement3;

✓ If the Expression is true (or non-zero) then Statement1 will be executed; otherwise if it is false (or zero),
then Statement2 will be executed.

9
Principles of Programming using C BPOPS103 Module 2
✓ In this case either true block or false block will be executed, but not both.
✓ This is illustrated in Figure 2. In both the cases, the control is transferred subsequently to the Statement3.

False True
Expression

Statement2 Statement1

Statement3

Figure 2: Flow chart of if-else statement


Example:

void main( )
{
int a=10, b=11;
if (a >b)
{
printf(“A is greater\n”);
}
else
{
printf(“B is greater\n”);
}
}

Output: B is greater

3. Nested if .. else statement: When a series of decisions are involved, we have to use more than
oneif..else statement in nested form as shown below in the general syntax.

10
Principles of Programming using C BPOPS103 Module 2

if (Expression1)
{
if(Expression2)
{
Statement1;
}
else
{
Statement2;
}
}
else if(Expression3)
{
Statement3;
}
else
{
Statement4;
}

✓ If Expression1 is true, check for Expression2, if it is also true then Statement1 is executed.
✓ If Expression1 is true, check for Expression2, if it is false then Statement2 is executed.
✓ If Expression1 is false, then Statement3 is executed.
✓ Once we start nesting if .. else statements, we may encounter a classic problem known as dangling else.
✓ This problem is created when no matching else for every if.
✓ C solution to this problem is a simple rule “always pair an else to most recent unpaired if in the
current block”.
✓ Solution to the dangling else problem, a compound statement.
✓ In compound statement, we simply enclose true actions in braces to make the second if a
compound statement.

Figure 3: Flow chart of Nested if-else statement

11
Principles of Programming using C BPOPS103 Module 2

Example:
#include<stdio.h>
void main( )
{
int a = 20, b=15, c=3;
if(a>b)
{
if(a>c)
{
printf(“A is Greater\n”);

}
else
{
printf(“C is greater\n”);

}
}
else
{
if(b>c)
{
printf(“B is greater\n”);
}
else
{
printf(“C is greater\n”);
}
}
]

Output: A is greater

4. else-if ladder or cascaded if else: There is another way of putting ifs together when multipath
decisions are involved. A multi path decision is a chain of ifs in which the statement associated with
each else is an if. It takes the following form.

12
Principles of Programming using C BPOPS103 Module 2

if (Expression1)
{
Statement1;
}
else if(Expression2)
{
Statement2;
}
else if(Expression3)
{
Statement3;
}
else
{
Statement4;
}
Next Statement;

13
Principles of Programming using C BPOPS103 Module 2

✓ This construct is known as the else if ladder.


✓ The conditions are evaluated from the top (of the ladder), downwards. As soon as true condition is
found, the statement associated with it is executed and control transferred to the Next statement skipping
the rest of the ladder.
✓ When all conditions are false then the final else containing the default Statement4 will be executed.

Figure 4: Flow chart of else if ladder statement


Example:
#include<stdio.h>
void main( )
{
int a=20, b=5, c=3;
if((a>b) && (a>c))
printf(“A is greater\n”);
else if((b>a) && (b>c))
printf(“B is greater\n”);
else if((c>a) && (c>b))
printf(“C is greater\n”);
else
printf(“All are equal\n”);
}

14
Principles of Programming using C BPOPS103 Module 2

Output: A is greater

5. Switch Statement:
✓ C language provides a multi-way decision statement so that complex else-if statements can be
easilyreplaced by it.
✓ C language‟s multi-way decision statement is called switch.
General syntax of switch statement is as follows:
switch(choice)
{
case label1: block1;
break;
case label2: block2;
break;
case label3: block-3;
break;
default: default-block;
break;
}

✓ Here switch, case, break and default are built-in C language words.
✓ If the choice matches to label1 then block1 will be executed else if it evaluates to label2 then block2
will be executed and so on.
✓ If choice does not matches with any case labels, then default block will be executed.

Figure 5: Flow chart of switch case statement


✓ The choice is an integer expression or characters.
✓ The label1, label2, label3,…. are constants or constant expression evaluate to integer constants.
✓ Each of these case labels should be unique within the switch statement. block1, block2, block3, …
are statement lists and may contain zero or more statements.
✓ There is no need to put braces around these blocks. Note that case labels end with colon(:).
✓ Break statement at the end of each block signals end of a particular case and causes an exit from
switch statement.

15
Principles of Programming using C BPOPS103 Module 2
✓ The default is an optional case when present, it will execute if the value of the choice does not match
with any of the case labels.

Example:
Label → Number Label → Character
#include<stdio.h> #include<stdio.h>
#include<stdlib.h> #include<stdlib.h>
void main( ) void main( )
{ {
int ch,a,b,res; int a,b,res;
float div; char ch;
printf(“Enter two numbers:\n”); float div;
scanf(“%d%d”,&a,&b); printf(“Enter two numbers:\n”);
printf(“1.Addition\n 2.Subtraction\n scanf(“%d%d”,&a,&b);
3.Multiplication\n 4.Division\n 5.Remainder\n”); printf(“a.Addition\n b.Subtraction\n
printf(“Enter your choice:\n”); c.Multiplication\n d.Division\n e.Remainder\n”);
scanf(“%d”,&ch); printf(“Enter your choice:\n”);
switch(ch) scanf(“%c”,&ch);
{ switch(ch)
case 1: res=a+b; {
break; case „a‟: res=a+b;
case 2: res=a-b; break;
break; case „b‟: res=a-b;
case 3: res=a*b; break;
break; case „c‟: res=a*b;
case 4: div=(float)a/b; break;
break; case „d‟: div=(float)a/b;
case 5: res=a%b; break;
break; case „e‟ : res=a%b;
default: printf(“Wrong choice!!\n”); break;
} default: printf(“Wrong choice!!\n”);
printf(“Result=%d\n”,res); }
} printf(“Result=%d\n”,res);
}

In this program if ch=1 case „1‟ gets executed and if ch=2, case „2‟ gets executed and so on.

16
Principles of Programming using C BPOPS103 Module 2

Iterative Statements
Definition of Loop: It is a programming structure used to repeatedly carry out a particular
instruction/statement until a condition is true. Each single repetition of the loop is known as an iteration
of the loop.

Three important components of any loop are:


1. Initialization (example: ctr=1, i=0 etc)
2. Test Condition (example: ctr<=500, i != 0 etc)
3. Updating loop control values (example: ctr=ctr+1, i =i-1)

Pre-test and Post-test loops

✓ Loops can be classified into two types based on the placement of test-condition.
✓ If the test-condition is given in the beginning such loops are called pre-test loops (also known as entry-
controlled loops).
✓ Otherwise if test condition is given at the end of the loop such loops are termed as post-test loops (or exit
controlled loops).
Note Figure 1:
1. Here condition is at the beginning of loop. That is why it is called pre-test loop
2. It is also called as entry controlled loop because condition is tested before entering into the loop.
3. while is a keyword which can be used here.

Note Figure 2:
1. Here condition is at the end of the loop. That is why it is called post-test loop.
2. It is also called as exit controlled loop because condition is tested after body of the loop is
executed at least once.
3. do and while are keywords which can be used here.

17
Principles of Programming using C BPOPS103 Module 2

LOOPS IN C
C language provides 3 looping structures namely:
1. while loop
2. do….while loop
3. for loop

The loops may be classified into two general types. Namely:


1. Counter-controlled loop
2. Sentinel-controlled loop

✓ When we know in advance exactly how many times the loop will be executed, we use a
counter-controlled loop. A counter controlled loop is sometimes called definite repetition loop.
✓ In a sentinel-controlled loop, a special value called a sentinel value is used to change the loop
controlexpression from true to false. A sentinel-controlled loop is often called indefinite repetition loop.

i. while loop:
It is a pre-test loop (also known as entry controlled loop).
This loop has following syntax:

Statement x;
while (condition)
{
statement-block;
}
Statement y;

➢ In the syntax given above „while‟ is a key word and condition is at beginning of the loop.
➢ If the test condition is true the body of while loop will be executed.
➢ After execution of the body, the test condition is once again evaluated and if it is true, the body is
executed once again.
➢ This process is repeated until condition finally becomes false and control comes out of the body of
theloop.

FLOWCHART:

18
Principles of Programming using C BPOPS103 Module 2

✓ Here is an example program using while loop for finding sum of 1 to 10.
Example: WAP to find sum of 1 to 10 using while.
#include<stdio.h>
void main()
{
int i=1, sum=0;
while (i<=10)
{
sum=sum+i;
i++;
}
printf(“%d”, sum);
}

ii. do…. while loop:


It is a post-test loop (also called exit controlled loop) it has two keywords do and while.
The General syntax:

Statement x;
do
{
Statement-block;
} while (condition);
Statement y;

✓ In this loop the body of the loop is executed first and then test condition is evaluated.
✓ If the condition is true, then the body of loop will be executed once again. This process continues as
long as condition is true.
✓ When condition becomes false, the loop will be terminated and control comes out of the loop.

FLOWCHART:

Example: WAP to find sum of 1 to 10 using do… while


#include<stdio.h>
void main()
{
int i=1, sum=0;

19
Principles of Programming using C BPOPS103 Module 2
do
{
sum=sum+i;
i=i++;
} while (i<=5)
printf(“%d”, sum);
}

iii. for loop:

✓ It is a pre test loop and also known as entry controlled loop.


✓ “for” keyword is used here.
✓ Here, the head of the for loop contains all the three components that is initialization, condition and
Updation.
General syntax:
for( initialization; test-condition; updation)
{
Statements;
}

The execution of for statement is as follows.


1. First the control variable will be initialized. (Example i=1)
2. Next the value of control variable is tested using test condition. (i<=n)
3. If the condition is true the body of the loop is executed else terminated.
4. If loop is executed then control variable will be updated (i++) then, the condition is checked again.

FLOWCHART:

Example: WAP to find the sum of 10 numbers using for loop.


#include<stdio.h>
void main( )
{
int i, sum=0;
for (i=1; i<=10; i++)
{
sum=sum+i;
}

20
Principles of Programming using C BPOPS103 Module 2
printf(“%d”, sum);
}

Note: In for loops whether both i++ or ++i operations will be treated as pre-increment only.

Points to Remember about for loop

✓ If all the expressions are omitted then there must be 2 semicolons inside a for loop
✓ Don‟t put the semicolon at the end of for loop
✓ Multiple initializations must be separated with a colon operator
✓ If there is no initialization to be done, then initialization statement can be skipped by
giving semicolon
✓ The loop variable is updated in the statement block
✓ Multiple conditions in the test expressions can be tested by using operators (&& and ||)
✓ Multiple statements inside the for loop separated with comma operator

Nested for loops


✓ A for loop inside a for loop is called Nested for loop.

FLOWCHART: `

Example: for(i=0; i<2; i++)


{
for(j=0; j<2; j++)
{
scanf(“%d”, &a[i][j])
}
}
Note: If updation is not present in loops then, it will execute infinite times.
If initialization is not given then, program prints nothing.

21
Principles of Programming using C BPOPS103 Module 2
Differences between while and do while loop
While do… while
It is a pre test loop. It is a post test loop.
It is an entry controlled loop. It is an exit controlled loop.
The condition is at top. The condition is at bottom.
There is no semi colon at the end of while. The semi colon is compulsory at the end of while.
It is used when condition is important. It is used when process is important.
Here, the body of loop gets executed if and only if Here, the body of loop gets executed atleast once
condition is true. even if condition is false.
SYNTAX, FLOWCHART, EXAMPLE (Same as in SYNTAX, FLOWCHART, EXAMPLE (Same as in
explanation) explanation)

Break and Continue statements

✓ break and continue statements are unconditional control construct.

1. break statement:

✓ It terminates the execution of remaining iteration of loop.


✓ A break can appear in both switch and looping statements.
Syntax Flowchart
#include<stdio.h>
while(condition) void main( )
{ {
Statements; int i;
if(condition) for(i=1; i<=5; i++)
break; {
Statements; if(i==3)
} break;
printf(“%d”, i)
}
}

OUTPUT 12

22
Principles of Programming using C BPOPS103 Module 2

2. Continue statement

✓ It terminates onlythe current iteration of the loop.


✓ Continue can appear in looping statements.
Syntax Flowchart
#include<stdio.h>
while(condition) void main( )
{ {
Statements; int i;
if(condition) for(i=1; i<=5; i++)
continue; {
Statements; if(i==3)
} continue;
printf(“%d”, i)
}
}

OUTPUT 124 5

23
Module 2

GOTO Statement
✓ goto statement is used to transfer the control to a specified label.
✓ goto is an unconditional branching statement.
✓ The syntax of goto is as follows:

Syntax Example
goto label; void main( )
{
statement1; int a=5, b=7;
goto end;
statement2; a=a+1;
b=b+1;
label: end: printf(“a=%d b=%d”, a,b);
}

VTU SOLVED QUESTIONS


Evaluate: i=1;
l : if(i >2)
{
printf(“saturday”);
i = i-1;
goto l;
}
printf(“sunday”);

Explain your result briefly.

Output → Sunday
The i is initialized to 1 and in if condition when checked 1 > 2 returns false. Hence, it doesn‟t enter inside loop and
just prints the Sunday.

24
Module 2
Quadratic Equation
✓ The Quadratic Formula uses the "a", "b", and "c" from "ax2 + bx + c", where "a", "b", and "c" are
just numbers; they are the "numerical coefficients" of the quadratic equation they've given you to solve.

#include<stdio.h>
#include<math.h>
void main()
{
float a, b, c, root1, root2, rpart, ipart, disc;
printf("Enter the 3 coefficients:\n”);
scanf(%f%f%f", &a, &b, &c);
if((a*b*c) = = 0)
{
printf("Roots cannot be Determined:\n");
exit(0);
}
disc=(b*b) - (4*a*c);
if(disc == 0)
{
printf("Roots are equal\n");
root1= -b / (2*a);
root2=root1;
printf ("root1 = %f \n", root1);
printf ("root2 = %f \n", root2);
}
else if(disc>0)
{

25
Module 2
printf("Roots are real and distinct\n");
root1= (-b + sqrt(disc)) / (2*a);
root2= (-b - sqrt(disc)) / (2*a);
printf ("root1 = %f \n", root1);
printf ("root2 = %f \n", root2);

}
else
{
printf("Roots are complex\n");
rpart = -b / (2*a);
ipart = (sqrt (-disc)) / (2*a);
printf("root1 = %f + i %f \n", rpart, ipart);
printf("root2 = %f - i %f \n", rpart, ipart);

}
}

26
Module 2

Output 1 Output 2 Output 3


Enter the 3 coefficients: Enter the 3 coefficients: Enter the 3 coefficients:
1 2 2
2 3 2
1 2 2
Roots are equal Roots are real and equal Roots are complex
Root1= -1.000000 Root1=-0.500000 Root1=-0.500000+i 0.866025
Root2= -1.000000 Root2=-2.000000 Root2=-0.500000- i 0.866025

✓ Write a program in C to display the grade based on the marks as follows:

Marks Grades
0 to 39 F
40 to 49 E
50 to 59 D
60 to 69 C
70 to 79 B
80 to 89 A
90 to 100 O
#include<stdio.h>
void main( )
{
int marks;
printf(“Enter the marks\n”);
scanf(“%d”, &marks);
if(marks >= 0 && marks <= 39)
printf(“F”);
else if(marks >= 40 && marks <= 49)
printf(“E”);
else if(marks >= 50 && marks <= 59)
printf(“D”);
else if(marks >= 60 && marks <= 69)
printf(“C”);
else if(marks >= 70 && marks <= 79)
printf(“B”);
else if(marks >= 80 && marks <= 89)
printf(“A”);
else
printf(“O”);
}
Module 2

✓ WAP to find sum of odd numbers from 1 to N using do-while loop.

include<stdio.h>

void main()
{
int n, i=1, sum=0;
printf(“Enter the value of n”\n);
scanf(“%d”, &n);
do
{
sum=sum+i;
i = i + 2;
} while (i<=n);
printf(“Sum of odd numbers is %d”, sum);
}

✓ WACP to check whether a given number is even or odd using if-else statement.

include<stdio.h>

void main()
{
int n;
printf(“Enter a number”\n);
scanf(“%d”, &n);
if(n%2==0)
printf(“The given number is Even\n”);
else
printf(“The given number is Odd\n”);
}

You might also like