0% found this document useful (0 votes)
24 views8 pages

Me 1ppsc03.10.2024finalsolutionscse2

The filr

Uploaded by

Gurram Thanuja
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)
24 views8 pages

Me 1ppsc03.10.2024finalsolutionscse2

The filr

Uploaded by

Gurram Thanuja
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/ 8

Minor Exam- I Question Paper

URR-24 Write your Roll number


KAKATIYA INSTITUTE OF TECHNOLOGY & SCIENCE, WARANGAL
(An Autonomous Institute under Kakatiya University, Warangal)
FACULTY OF ENGINEERING AND TECHNOLOGY
SEMESTER I
BRANCH CSE
COURSE CODE U24CS104
COURSE NAME PROGRAMMING FOR PROBLEM SOLVING WITH C

Time: 45 [Max. Marks: 25


Minutes
Date:03.10.2024
Note: 1. Answer all the questions.
Marks CDLL CO
1. Short answer type questions covering all the contents of Unit-I.
i. Write the typical (generic) structure of a C program [1] R CO1

ii. Define a keyword. Write any five keyworks in C language [1] R CO1
Keywords in C language has fixed meaning given by the C developers which cannot
be changed. Keywords serve as basic building blocks for program statements.
iii. Define a variable in C language. Identify the valid variable [1] R CO1
names from the following by applying naming rules.
a) 2024CSE
b) classCount
c) int
d) cse_2024

A variable is a data name that may be used to store a data value.


Variable naming rules:
i. Names can contain letters, digits and underscores.
ii. Names must begin with a letter or an underscore (_)
iii. Names are case-sensitive ( myVar and myvar are different variables)
iv. Names cannot contain whitespaces or special characters like !, #, %, etc.
v. Reserved words (such as int ) cannot be used as names.
Valid variable names are:
b) classCount d) cse_2024

iv. What is the output of the following C program? [2] U CO1


int main( )
{
int a=9, b=12, c;
c = a & b;
printf(“Left Shift: %d”, a<<2);
printf(“Bitwise AND: %d”, c);
return 0;
}

OUTPUT:
Left Shift: 36
Bitwise AND: 8

2. Long answer type questions covering contents of Unit-I of the syllabus


i. Define an algorithm and List the characteristics of an algorithm [3] Ap CO1

Develop an algorithm to input a number from the user and to [3]


check whether the number is Even number or Odd number.

Write a C program to read four numbers from the user and print [4]
their sum, product and average.

An algorithm is a finite sequence of well-defined instructions, typically to solve a specific


problem or perform a computation. It takes some input, processes it, and produces an output,
ensuring that the process terminates after a finite number of steps.

Characteristics of an Algorithm
1. Finiteness: The algorithm must terminate after a finite number of steps.
2. Definiteness: Each step of the algorithm must be precisely defined.
3. Input: An algorithm must take zero or more inputs.
4. Output: The algorithm should produce at least one output.
5. Effectiveness: All operations in the algorithm should be simple and basic enough to be
carried out, ideally, by a human or a machine.
Algorithm to Check Whether a Number is Even or Odd

1. Step 1: Start.
2. Step 2: Input the number n.
3. Step 3: Check if n % 2 == 0.
o If true, print "Even number."
o If false, print "Odd number."
4. Step 4: Stop.

C Program:
#include <stdio.h>

int main() {
int num1, num2, num3, num4;
int sum, product;
float average;

// Input four numbers from the user


printf("Enter four numbers:\n");
scanf("%d %d %d %d", &num1, &num2, &num3, &num4);

// Calculate sum, product, and average


sum = num1 + num2 + num3 + num4;
product = num1 * num2 * num3 * num4;
average = sum / 4.0;

// Print the results


printf("Sum: %d\n", sum);
printf("Product: %d\n", product);
printf("Average: %.2f\n", average);

return 0;
}

ii. Define an Operator & an Expression in C language. List the [3] Ap CO1
Types of operators.

Apply the Operator precedence and associativity rules to solve [4+3]


the following expressions in steps.
i. 5*2>6 && (4-8/3+7) || 5<=5
ii. A+B-C/D-(++E%A*B+F)
where (A=10, B=6, C=8, D=3, E=44, F=15)

Definition of an Operator and an Expression in C

• Operator: An operator in C is a symbol that tells the compiler to perform specific


mathematical, logical, or relational operations. For example, +, -, *, /, and == are all
operators.
• Expression: An expression in C is a combination of variables, constants, and operators
that are evaluated to produce a result. For example, a + b * c is an expression.

Types of Operators in C
1. Arithmetic Operators: +, -, *, /, % (modulus)
2. Relational Operators: ==, !=, >, <, >=, <=
3. Logical Operators: && (AND), || (OR), ! (NOT)
4. Bitwise Operators: &, |, ^, ~, <<, >>
5. Assignment Operators: =, +=, -=, *=, /=, %=
6. Increment/Decrement Operators: ++, --
7. Conditional (Ternary) Operator: ? :
8. Comma Operator: ,
9. Sizeof Operator: sizeof()
10. Pointer Operators: *, &

Applying Operator Precedence and Associativity

i. Expression: 5*2>6 && (4-8/3+7) || 5<=5

• Step 1: Handle the arithmetic operations first (*, /, +, -).


o 5 * 2 = 10
o 8 / 3 = 2 (integer division in C)
o Expression becomes: 10 > 6 && (4 - 2 + 7) || 5 <= 5
• Step 2: Solve within parentheses.
o 4-2+7=9
o Expression becomes: 10 > 6 && 9 || 5 <= 5
• Step 3: Handle relational operations (>, <=).
o 10 > 6 is true (or 1)
o 5 <= 5 is true (or 1)
o Expression becomes: 1 && 9 || 1
• Step 4: Handle logical AND (&&).
o 1 && 9 is true (non-zero value is considered true)
o Expression becomes: true || 1
• Step 5: Handle logical OR (||).
o true || 1 is true

Final result: true (or 1 in C).

ii. Expression: A + B - C / D - (++E % A * B + F) where (A = 10, B = 6, C = 8, D = 3,


E = 44, F = 15)
Step 1: Increment E using ++E.
++E becomes 45
Expression becomes: A + B - C / D - (45 % A * B + F)
Step 2: Handle modulus (%), division (/), and multiplication (*).
45 % 10 = 5
5 * 6 = 30
8/3=2
Expression becomes: A + B - 2 - (30 + F)
Step 3: Add and subtract within parentheses.
30 + 15 = 45
Expression becomes: A + B - 2 - 45
Step 4: Substitute values of A and B.
10 + 6 - 2 - 45
Step 5: Perform addition and subtraction.
10 + 6 = 16
16 - 2 = 14
14 - 45 = -31
Final result: -31
(OR)
iii. Define a flowchart and List the symbols of a flowchart with their [3] Ap CO1
purpose.

Draw a flow chart that takes diameter of a circle as input and [3]
computes and prints the Area and Circumference of that circle.

Write a C Program that takes diameter of a circle as input and [4]


computes and prints the Area and Circumference of that circle.

A flowchart is a visual representation of the sequence of steps and decisions needed to


perform a process. It uses various symbols to represent different types of actions or
operations, making it easy to follow and understand how a particular task is
performed.

C Program:
#include <stdio.h>
#define PI 3.14159 // Define the value of Pi

int main() {
float diameter, radius, area, circumference;
// Input the diameter of the circle
printf("Enter the diameter of the circle: ");
scanf("%f", &diameter);
// Calculate the radius
radius = diameter / 2;
// Calculate the area
area = PI * radius * radius;
// Calculate the circumference
circumference = 2 * PI * radius;
// Print the results
printf("Area of the circle: %.2f\n", area);
printf("Circumference of the circle: %.2f\n", circumference);

return 0;
}

SAMPLE OUTPUT:
Enter the diameter of the circle: 2
Area of the circle: 3.14
Circumference of the circle: 6.28

Flowchart:

iv. Develop a C program to find the value of a2+b2+2ab. The values [5] Ap CO1
of a and b input through the keyboard.

Explain the usage of conditional operator and write its syntax. [2]

Develop a C program that receives two numbers from the [3]


keyboard, find the biggest of these two numbers using
conditional operator.

C Program to Find the Value of a2+b2+2ab

#include <stdio.h>
int main() {
int a, b, result;

// Input values of a and b


printf("Enter the value of a: ");
scanf("%d", &a);

printf("Enter the value of b: ");


scanf("%d", &b);

// Calculate a^2 + b^2 + 2ab


result = (a * a) + (b * b) + (2 * a * b);

// Print the result


printf("The result of a^2 + b^2 + 2ab is: %d\n", result);

return 0;
}

SAMPLE OUTPUT:
Enter the value of a: 1
Enter the value of a: 2
The result of a^2 + b^2 + 2ab is: 9

Conditional (Ternary) Operator in C

The conditional operator (also known as the ternary operator) is a shorthand way to
evaluate an expression based on a condition. It consists of three parts: a condition, a result for
true, and a result for false.

Syntax:
condition ? expression_if_true : expression_if_false;

• Condition: This is the condition to be checked (e.g., a > b).


• Expression if true: The result when the condition is true.
• Expression if false: The result when the condition is false.

The conditional operator is particularly useful for replacing simple if-else constructs in a
concise manner.

C Program to Find the Biggest of Two Numbers Using Conditional Operator

#include <stdio.h>

int main() {
int num1, num2, largest;

// Input two numbers from the user


printf("Enter the first number: ");
scanf("%d", &num1);

printf("Enter the second number: ");


scanf("%d", &num2);
// Use conditional operator to find the largest number
largest = (num1 > num2) ? num1 : num2;

// Print the largest number


printf("The biggest number is: %d\n", largest);
return 0;
}
SAMPLE OUTPUT:
Enter the first number:12
Enter the second number:25
The biggest number is: 25

S. No Faculty Name
1. Dr. P. Niranjan, Professor
2. Dr. P. Vijay Kumar, Associate Professor
3. Sri. D. Naveen Kumar, Assistant Professor
4. Smt. R. Radhika, Assistant Professor
5. Sri. A. Venkatesh, Assistant Professor

You might also like