Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

PROGRAMMING IN C ( PRACTICE ASSIGNMENT2FOR EXAM )(BTPS101-18), Study notes of C programming

THIS PDF IS PRACTICING IMP QUES FOR EXAMS FOR C .TOPICS INCLUDES OPERATORS PRECEDENCE, CONDITIONAL BRANCHING STATEMENTS, ARITHMETIC EXPRESSIONS. for,while and do while loops,PROGRAM FOR for LOOP.MY NOTES IS EASY TO UNDERSTAND AND IS REASONABLE WITH THE PRICE. CHECK IT OUT .

Typology: Study notes

2024/2025

Available from 07/04/2025

KANOYA2786
KANOYA2786 🇮🇳

9 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
💡
PROGRAMMING FOR PROBLEM SOLVING
IMPORTANT PRACTICE QUESTIONS (FOR,WHILE ,DO WHILE LOOP,IF ,IF ELSE,
CONDITIONAL BRANCHING PROGRAMS )
1. What is operator precedence in C?
Operator precedence in C dictates the order in which the operators will be evaluated in an
expression. Associativity, on the other hand, defines the order in which the operators of the
same precedence will be evaluated. Associativity can occur from either left to right or right to
left.
Example:
1. x = 10 / 10 + 20
1 + 20 = 21
2. 10 / (10 + 20)
10 / 30 = 0.3
Without rules, different solutions are obtained. Therefore, precedence is defined; division has
precedence over addition, so case 1 is correct.
Operator Precedence and Associativity Table
NAME
ASSOCIATIVITY
PRECEDENCE
() function call, [] Array
Subscript, . Dot, -> Arrow
Left to Right
14
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download PROGRAMMING IN C ( PRACTICE ASSIGNMENT2FOR EXAM )(BTPS101-18) and more Study notes C programming in PDF only on Docsity!

💡 PROGRAMMING FOR PROBLEM SOLVING

IMPORTANT PRACTICE QUESTIONS (FOR,WHILE ,DO WHILE LOOP,IF ,IF ELSE,

CONDITIONAL BRANCHING PROGRAMS )

1. What is operator precedence in C?

Operator precedence in C dictates the order in which the operators will be evaluated in an expression. Associativity , on the other hand, defines the order in which the operators of the same precedence will be evaluated. Associativity can occur from either left to right or right to left.

Example:

  1. x = 10 / 10 + 20 1 + 20 = 21
  2. 10 / (10 + 20) 10 / 30 = 0.

Without rules, different solutions are obtained. Therefore, precedence is defined; division has precedence over addition, so case 1 is correct.

Operator Precedence and Associativity Table

NAME ASSOCIATIVITY PRECEDENCE

() function call, [] Array Subscript,. Dot, -> Arrow

Left to Right 14

++ Increment, -- Decrement

Right to Left 13

~ one's compliment, & address operator

  • Multiplication, % Modulus, / Division

Left to Right 12

  • Addition, - Subtraction Right to Left

2. Name three conditional branching statements along with their

syntax.

The conditional branching statements help to jump from one part of the program to another depending on whether the condition is satisfied or not.

(i) if statement

The if statement is the most simple decision-making statement. It is used to decide if a certain condition is true, then a block of code or statement is executed; otherwise not.

Syntax:

if (condition) { statement 1 ; // ... statement x; }

include <stdio.h>

int main() { int x = 65 , y = 35 , z = 2 ; if (x > y) { if (x > z) { printf("x is greater than y and z"); } } printf("End of program"); }

(iii) if-else Statement

The if statement executes if a specified condition is true. If the condition is false, the else statement can be executed.

Syntax:

if (condition) { Statement 1 ; } else { Statement 2 ; }

Example:

include <stdio.h>

int main() { int x = 10 ; if (x % 2 == 0 ) { printf("Even"); } else { printf("Odd"); } }

3. Explain Arithmetic expression with the help of suitable example.

An arithmetic expression consists of operators and operands. It is used to perform calculations to achieve a specific goal or task.

Example: C = A + B;

Operands : Operands are the values or variables on which operations are being performed. In the above given expression, A and B are the operands. ● Operator : The symbols which are used to perform operations on operands are known as operators. In the above given example, the addition + sign is the arithmetic operator.

Full Example Code:

C

include <stdio.h>

A for loop executes the statements of a program repeatedly several times until a given condition returns false. It is a pre-test loop.

Syntax:

for (initialization; condition; update) { statements; }

Example:

include <stdio.h>

int main() { for (int i = 0 ; i <= 10 ; i++) { printf("%d\n", i); } return 0 ; }

Output:

while loop:

It is a pre-test loop; it first tests a conditional expression. As long as the conditional expression is true, the loop body statement will be executed.

Syntax:

C

while (condition) { Statement; update (increase or decrease); }

Example:

include <stdio.h>

int main() { int i = 0 ; while (i < 10 ) { printf("%d\n", i); i++; } return 0 ;

include <stdio.h>

int main() { int a = 1 ; do { printf("%d\n", a); a++; } while (a < 10 ); return 0 ; }

Output:

5. Construct a table of 2 using for loop.

Example:

int main() { int i; for (i = 1 ; i <= 10 ; i++) { printf("2 x %d = %d\n", i, 2 * i); } return 0 ; }

Output:

2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20