26
Unit 2 Expressions and conditional statements Arithmetic expression: 1) + It is addition of variable & . 2) It is subtraction between & . 3) It is multiplication between & . 4) / It is division of . 5) % It is remainder obtained by divided . 6) ( + ) ∗ is added to then it is multiplied by . 7) ( + )/( + ) ( + ) & ( + ) are executed then the division between them. The multiplication division & modulus operator are performed first & then addition & subtraction are performed. The operations are performed from left to right. The bracket receive first performance for calculations. Ex.1] Write the arithmetic statement in ‘c’. 1) 2 + 2 2 + = Sol: ( ∗ + ∗ )( ∗ + ) = 2) + = Sol: ∗ /( + ) = 3) (+) 2 + + 2 = Sol: ( + ) ∗ ( + )/( + ) + ∗ = 4) ( 2 + 2 + ) 3 = Sol: ( ∗ + ∗ + ∗ ) ∗ ( ∗ + ∗ + ∗ ) × ( ∗ + ∗ + ∗ ) = 5) + + 5 2 = Sol: /( + ) + 5 ∗ ∗ = 6) = [1 − 2 2 2 2 ] Sol: = /( ∗ ∗ ) ∗ (1 − ( ∗ )/( ∗ ) − ( ∗ )/( ∗ ) Syllabus : Arithmetic expression and its evaluation precedence of arithmetic operators type. Conversion operator precedence, mathematical functions. Reading and writing a character. Formatted input and output. Decision making, if, if else, switch and go to statements T.Y.B.Sc. PAGE-01

T.Y.B.Sc. PAGE-01 ·  · 2017-10-055) ... The general form of the statement is scanf ("% " ,& ); ... 1) printf (“programing in ‘’ is very interesting \n\n”) Output: programing

Embed Size (px)

Citation preview

Unit 2

Expressions and conditional statements

Arithmetic expression:

1) 𝑎 + 𝑏 It is addition of variable 𝑎 & 𝑏.

2) 𝑎 − 𝑏 It is subtraction between 𝑎 & 𝑏.

3) 𝑎 ∗ 𝑏 It is multiplication between 𝑎 & 𝑏.

4) 𝑎/𝑏 It is division of 𝑎 𝑏𝑦 𝑏.

5) 𝑎%𝑏 It is remainder obtained by 𝑎 divided 𝑏.

6) (𝑎 + 𝑏) ∗ 𝑐 𝑎 is added to 𝑏 then it is multiplied by 𝑐.

7) (𝑎 + 𝑏)/(𝑐 + 𝑑) (𝑎 + 𝑏) & (𝑐 + 𝑑) are executed then the division between them.

The multiplication division & modulus operator are performed first & then addition &

subtraction are performed. The operations are performed from left to right. The bracket receive

first performance for calculations.

Ex.1] Write the arithmetic statement in ‘c’.

1) 𝑥2+𝑦2

𝑎2+𝑏= 𝑧

Sol: (𝑥 ∗ 𝑥 + 𝑦 ∗ 𝑦)(𝑎 ∗ 𝑎 + 𝑏) = 𝑧

2) 𝑎∙𝑏

𝑐+𝑧= 𝑏

Sol: 𝑎 ∗ 𝑏/(𝑐 + 𝑧) = 𝑏

3) (𝑎+𝑏)2

𝑐+𝑑+ 𝑓2 = 𝑞

Sol: (𝑎 + 𝑏) ∗ (𝑎 + 𝑏)/(𝑐 + 𝑑) + 𝑓 ∗ 𝑓 = 𝑞

4) (𝑥2 + 𝑦2 + 𝑧)3 = 𝑛

Sol: (𝑥 ∗ 𝑥 + 𝑦 ∗ 𝑦 + 𝑧 ∗ 𝑧) ∗ (𝑥 ∗ 𝑥 + 𝑦 ∗ 𝑦 + 𝑧 ∗ 𝑧) × ( 𝑥 ∗ 𝑥 + 𝑦 ∗ 𝑦 + 𝑧 ∗ 𝑧) = 𝑛

5) 𝑑

𝑘+𝑧+ 5𝑗2 = 𝑚

Sol: 𝑑/(𝑘 + 𝑧) + 5 ∗ 𝑗 ∗ 𝑗 = 𝑚

6) 𝑏 =𝑙

𝑝𝑎𝑏[1 −

𝑢2

𝑎2 −𝑣2

𝑏2]

Sol: 𝑏 = 𝑙/(𝑝 ∗ 𝑎 ∗ 𝑏) ∗ (1 − (𝑢 ∗ 𝑢)/(𝑎 ∗ 𝑎) − (𝑣 ∗ 𝑣)/(𝑏 ∗ 𝑏)

Syllabus : Arithmetic expression and its evaluation precedence of arithmetic operators type.

Conversion operator precedence, mathematical functions.

Reading and writing a character.

Formatted input and output.

Decision making, if, if – else, switch and go to statements

T.Y.B.Sc. PAGE-01

Input statement:

This statement is used for the supplying of data to the program. This statement is

optical & can be used whenever the program is to be made as a general computer program.

The input statement of the ‘C’ program is indicated by a “scanf” statement.

The general form of the statement is scanf ("%𝑥𝑎" , &𝑦);

Where 𝑎 is a format conversion character.

𝑦 is a variable to be read by the computer.

𝑥 is a the format conversion modifier.

Format conversion character : The following format conversion character are used in the program.

character use

d used for reading of integer variable.

f read is expressed value in floating value of variable or

double value.

e This format character is identical to the f format but

used for exponent value.

c This format is used for reading of character, number,

alphabets, spherical character etc.

s sequence of character or string.

Ex. The following example show the data input for the given variables.

1)scanf (“%d %d %d”, &hour, &min, &sec);

Data 10, 20, 30

2)scanf (“%5d %5d %5d”, &a,&b, &c);

Data 10 20 30

3)scanf (“%d %f %e”, &a, &b, &c);

Data 10 2.654 2.1e-5

Output statement:

This statement is used for printing of variable & arithmetic expressions etc. the

general form of the statement is printf(“%𝑥𝑎𝑢𝑣”, 𝑦);

Where 𝑥 = format conversion modifier

𝑎 = format conversion character

𝑢 = comment

𝑦 = variable or arithmetic expression

𝑣 = white space character.

T.Y.B.Sc. PAGE-02

Format conversion character:

character use/purpose

d The value is displayed as an integer variable or expression.

u The value of variable is displayed as an unsigned decimal number.

f The value is displayed in floating point from either (float or double).

e The value is displayed in exponential.

g This use is for float or double value in e or f

format which even takes less space.

c The value is displayed as a single character.

s The value is displayed as a character sting & used for character array.

White Space Character : The white space character are used along with printing of variable to shift

control. These are as follows

1) Blank space : This leaves blank space at the time of printing.

2) \t : This leaves five blank spaces at a time.

3) \n : This shift the control to a new line.

4) \f : This shift the control to a new page.

5) \v : This shift the control to a vertical tab.

6) \b : This shift the control to a backspace.

Examples of ‘printf’ statement:

1) printf (“programing in ‘C’ is very interesting \n\n”)

Output: programing in ‘C’ is very interesting control goes to third line.

2) If 𝑎 = 10, 𝑏 = 20, 𝑐 = 30

printf (“%𝑑 %𝑑 %𝑑”, 𝑎, 𝑏, 𝑐);

Output 10 20 30

3) printf (“%5𝑑 %5𝑑 %5𝑑”, 𝑎, 𝑏, 𝑐);

Output 10 20 30

4) printf (“𝑎 = %𝑑 𝑏 = %𝑑 𝑐 = %𝑑\𝑛”, 𝑎, 𝑏, 𝑐);

Output 𝑎 = 10 𝑏 = 20 𝑐 = 30

5) printf (“𝑎 = %3𝑑 𝑏 = %4𝑑 𝑐 = %5𝑑\𝑛”, 𝑎, 𝑏, 𝑐);

Output 𝑎 = 10 𝑏 = 20 𝑐 = 30

6) printf (“𝑎 = %03𝑑 𝑏 = %05𝑑 𝑐 = %07𝑑\𝑛”, 𝑎, 𝑏, 𝑐);

Output 𝑎 = 010 𝑏 = 00020 𝑐 = 0000030

T.Y.B.Sc. PAGE-03

7) printf (“𝑑 = %𝑑\𝑛”, 𝑏 + 𝑐);

Output 𝑑 = 50

Ex.1. Write a program to print our college name , class and subject.

Sol : /* Program to print our college name, class and subject*/

main( )

{

printf(”College : Shri. V. S. Naik College, Raver”);

printf(“Class : T. Y. B.Sc.”);

printf(“Subject : Mathematics”);

}

Output : College : Shri. V. S. Naik College, Raver

Class : T. Y. B. Sc.

Subject : Mathematics

Ex.2. Write a program of sum of two numbers 2 and 3.

Sol : /*program on addition of two variable */

main( )

{

Int 𝑎, 𝑏, 𝑐;

𝑎 = 2;

𝑏 = 3;

𝑐 = 𝑎 + 𝑏;

printf (“%𝑑 + %𝑑 = %𝑑\𝑛” 𝑎, 𝑏, 𝑐);

}

Output: 2 + 3 = 5

Ex.3. write a program to find the average of three number.

/* program of average of three number */

main( )

{

int 𝑎, 𝑏, 𝑐 𝑎𝑣𝑒;

printf (“enter the value of 𝑎, 𝑏, 𝑐”);

scanf (“%𝑑 %𝑑 %𝑑” , &𝑎, &𝑏, &𝑐);

ave = (𝑎 + 𝑏 + 𝑐)/3;

T.Y.B.Sc. PAGE-04

printf (“average = %𝑑”, 𝑎𝑣𝑒);

}

Or

main( )

{

int 𝑎, 𝑏, 𝑐;

float 𝑎𝑣𝑒;

printf (“enter the value of 𝑎, 𝑏, 𝑐”);

scanf (“%𝑑 %𝑑 %𝑑” , &𝑎, &𝑏, &𝑐);

ave = (𝑎 + 𝑏 + 𝑐)/3;

printf (“average = %𝑓”, 𝑎𝑣𝑒);

}

Ex.4. write a program to evaluate 𝑎 + 𝑏, 𝑎 − 𝑏, 𝑎. 𝑏,𝑎

𝑏, remainder of

𝑎

𝑏 , where 𝑎 & 𝑏 are integers

to be given.

Sol : main( )

{

int 𝑎, 𝑏, 𝑠𝑢𝑏, 𝑠𝑢𝑚, 𝑚𝑢𝑙, 𝑑𝑖𝑣, 𝑟𝑒𝑚;

printf (“𝐼𝑛𝑝𝑢𝑡 𝑣𝑎𝑙𝑢𝑒𝑠 𝑜𝑓 𝑎 & 𝑏 ");

scanf (“%𝑑 %𝑑”, &𝑎, &𝑏);

𝑠𝑢𝑚 = 𝑎 + 𝑏;

𝑠𝑢𝑏 = 𝑎 − 𝑏;

𝑚𝑢𝑙 = 𝑎 ∗ 𝑏;

𝑑𝑖𝑣 = 𝑎/𝑏;

𝑟𝑒𝑚 = 𝑎%𝑏;

𝑝𝑟𝑖𝑛𝑡𝑓 (“𝑠𝑢𝑚 = %𝑑, 𝑠𝑢𝑏 = %𝑑, 𝑚𝑢𝑙 = %𝑑\𝑛”, 𝑠𝑢𝑚, 𝑠𝑢𝑏, 𝑚𝑢𝑙);

𝑝𝑟𝑖𝑛𝑡𝑓 (“𝑑𝑖𝑣𝑖𝑠𝑖𝑜𝑛 = %𝑑, 𝑟𝑒𝑚𝑎𝑖𝑛𝑑𝑒𝑟 = %𝑑", 𝑑𝑖𝑣, 𝑟𝑒𝑚);

}

Ex.5. program to calculate volume of a box.

Sol : main( )

{

𝑓𝑙𝑜𝑎𝑡 𝑙, 𝑏, ℎ, 𝑣;

𝑝𝑟𝑖𝑛𝑡𝑓 (“𝑒𝑛𝑡𝑒𝑟 𝑡ℎ𝑒 𝑣𝑎𝑙𝑢𝑒 𝑜𝑓 𝑙𝑒𝑛𝑔𝑡ℎ, 𝑏𝑟𝑒𝑎𝑡ℎ, & ℎ𝑒𝑖𝑔ℎ𝑡 \𝑛”);

T.Y.B.Sc. PAGE-05

𝑠𝑐𝑎𝑛𝑓 (“%𝑓 %𝑓 %𝑓”, &𝑙, &𝑏, &ℎ);

𝑣 = 𝑙 ∗ ℎ ∗ 𝑏;

𝑝𝑟𝑖𝑛𝑡𝑓 (“𝑉𝑜𝑙𝑢𝑚𝑒 𝑜𝑓 𝑏𝑜𝑥 = %𝑓”, 𝑣);

}

Ex.6. Write a program to calculate area and circumference of the circle, whose radius is given.

Sol : ⋕ 𝑑𝑒𝑓𝑖𝑛𝑒 𝑃𝐼 3.142

main( )

{

Int r;

float area, circumference;

printf (“Input radius of the circle”);

scanf (“%d”,&r);

area = 𝑃𝐼 ∗ 𝑟 ∗ 𝑟;

circumference = 2 ∗ 𝑃𝐼 ∗ 𝑟;

printf(“area of the circle= %𝑓\n”,area);

printf(“Circumference of the circle= %𝑓\𝑛”,circumference);

}

Ex.7. if the marks obtained by a students in six different subjects are input. Write a program to find

out the aggregate makes & percentage marks obtained by the students. Assume that the maximum

marks that can be obtained by a student in each subject is 100.

Sol : main( )

{

𝑖𝑛𝑡 𝑚1, 𝑚2, 𝑚3, 𝑚4, 𝑚5, 𝑚6;

𝑓𝑙𝑜𝑎𝑡 𝑎𝑣𝑔;

𝑝𝑟𝑖𝑛𝑡𝑓 (“Input six subject marks”);

𝑠𝑐𝑎𝑛𝑓 (“%𝑑 %𝑑 %𝑑 %𝑑 %𝑑 %𝑑”, &𝑚1, &𝑚2, &𝑚3, &𝑚4, &𝑚5, &𝑚6);

𝑎𝑣𝑔 = (𝑚1 + 𝑚2 + 𝑚3 + 𝑚4 + 𝑚5 + 𝑚6)/6;

𝑝𝑟𝑖𝑛𝑡𝑓 (“𝑎𝑔𝑔𝑟𝑒𝑔𝑎𝑡𝑒 𝑚𝑎𝑟𝑘𝑠 = %𝑓”, 𝑎𝑣𝑔);

𝑝𝑟𝑖𝑛𝑡𝑓 (“𝑝𝑒𝑟𝑐𝑒𝑛𝑡𝑎𝑔𝑒 𝑚𝑎𝑟𝑘𝑠 = %2.2𝑓%”, 𝑎𝑣𝑔);

}

Ex.8. Write a program to calculate simple interest and amount when principle, rate of

interest and period is given.

Sol : main( )

T.Y.B.Sc. PAGE-06

{

float p, r, n, simple_interest, amount;

printf(“Input principle, rate of interest and period”);

scanf(“%f%f%f”,&p,&r,&n);

simple_interest = (𝑝 ∗ 𝑟 ∗ 𝑛)/100;

amount = 𝑝𝑟𝑖𝑛𝑐𝑖𝑝𝑙𝑒 + 𝑠𝑖𝑚𝑝𝑙𝑒_𝑖𝑛𝑡𝑒𝑟𝑒𝑠𝑡;

printf(“Simple Interest =%f”,simple_interest);

printf(“Amount =%f”, amount);

}

Ex.9. Dipika’s basic salary is input through keybord. Her pay band is 5400, dearness

allowance is 90% of basic salary, house rent allowance is 10% of basic salary, travelling

allowance is 5% of basic salary. Write a program of gross salary.

( Gross salary = basic salary + pay band + dearness allowance + house rent + travelling

allowance )

Sol: main( )

{

𝑖𝑛𝑡 𝑏_ 𝑠𝑎𝑙, 𝑝𝑎𝑦_𝑏𝑎𝑛𝑑, 𝑑𝑎, ℎ𝑟𝑎, 𝑡𝑎, 𝑔𝑟𝑜𝑠𝑠_𝑠𝑎𝑙;

𝑝𝑎𝑦𝑏𝑎𝑛𝑑 = 5400;

𝑝𝑟𝑖𝑛𝑡𝑓 (“Input basic salary”);

𝑠𝑐𝑎𝑛𝑓 (“%𝑑”, & 𝑏_𝑠𝑎𝑙);

𝑑𝑎 = 𝑏_𝑠𝑎𝑙 × 90/100;

ℎ𝑟𝑎 = 𝑏_𝑠𝑎𝑙 × 10/100;

𝑡𝑎 = 𝑏_𝑠𝑎𝑙 × 5/100;

𝑔𝑟𝑜𝑠𝑠_𝑠𝑎𝑙 = 𝑏_𝑠𝑎𝑙 + 𝑝𝑎𝑦_ 𝑏𝑜𝑛𝑑 + 𝑑𝑎 + ℎ𝑟𝑎 + 𝑡𝑎;

𝑝𝑟𝑖𝑛𝑡𝑓 (“𝐷𝑖𝑝𝑖𝑘𝑎′𝑠 gross salary = %𝑑", 𝑔𝑟𝑜𝑠𝑠_𝑠𝑎𝑙);

}

Ex.10. Write a program to convert days to months & day.

Sol : main ( )

{

int month, days;

printf (“Enter days\n”);

T.Y.B.Sc. PAGE-07

scanf (“%d”, & days);

month = days/30;

days = days % 30;

print f (“month=%d, days=%d”, months, days);

}

Output : Enter days. 265

Month = 8 Days = 25

Ex.11. If a five digit number is input. Write a program to calculate the sum of it’s digits.

Sol : main( )

{

int number, sum, rem;

sum = 0;

printf(“Input five digit number”);

scanf(“%d”, &number);

rem = number%10;

sum = sum + rem;

number = number/10;

rem = number%10;

sum = sum + rem;

number = number/10;

rem = number%10;

sum = sum + rem;

number = number/10;

rem = number%10;

sum = sum + rem;

number = number/10;

sum = sum + number;

printf(“sum = %d”, sum);

}

Ex.12. If a five digit number is input. Write a program to reverse the number.

Sol : main( )

{

int number, rem;

T.Y.B.Sc. PAGE-08

printf(“Input five digit number”);

scanf(“%d”, &number);

rem = number%10;

printf(“%d”,rem);

number = number/10;

rem = number%10;

printf(“%d”,rem);

number = number/10;

rem = number%10;

printf(“%d”,rem);

number = number/10;

rem = number%10;

printf(“%d”,rem);

number = number/10;

printf(“%d\n”number);

}

Simple ‘if’ statement : This statement is used when the given condition is true, then the decision is

executed. The general form of this statement is

if (𝑥1)

𝑦1;

Where 𝑥1: logical expression which create a condition.

𝑦1: : programing statement.

This statement is executed as it 𝑥1 condition is true, then 𝑦1 decision is executed.

Logical expression : If the variable is joined to a constant or variable by a logical operator, then it is

a logical expression.

Logical operators meaning

< less than

> greater than

> = greater than or equal to

< = less than or equal to

= = equal to

! = not equal to

Ex.1. Write a program to calculate the absolute value of the number.

T.Y.B.Sc. PAGE-09

Sol. main ( )

{

float number;

print (“write your number |n”);

scant (“%f”, & number);

if (number < 0.0)

number = − number;

printf (“the absolute value is %f\n”, number);

}

‘If –else’ statement : This statement is used for taking two decision in the program. If the condition

is found to be true then the first decision is executed & if the condition is found to be false, then the

second decision is executed.

If (𝑥1)

{

𝑦1;

}

else, 𝑦2 ; where 𝑥1 ∶ logical expression.

𝑦1, 𝑦2 ∶ program statement.

Ex.1. Write a program to check given integer number is less than 100 or more than two digits.

Sol : main( )

{

int number;

printf(“Enter an integer number”);

scanf(“%d”,&number);

if(number < 100)

printf(“Given number is smaller than 100\n”);

else

printf(“Given number contain more than two digits\n”);

}

Ex.2. Write a program to check given integer number is even or odd.

Sol : main( )

{

int number, rem;

T.Y.B.Sc. PAGE-10

printf(“Enter an integer number”);

scanf(“%d”,&number);

rem = number%2;

if(rem == 0)

printf(“Given number %d is even\n”, number);

else

printf(“Given number %d is odd\n”, number);

}

Ex.3. Write a program to check whether the given number is divisible by 3 or not.

Sol : main ( )

{

int num, rem;

printf (“Enter number”);

scanf (“%d”, & num);

rem = num%3;

if (rem == 0)

printf (“Given number %d is divisible by 3”,num);

else

printf (“Given number %d is not divisible by 3”, num);

}

Ex.4. Write a program to evaluates the function.

𝑓(𝑥) = 𝑎𝑥2 + 𝑏 𝑖𝑓 𝑥 >= 2

= 𝑎𝑥 + 𝑏𝑥 𝑖𝑓 𝑥 < 2

Sol : main ( )

{

float 𝑎, 𝑏, 𝑥, 𝑦;

printf (“enter value of 𝑎, 𝑏, 𝑥\𝑛");

scanf (“%𝑓 %𝑓 %𝑓", &𝑎, &𝑏, &𝑥);

If (𝑥 >= 2.0)

𝑦 = 𝑎 ∗ 𝑥 ∗ 𝑥 + 𝑏;

else

𝑦 = 𝑎 ∗ 𝑥 + 𝑏 ∗ 𝑥;

T.Y.B.Sc. PAGE-11

printf (“The value of 𝑦 = %𝑑\𝑛", 𝑦);

}

Ex.5. The formula for calculating income tax is as under

Income tax = 0 , if income <= 250000

Income tax = 0.10(Income − 250000) , if income > 2500000. Write a program.

Sol : main ( )

{

int income, itax;

printf (“Input income of a person”);

scanf (“%d”, income);

if (income < = 250000)

itax = 0 ;

else

itax = 0.10 ∗ (income − 250000);

printf (“Income tax = %d”, itax);

}

Compound ‘if’ statement : If two condition are to be checked simultaneously & the execution of

decisions depends on these two conditions, then it is used for decision making. This statement takes

the decision on checking of two or more condition simultaneously.

operator meaning

&& and

|| or

! negation (not)

eg. : if ((𝑎 > 𝑏)&&(𝑏 > 𝑐))

𝑑 = 𝑎 + 𝑏;

else

𝑑 = 𝑎 − 𝑏;

Ex.1. Write a program if the student age is between 5 to 15 then he appear for contest.

Sol : main ( )

{

int age;

printf (“Enter the age of the student\n”);

T.Y.B.Sc. PAGE-12

scanf (“%d”, & age);

if (age > 5 && age < 15)

{

printf (“student can appear for contest\n”);

}

else

{

printf (“student cannot appear for contest\n”);

}

}

Ex.2. Write a program to check given alphabet is vowel or consonant.

Sol : main( )

{

char c;

printf(“Enter an alphabet”);

scanf(“%c”,&c);

if(𝑐 == ′𝑎′ ||𝑐 ==′ 𝐴′||𝑐 ==′ 𝑒′||𝑐 ==′ 𝐸′||𝑐 ==′ 𝑖′||𝑐 ==′ 𝐼′||𝑐 ==′ 𝑜′||𝑐 ==′ 𝑂′||𝑐 =′ 𝑢′||𝑐 == ′𝑈′)

printf(“%c is vowel”, c);

else

printf(“%c is a consonant”, c);

}

Ex.3. Write a program to check whether a given character is alphabet or not.

Sol : main( )

{

char c;

printf(“Enter a character”);

scanf(“%c”,&c);

if((𝑐 ≥′ 𝑎′&& 𝑐 ≤ ′𝑧′)||( 𝑐 ≥′ 𝐴′ && 𝑐 ≤ ′𝑍′))

printf(“%c is an alphabet”, c);

else

printf(“%c is not an alphabet”, c);

}

T.Y.B.Sc. PAGE-13

Nested ‘if’ statements : When there are two or more condition to be satisfied but the checking of

one condition is dependent on another condition then if both condition are found to be true then

the decision is to be executed. The general form of statements are,

1] if (𝑥1)

If (𝑥2) 𝑥1,𝑥2 −logical expression

𝑦1; 𝑦1,𝑦2 −program statements

If 𝑥1 is true & 𝑥2 is true , then 𝑦1 is executed.

2] if (𝑥1)

If (𝑥2)

𝑦2;

else

𝑦1;

It 𝑥1 is true & 𝑥2 is true then 𝑦2 is executed but if 𝑥2 is false 𝑦1 is executed.

3] if ( 𝑥1)

{

If (𝑥2)

𝑦2;

else

𝑦1;

}

else

𝑦3;

If 𝑥1 is true & 𝑥2 are true then 𝑦2 is executed , if 𝑥2 is false 𝑦1 is executed, otherwise 𝑦3 is

executed.

Ex.1. Write a program to check whether given number is positive, negative or zero.

Sol : mian( )

{

float number;

printf(“Enter a number”);

scanf(“%f”, &number);

if (number<= 0.00)

{

T.Y.B.Sc. PAGE-14

If (number== 0)

printf(“Entered number is zero”);

else

printf(“Entered number %.2f is negative”, number);

}

else

printf(“Entered number %.2f is positive”, number);

}

Ex.2. Write a program to find the largest of three numbers.

Sol : main( )

{

float 𝑎, 𝑏, 𝑐;

printf(“Enter three numbers”);

scanf(“%f %f %f”, &𝑎 &b &c);

if (𝑎 > 𝑏)

{

If (𝑎 > 𝑐)

printf(“Largest number = %.2f”, 𝑎);

else

printf(“Largest number = %.2f”, 𝑐);

}

else

{

If (𝑏 > 𝑐)

printf(“Largest number = %.2f”, 𝑏);

else

printf(“Largest number = %.2f”, 𝑐);

}

}

Ex.3. Write a program to check whether given year is leap or not.

Sol : main ( )

{

int year;

T.Y.B.Sc. PAGE-15

printf (“Enter year”);

scanf (“%d”, & year);

If (year %4 == 0 )

{

if (year %100 == 0)

{

if (year %400 == 0)

printf (“Year %d is leap”, year);

else

printf (“ Year %d is not leap”, year);

}

else

printf(“Year %d is leap”, year);

}

else

printf(“Year %d is not leap”, year);

}

Ex.4. Write a program to find the roots of quadratic equation.

Sol : main( )

{

float 𝑎, b, c, decriminant, r1, r2;

printf(“Enter the coefficient of quadratic equation 𝑎, b, c”);

scanf(“%f %f %f”, &𝑎 &b &c);

decriminant = 𝑏 ∗ 𝑏 − 4 ∗ 𝑎 ∗ 𝑐;

if(discriminant > 0)

{

𝑟1 = (−𝑏 + 𝑠𝑞𝑟𝑡(𝑑𝑖𝑠𝑐𝑟𝑖𝑚𝑖𝑛𝑎𝑛𝑡))/(2 ∗ 𝑎);

𝑟2 = (−𝑏 − 𝑠𝑞𝑟𝑡(𝑑𝑖𝑠𝑐𝑟𝑖𝑚𝑖𝑛𝑎𝑛𝑡))/(2 ∗ 𝑎);

printf(“Roots are %.2f and %.2f”, r1, r2);

}

else if (discriminant = 0)

{

T.Y.B.Sc. PAGE-16

𝑟1 = 𝑟2 = −𝑏/(2 ∗ 𝑎);

printf(“Roots are %.2f and %.2f”, r1, r2);

}

else

{

printf(“Roots are imaginery”);

}

}

Ex.5. write a program to find the discount it price >5000 & no. of product >5 then discount is 5%

otherwise no discount.

Sol : main ( )

{

float a, b, discount;

printf (“enter the price &no. of product |n”)

scanf (“%f %f”, & a &b);

if(a>5000)

{

if(b>5)

{

discount= 𝑎 ∗ 𝑏 ∗ 0.5;

}

else

discount = 0;

}

printf (“the discount on the product is %g”, discount);

}

Ex.6. Define 𝑦 = 2𝑛 𝑖𝑓 𝑛 > 5

= 𝑛 + 2 𝑖𝑓 𝑛 < 5

= 𝑛 − 2 𝑖𝑓 𝑛 = 5

Write a program to find the value of 𝑦 where n is number.

Sol : main ( )

{

T.Y.B.Sc. PAGE-17

int n, y ;

print f (“enter the number \n”);

scan f (“%d”, & n);

if (𝑛 > 5)

{

𝑦 = 2 ∗ 𝑛;

}

else

if (𝑛 < 5)

{

𝑦 = 𝑛 + 2;

}

else

𝑦 = 𝑛 − 2;

print f (“The value of 𝑦 is %d” , 𝑦);

}

Ex,7. The marks obtained by a student in 5 different subjects are input. The student sets a grade as

percentage, the following rules .

Percentage above or equal to 80 - 𝐴 𝑔𝑟𝑎𝑑𝑒

Percentage between 60 to 79 - 𝐵 𝑔𝑟𝑎𝑑𝑒

Percentage between 40 to 59 - 𝐶 𝑔𝑟𝑎𝑑𝑒

Percentage below 40 - fail

Sol : main ( )

{

int 𝑚1, 𝑚2, 𝑚3, 𝑚4, 𝑚5, 𝑝𝑒𝑟;

printf (“Enter marks in five subjects”);

scanf (“%d, %d, %d, %d, %d”, &m1, &m2, &m3, &m4, &m5);

per = (𝑚1 + 𝑚2 + 𝑚3 + 𝑚4 + 𝑚5)/5;

if (per>= 80)

{

printf (“A grade”);

T.Y.B.Sc. PAGE-18

else

{

If (per >= 60)

printf (“B grade”);

else

{

If (per >= 40)

printf (“C grade”);

else

{

printf (“fail”);

}

}

}

}

}

Ex.8. In a company an employee is paid salary as under: if his basic salary is less than Rs.5000, then

HRA = 10% of basic salary & DA = 90% of basic salary. if his salary is either equal or above

Rs.5000 then HRA = Rs.1000 & DA = 98% of basic salary. write a program to find his gross salary.

Sol : /* Calculation of gross salary */

main ( )

{

float bs, gs, da, hra;

printf (“Enter basic salary”);

scanf (“%f”, & bs);

if(bs < 5000)

{

hra = bs *10/100;

da = bs*90/100;

}

else

T.Y.B.Sc. PAGE-19

{

hra = 1000;

da = bs*98/100;

}

gs = bs+hra+da;

printf (“Gross salary = Rs.%d”, gs);

}

Ex.9. A company insures it’s drivers in the following cases, if the driver is married, if the drivers is

unmarried male & above 30 years of age, If the driver is unmarried female & above 25 years of age.

Sol : main ( )

{

cha sex, ms;

int age;

print f (“Enter age, sex, marital status”);

scan f (“%d %c %c”, &age, &sex, &ms);

if (ms == ‘m’)

{

printf (“Driver is insured”);

}

else

{

If (sex == ‘m’)

{

If (age >30);

printf (“Driver is insured”);

}

else

{

printf (“Driver is not insured”);

}

else

{

T.Y.B.Sc. PAGE-20

If (age > 25)

{

printf (“Driver is insured”);

}

else

{

printf (“Driver is not insured”);

}

}

}

}

Ex.10. The program reads four values a, b, c & d & evaluates the ratio of ( a+b ) to ( c-d ) & prints the

result, it c-d is not equal to zero.

Sol : main ( )

{

int a,b,c,d;

float ratio;

printf (“Enter four integer values\n”);

scanf (“%d %d %d%d”, &a, &b, &c, &d);

if (c-d!=0)

{

ratio =(float) (a+b)/(float) (c-d);

print f (“ratio =%f \n”, ratio);

}

}

Ex.11. The formula for calculating z are given below,

𝑧 = 3𝑥2 − 7𝑥 + 2 for 𝑥 < 4.7

𝑧 = 3𝑥2 + 21.2𝑥 − 0.8 for 𝑥 = 4.7

𝑧 = 3𝑥2 + 21.3𝑥 − 1.6 for 𝑥 > 4.7

Write a program for evaluating of z.

Sol : main ( )

{

T.Y.B.Sc. PAGE-21

float x, z;

print f (“Enter the value of x”);

scan f(“%f”, &x);

if (𝑥 < 4.7)

{

𝑧 = 3 ∗ 𝑥 ∗ 𝑥 − 7 ∗ 𝑥 + 2;

else

{

If (𝑥 = 4.7);

{

𝑧 = 3 ∗ 𝑥 ∗ 𝑥 + 21.2 ∗ 𝑥 − 0.8

else

{

If (𝑥 > 4.7);

{

𝑧 = 3 ∗ 𝑥 ∗ 𝑥 + 21.3 ∗ 𝑥 − 1.6;

}

}

}

}

print f (“The value of z = %f, z”);

}

}

Ex.12. Write a program to compute 𝑓(𝑥)

𝑓(𝑥) = 1 +𝑥

1+𝑥2 for 𝑥 < 0,

= 0 for 𝑥 = 0,

= 1 −𝑥

1+𝑥2 for 𝑥 > 0,

Sol : main ( )

{

float 𝑥, 𝑓(𝑥);

printf (“enter the value of 𝑥”);

T.Y.B.Sc. PAGE-22

scanf (“%f”, &𝑥);

if (𝑥 < 0)

{

𝑓(𝑥) = 1 + (𝑥/1 + 𝑥 ∗ 𝑥);

else

{

if( 𝑥 = 0)

{

𝑓(𝑥) = 0

else

if (𝑥 > 0)

{

𝑓(𝑥) = 1 − (𝑥/1 + 𝑥 ∗ 𝑥);

}

}

}

}

printf (“The value of 𝑓(𝑥) = %𝑓", 𝑓(𝑥));

}

Ex.13. Evaluate a function 𝑓(𝑥),

𝑓(𝑥) = 𝐴𝑥2 + 𝐵𝑥 + 𝐶 𝑖𝑓 𝑥 < 𝐷

= 0 𝑖𝑓 𝑥 = 𝐷

= 𝐴𝑥2 + 𝐵𝑥 − 𝐶 𝑖𝑓𝑥 > 𝐷

for each value of 𝑥, 𝑝𝑟𝑖𝑛𝑡 𝑓(𝑥).

“ Switch” statement : The general form of the statement is,

switch (𝑥1)

{

case 𝑧1;

𝑦1;

𝑦2;

break; where 𝑥1 is variable to be compared &

𝑧1, 𝑧2 are the value of variables with which

T.Y.B.Sc. PAGE-23

case 𝑧2; 𝑥1 to bx compared.

𝑝1;

𝑝2;

break;

default: 𝑦1, 𝑦2

𝑎1; 𝑝1, 𝑝2

𝑎2; 𝑎1, 𝑎2 are program statement.

break;

Ex.1. Write a program of meaning of grade as follows.

Grade A – Excellent, Grade B – Good, Grade C – OK , Grade D – Mumm . . , Grade F – Fail

Sol : main( )

{

int grade;

printf(“Enter the grade”);

scanf(“%d”, &grade);

switch(grade)

{

case ‘A’ : printf(“Excellent\n”);

break;

case ‘B’ : printf(“Good\n”);

break;

case ‘C’ : printf(“OK \n”);

break;

case ‘D’ : printf(“Mummmm . . . . \n”);

break;

case ‘F’ : printf(“Fail \n”);

break;

default : printf(“ What is your grade ? \n”);

break;

} }

Ex.2. Write a program to choose ice cream flavour in ice cream parlour as follows.

T.Y.B.Sc. PAGE-24

1 : Chocolate , 2 : Vanilla , 3 : American dry fruits, 4 : Tuti fruit, 5 : Stra berry

,Sol : main( )

{

int choice;

printf(“Enter the choice”);

scanf(“%d”, & choice);

switch(choice)

{

case ‘1’ : printf(“Chocolate \n”);

break;

case ‘2’ : printf(“Vanilla \n”);

break;

case ‘3’ : printf(“American dry fruits \n”);

break;

case ‘4’ : printf(“Tuti fruit \n”);

break;

case ‘5’ : printf(“Stra berry \n”);

break;

default : printf(“ What is your choice ? \n”);

break;

}

}

“goto” statement : In ‘C’ language ‘goto’ statement is used to shift the control to any other

statement which is not in sequence. This is written as

goto p ; where p - label name.

then control is shifted to the statement where label name is written.

Ex.1. A computer manufacturing company has the following monthly compensation policy to their

sales persons : Minimum base salary : 15000

Bonus for every computer sold : 1000

Comission on the total monthly sales : 2 %

Assume that the sales price of each computer is fixed at the beginning of every month. Write a

program to compute a sale person’s gross salary.

T.Y.B.Sc. PAGE-25

Sol : main( )

{

int base_salary, no_of_computer_sold, commission, bonus, sale_price, gross_salary;

base_salary = 15000;

bonus = no_of_computer_sold*1000;

commission = no_of_computer_sold*sale_price*0.02;

gross_salary = base_salary + bonus + commission;

printf(“Gross salary = %d”, gross_salary);

}

Practical No.-5(A): Basic concept1. Write a C program that will obtain the length and width of a rectangle from the user and compute its area

and perimeter.2. Write a C program to find the area of a triangle, given three sides.3. Write a C program to find the simple interest, given principle, rate of interest and time.4. Write a C program to read a five digit integer and print the sum of its digits.5. Write a C program to convert a given number of days into months and days.6. A computer manufacturing company has the following monthly compensation policy to their sales persons:

Minimum base salary : 15000.00Bonus for every computer sold 1000.00Commission on the total monthly sales: 2 per cent

Assume that the sales price of each computer is fixed at the beginning of every month. Write a C program tocompute a sales person's gross salary.

Practical No.-6(A): Expressions and conditional statements1. Write a C program that determines whether a given integer is odd or even and displays the number and

description on the same line.2. Write a C program that determines whether a given integer is divisible by 3 or not and displays the number

and description on the same line.3. Write a C program that determines the roots of the quadratic equation ax2 + bx + c = 0, a * 0.4. Write a C program to print the largest of the three numbers using nested if ... else statement.5. Read four values a,b, c and d from the terminal and evaluates the ratio of (a + b) to (c - d)and prints the

result, if c - d is not equal to zero.

T.Y.B.Sc. PAGE-26