Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Mid-1 (QA 2024)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

1.What is the difference between a variable and a constant in programming?

Ans) A variable is a value that can change during program execution, while a constant is a
value that remains fixed and cannot be altered once defined.
Example for variable
int x = 10;
x = 20; // Value of x changes from 10 to 20
Example for Constant
const float PI = 3.14;
// PI cannot be changed later in the program
Here const is a key word which tells PI has a constat value.
2. What function is used to print output in C? Give its syntax.
Ans) The function used to print output in C is printf().
printf("format string", arguments);
The format string contains text to display and format specifiers (like %d for integers, %f for
floating-point numbers, etc.).
The arguments ( means variable names ) are the values to be printed, which correspond to
the format specifiers.
3. How do logical operators differ from arithmetic operators in C?
Ans) In C, logical operators are used to evaluate logical expressions (true or false), while
arithmetic operators perform mathematical calculations.
Common operators: && (AND), || (OR), ! (NOT)
Common operators: + (addition), - (subtraction), * (multiplication), / (division), % (modulus).
4. How does operator precedence affect the evaluation of expressions?
Operator precedence in C determines the order in which operators are evaluated in an
expression. Operators with higher precedence are evaluated before those with lower
precedence.
int result = 5 + 3 * 2;
Here, * has higher precedence than +, so 3 * 2 is evaluated first, then + 5.
5. Identify the role of an identifier. Choose various basic datatypes used in C?
An identifier is a name given to elements like variables, functions, arrays, etc., to uniquely
identify them in the program.
It allows programmers to refer to these elements throughout the code.
Identifiers must follow naming rules, such as starting with a letter or underscore and not
containing special characters or spaces.
Basic Data Types in C:
1. int: Used to store integers (whole numbers).
o Example: int age = 25;
2. float: Used to store floating-point (decimal) numbers.
o Example: float height = 5.9;
3. char: Used to store a single character.
o Example: char grade = 'A';
4. double: Used to store double-precision floating-point numbers, providing more
precision than float.
o Example: double salary = 12345.67;
5. void: Represents the absence of any data type, commonly used for functions that don't
return a value.
o Example: void displayMessage() { /* code */ }
6. Identify different types of operators and explain any five of them with an
example code snippet.

Different Types of Operators in C:


1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Unary Operators
7. Ternary (Conditional) Operator
8. Increment/Decrement Operators
Explanation of Five Operators with Code Snippets:

1. Arithmetic Operator (+):


o Used for basic mathematical operations like addition, subtraction, etc.
int a = 5, b = 3,sum;
sum = a + b; // sum = 8
2. Relational Operator (==):
o Used to compare two values.
int x = 10, y = 20;
if (x == y)
{
printf("x is equal to y");
}
else
{
printf("x is not equal to y"); // This will print
}

3. Logical Operator (&&):


o Used to evaluate multiple conditions.
int a = 5, b = 10;
if (a > 0 && b > 0)
{
printf("Both are positive numbers");
}

4. Assignment Operator (=):


o Assigns a value to a variable.
int num;
num = 25; // num now holds the value 25

5. Ternary Operator (? :):


o A shorthand for if-else conditions.
int x = 10;
int result = (x > 5) ? 100 : 200; // result = 100 because x > 5

7. Construct an algorithm to find the volume of the cuboid. (Volume of cuboid=lbh)


Step 1: Start
Step 2: Declare variables l,b,h and cuboid.
Step 3: Read l,b,h values.
Step 4: Calculate Cuboid using, cuboid = (l*b*h).
Step 5:Display the result of cuboid.
Step 6:Stop.
8. Accept any two numbers from the user, if the first number is greater than second then
print the sum of these two numbers, otherwise print their difference. Build this program
using ternary operator.
#include <stdio.h>
int main()
{
int num1, num2, result;
printf("Enter num1 and num2 number: ");
result = (num1 > num2) ? (num1 + num2) : (num1 - num2);
printf("The result is: %d\n", result);
return 0;
}
9) Build a C program to accept any five-digit number from the user and print the value
of remainder after dividing by 3.
#include <stdio.h>
int main()
{
int number, remainder;
printf("Enter a five-digit number: ");
scanf("%d", &number);
remainder = number % 3;
printf("The remainder when %d is divided by 3 is: %d\n", number, remainder);
return 0;
}
10) Build a program to accept the principal, rate, and number of years from the user
and find out the simple interest. Note: Simple interest formula is (P*T*R)/100.
# include <stdio.h>
int main()
{
int P, R, T;
float SI;
printf(“Enter P,T,R values\n”);
scanf("%d%d%d", &P,&T,&R);
SI = (P*T*R)/100.0;
printf("The Simple interest is %f", SI);
return 0;
}
11)Accept the radius of the circle from the user and identify the area and perimeter of
the circle in C language.
Note: Area of circle= Pie*r*r, Perimeter of circle=2*Pie*r.
#include <stdio.h>
int main()
{
float radius, perimeter, area,PI;
PI=3.14;
scanf("%f", & radius);
perimeter = 2 * PI * radius;
area = PI * radius * radius;
printf("Area of circle: %f\n", area);
printf("Perimeter of circle: %f\n", perimeter);
return 0;
}
12.Accepts marks in five subjects from the user and identify the total percentage of the marks
using C program.
#include <stdio.h>
int main()
{
int s1,s2,s3,s4,s5
float total, percentage;
scanf("%f%f%f%f%f",&s1,&s2,&s3,&s4,&s5);
total = s1 + s2 + s3 + s4 + s5;
percentage = (total / 500.0) * 100;
printf("Percentage = %f", percentage);
return 0;
}
Unit-2
1. What is control flow in programming?
• In C language, control flow refers to the order in which individual statements, or
functions are executed or evaluated.
• By default, C programs execute statements sequentially, but
• control flow statements allow you to change this order based on conditions,
branching statements, Or loops,
2) How does the if-else statement work? Give its syntax

if-else:
This is a bi-directional conditional control statement.
This statement is used to test a condition and take one of the two possible actions.
 If the condition is true,(non-Zero) then a single statement or a block of statements is
executed (inside of if block).
 Otherwise another single statement or a block of statements is executed (inside of else
block).
Syntax:-
if(condition)
{
statement 1;

}
else
{
statement 2;
}
Next Statement; ( it is optional)

3) What is the difference between while and do-while loops?

The main difference between a while loop and a do-while loop is in when the condition is
checked:
 while loop: The condition is checked before the loop executes. If the condition is
false initially, the loop may not execute even once.
while (condition)
{
// Code to execute
}
 do-while loop: The loop executes at least once, and the condition is checked after the
loop has executed.
do
{
// Code to execute
} while (condition);

In short, while is a pre-condition (Entry) loop, and do-while is a post-condition(Exit) loop.


4.What does the break statement do?

The break statement is used inside loops and switch statements.


Sometimes it becomes necessary to come out of the loop even before the loop condition
becomes false.
In such a situation, the break statement is used to terminate the loop.

• This statement causes an immediate exit from that loop in which this statement
appears.
• It can be written as:
break;

5. Build a program to print prime numbers from 1 to 99


//program to find prime numbers upto given Range number(n)
#include<stdio.h>
int main()
{
int Range,i,j,count=0;
printf("Enter Prime numbers Range\n");
scanf("%d",&Range);
for(i=1; i<=Range; i++)
{
count=0;
for(j=1; j<=i; j++)
{
if(i%j==0)
{
count=count+1;
}
}
if(count==2)
printf("%d ",i);
}
return 0;
}
6.Build a program to find out the grade of a student when the marks of 4 subjects are
given. The method of assigning grade is as below,
a) percentage>=85, Grade A
b) percentage<85 and percentage>=70, Grade B
c) percentage<70 and percentage>=50, Grade C
d) percentage<55 and percentage>=40, Grade D
e) percentage<40, Grade E
// program to find the Grade of a student
#include<stdio.h>
int main()
{
int m1,m2,m3,m4;
float total,per;
char grade;
printf("Enter Marks of 4 subjects\n");
scanf("%d%d%d%d",&m1,&m2,&m3,&m4);
total=m1+m2+m3+m4;
per=total/4;
if(per>=85)
{
grade='A';
}
else if(per>=70)
{
grade='B';
}
else if(per>=55)
{
grade='C';
}
else if(per>=40)
{
grade='D';
}
else
{
grade='E';
}
printf("Percentage is %f\n Grade is %c\n",per,grade);
return 0;
}
7. Identify the use of if and if-else decision-making statements in C language.

a) Simple if or Null else:


• This is a uni-directional conditional control statement.
• A set of statements is executed if the condition is true(non-zero). If the condition is
false(Zero), the code inside the if block is skipped.

• Syntax:
if(condition)
{
Statement 1;
}
Next Statement; ( it is optional)

b) if-else:
This is a bi-directional conditional control statement.
This statement is used to test a condition and take one of the two possible actions.
 If the condition is true,(non-Zero) then a single statement or a block of statements is
executed (inside of if block).
 Otherwise another single statement or a block of statements is executed (inside of else
block).
Syntax:-
if(condition)
{
statement 1;

}
else
{
statement 2;
}
Next Statement; ( it is optional)
8.Make use of switch-case-default statement to explain its working.

switch:
• This is a multi-directional conditional control statement. Sometimes there is a need in
program to make a choice among a number of alternatives. For making this choice,
we use the switch statement. This can be written as.

• Syntax:
switch(expression)
{
case constant1:
Block_A1;
break;
case constant2:
Block_A2;
break;
……….
case constantN:
Block_AN;
break;
default: Block_D;
}
Out_of_switch statements;

• Firstly, the switch expression is evaluated, then the value of this expression is
compared one by one with every case constant.
• If the value of the expression matches with any case constant, then all statements
under that particular case are executed. If none of the case constants matches with the
value of the expression, then the block of statements under 'default' is executed.
• 'default' is optional; if it is not present and no case matches, then no action takes
place.
• These cases and default can occur in any order.
9.Identify the difference while loop with for loop with suitable example?

The primary difference between a while loop and a for loop lies in how they control the loop
execution and initialization:
1. Syntax and Use
while loop: Typically used when the number of iterations is not known beforehand. It keeps
executing as long as the condition remains true.
Example:
int i = 0;
while (i < 5)
{
printf("%d ", i);
i++;
}
Output: 0 1 2 3 4
o Here, the initialization (i = 0), condition (i < 5), and increment (i++) are
handled separately.
o
for loop: Typically used when the number of iterations is known in advance. It has a more
concise structure with initialization, condition, and increment in one line.
Example:
for (int i = 0; i < 5; i++)
{
printf("%d ", i);
}
Output: 0 1 2 3 4
o In a for loop, the initialization (i = 0), condition (i < 5), and increment (i++)
are bundled into the loop's structure.
2. Key Differences:
 while loop: Suited for scenarios where the number of iterations depends on external
conditions that might change during runtime.
 for loop: Ideal for situations where the iteration count is fixed or predictable
10 Build a program to print the below kind of patterns for the given value of n.

a)
#include<stdio.h>
int main()
{
int i,j;
for(i=1; i<=4; i++)
{
for(j=1; j<=i; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}

b)
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=4;j>=i;j--)
{
printf("*");
}
printf("\n");
}
return 0;
}
c)
#include <stdio.h>
int main()
{
int i, j, space, rows = 4;
for(i = 1; i <= rows; i++)
{

for(space = 1; space <= rows - i; space++)


{
printf(" ");
}

for(j = 1; j <= i; j++)


{
printf("* ");
}

printf("\n");
}
return 0;
}

11)Build a program to find the sum of the series up to n terms where n is an integer
entered by the user.
1+2+4+7+11+16+....

#include <stdio.h>

int main()
{
int n,i,term = 1, sum = 0, diff = 1;

printf("Enter the number of terms: ");


scanf("%d", &n);

for (i = 1; i <= n; i++)


{
sum = sum + term; // Add the current term to the sum
term = term + diff; // Update the term for the next value
diff= diff + 1; // Increase the difference by 1 for the next term
}
printf("Sum of the series up to %d terms is: %d\n", n, sum);

return 0;
}
12) Build a program to find out the value of x raised to the power y, where x and yare
positive integers without using pow() function.

#include <stdio.h>

int main()
{
int x, y, i, result = 1;

printf("Enter the base (x): ");


scanf("%d", &x);
printf("Enter the exponent (y): ");
scanf("%d", &y);

// Multiply x, y times
for (i = 1; i <= y; i++)
{
result = result * x;
}

printf("%d raised to the power of %d is: %d\n", x, y, result);

return 0;
}

You might also like