[go: up one dir, main page]

0% found this document useful (0 votes)
9 views24 pages

Assignment 3 Rojit

The document contains a series of multiple-choice questions (MCQs) and programming problems related to the C programming language, focusing on control structures, loops, functions, and basic programming concepts. It includes explanations for the answers to the MCQs and provides sample code for various programming tasks such as finding the largest number, checking for prime numbers, and calculating the sum of digits. The document serves as an assignment for a student named Rojit Shrestha in the Netscape section.

Uploaded by

Y-O-U-R Boy
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)
9 views24 pages

Assignment 3 Rojit

The document contains a series of multiple-choice questions (MCQs) and programming problems related to the C programming language, focusing on control structures, loops, functions, and basic programming concepts. It includes explanations for the answers to the MCQs and provides sample code for various programming tasks such as finding the largest number, checking for prime numbers, and calculating the sum of digits. The document serves as an assignment for a student named Rojit Shrestha in the Netscape section.

Uploaded by

Y-O-U-R Boy
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/ 24

Assignment 3

Student name: Rojit Shrestha

Student section: Netscape

MCQS

Q.N ANSWER OPTION WHY DO YOU THINK THE ANSWER IS RIGHT ?


1 C In C, an 'if' statement evaluates a Boolean expression, which
determines whether the code block inside the 'if' statement should
be executed based on whether the expression evaluates to true
(non-zero) or false (zero)
2 C A 'switch' statement in C can have any number of cases, allowing
for multiple potential values to be evaluated. Each case
corresponds to a specific value that the switch expression can take
3 C In C, the ? operator is used as the conditional (ternary) operator. It
allows for conditional execution in a compact form, typically used as
follows: condition ? expression_if_true : expression_if_false;.
4 B In C, the break statement is used to prematurely exit a loop (such
as for, while, or do-while loops). The continue statement, on the
other hand, is used to skip the current iteration and proceed to the
next iteration of the loop.
5 C The while statement is a loop structure in C. It repeatedly executes
a block of code as long as a specified condition is true. The other
options (if-else, switch, and case) are control flow statements but
not loop structures.
6 A This is the correct syntax for starting a do-while loop in C. The loop
will execute the statements inside the braces at least once and will
continue to execute as long as the specified condition is true.
7 B In a for loop in C, the three parts of the declaration (initialization,
condition, and increment/decrement) are separated by semicolons
8 B The purpose of the break statement in C is to exit the current loop
(such as for, while, or do-while) or to terminate a switch statement.
9 A The continue statement in C is used to skip the current iteration of a
loop and proceed with the next iteration. When continue is
encountered, the remaining statements in the loop for that iteration
are skipped, and the loop proceeds to the next iteration
10 C The switch statement in C compares the value of an expression
against multiple cases. Each case represents a potential value that
the expression can take, and the code block corresponding to the
matching case is executed.
11 A The keyword case is used to define a case in a switch statement.
Each case specifies a value that the switch expression can match,
and the code following the case will execute if there is a match.
12 B In C, the condition if(0) evaluates to false, so the code inside the
else block will be executed, resulting in "FALSE" being printed.
13 C The switch statement is not a loop in C. It is a control flow
statement that allows for multi-way branching based on the value of
an expression. The other options (for, while, and do-while) are all
types of loops that repeatedly execute a block of code based on a
condition.
14 B In a switch statement, the default keyword denotes the block of
code that will be executed if none of the specified cases match the
value of the switch expression. It acts as a fallback option.
15 B A do-while loop in C executes its body at least once, regardless of
the condition. This is because the condition is checked after the
loop body has executed.
16 B the first part of the && (logical AND) operation is false, the entire
expression evaluates to false (0) because in a logical AND
operation, if any operand is false, the result is false.
17 B When i is 3, the continue statement is executed, which skips the
printf("%d",i); statement for that iteration.
18 C Checks condition at the start of each iteration; used when iterations
are unknown.
19 B The code uses the ternary operator (condition) ? expression1 :
expression2. It evaluates the condition and returns expression1 if
the condition is true, otherwise it returns expression2.
20 C The while keyword is used in C to repeat a statement or a group of
statements as long as a given condition is true. The condition is
checked at the beginning of each iteration
21 BOTH A AND B In C, both while (1) and for (;;) are valid ways to create an infinite
loop.
22 D The switch statement allows you to execute different blocks of code
based on the value of a single variable or expression. It is specifically
designed for this purpose.
23 C In a switch statement, the break statement is used to exit the switch
block after a case is matched. If the break statement is omitted, the
program will fall through and execute the code for the next case(s) as
well, even if the condition for those cases is not met.
24 C The do-while loop is the only loop in C that checks its condition at the end
of the loop body. This means the loop body is guaranteed to execute at
least once, even if the condition is false initially.
25 B The continue statement is used in loops to skip the remaining code in the
current iteration and move to the next iteration of the loop. It does not
terminate the loop; it only skips the current iteration.

PROGRAMMING PROBLEMS

Q1.

#include <stdio.h>

int main() {
int num1, num2;

// Input two integers


printf("Enter the first integer: ");
scanf("%d", &num1);
printf("Enter the second integer: ");
scanf("%d", &num2);

// Check if the integers are equal


if (num1 == num2) {
printf("The two integers are equal.\n");
} else {
printf("The two integers are not equal.\n");
}

return 0;
}

Q2

#include <stdio.h>

int main() {
int num1, num2, num3;
// Input three numbers
printf("Enter three integers: ");
scanf("%d %d %d", &num1, &num2, &num3);

// Assume num1 is the largest initially


int largest = num1;

// Check if num2 is larger than the current largest


if (num2 > largest) {
largest = num2;
}

// Check if num3 is larger than the current largest


if (num3 > largest) {
largest = num3;
}

// Print the largest number


printf("The largest number is: %d\n", largest);

return 0;
}
Q3.

#include <stdio.h>

int main() {
float temperature;

// Input temperature in centigrade


printf("Enter the temperature in centigrade: ");
scanf("%f", &temperature);

// Display a suitable message based on the temperature


if (temperature < 0) {
printf("Freezing weather\n");
} else if (temperature >= 0 && temperature <= 10) {
printf("Very Cold weather\n");
} else if (temperature > 10 && temperature <= 20) {
printf("Cold weather\n");
} else if (temperature > 20 && temperature <= 30) {
printf("Normal in Temp\n");
} else if (temperature > 30 && temperature < 40) {
printf("Its Hot\n");
} else {
printf("Its Very Hot\n");
}

return 0;
}
Q4.

#include <stdio.h>

int main() {
char ch;

// Input a character
printf("Enter a character: ");
scanf("%c", &ch);

// Check if the character is an alphabet


if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
printf("'%c' is an alphabet.\n", ch);
}
// Check if the character is a digit
else if (ch >= '0' && ch <= '9') {
printf("'%c' is a digit.\n", ch);
}
// Otherwise, it is a special character
else {
printf("'%c' is a special character.\n", ch);
}

return 0;
}

Q5.

#include <stdio.h>

int main() {
int digit;
// Input a digit (0-9)
printf("Enter a digit (0-9): ");
scanf("%d", &digit);

// Display the digit in words using a switch statement


switch (digit) {
case 0:
printf("Zero\n");
break;
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
case 3:
printf("Three\n");
break;
case 4:
printf("Four\n");
break;
case 5:
printf("Five\n");
break;
case 6:
printf("Six\n");
break;
case 7:
printf("Seven\n");
break;
case 8:
printf("Eight\n");
break;
case 9:
printf("Nine\n");
break;
default:
printf("Invalid input! Please enter a digit between 0 and 9.\n");
}

return 0;
}
Q6.

#include <stdio.h>

// Function to calculate GCD (Greatest Common Divisor)


int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

// Function to calculate LCM using GCD


int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}

int main() {
int num1, num2;

// Input two numbers


printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);

// Calculate and display LCM


int result = lcm(num1, num2);
printf("The LCM of %d and %d is: %d\n", num1, num2, result);
return 0;
}

Q7.

#include <stdio.h>

int main() {
int num, count = 0;

// Input a number
printf("Enter an integer: ");
scanf("%d", &num);

// Handle the case for 0


if (num == 0) {
count = 1;
} else {
// Count the number of digits
while (num != 0) {
num = num / 10; // Remove the last digit
count++; // Increment the count
}
}

// Display the result


printf("Number of digits: %d\n", count);

return 0;
}
Q8.

#include <stdio.h>

int main() {
int num, i, isPrime = 1;

// Input a number
printf("Enter a positive integer: ");
scanf("%d", &num);

// Check if the number is less than 2


if (num < 2) {
isPrime = 0; // Numbers less than 2 are not prime
} else {
// Check for factors from 2 to sqrt(num)
for (i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = 0; // If divisible, it's not prime
break;
}
}
}

// Display the result


if (isPrime) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}

return 0;
}
Q9.

#include <stdio.h>
#include <math.h>
int main() {
int num, originalNum, remainder, n = 0, result = 0;

// Input a number
printf("Enter an integer: ");
scanf("%d", &num);

originalNum = num;

// Count the number of digits


while (originalNum != 0) {
originalNum /= 10;
n++;
}

originalNum = num;

// Calculate the sum of digits raised to the power of n


while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}

// Check if the number is Armstrong


if (result == num) {
printf("%d is an Armstrong number.\n", num);
} else {
printf("%d is not an Armstrong number.\n", num);
}

return 0;
}
Q10.

#include <stdio.h>
#include <math.h>

int main() {
int n, num, originalNum, remainder, digits, sum;
// Input the upper limit
printf("Enter the upper limit (n): ");
scanf("%d", &n);

printf("Armstrong numbers between 1 and %d are:\n", n);

// Loop through all numbers from 1 to n


for (num = 1; num <= n; num++) {
originalNum = num;
digits = 0;
sum = 0;

// Count the number of digits


while (originalNum != 0) {
originalNum /= 10;
digits++;
}

originalNum = num;

// Calculate the sum of digits raised to the power of digits


while (originalNum != 0) {
remainder = originalNum % 10;
sum += pow(remainder, digits);
originalNum /= 10;
}

// Check if the number is Armstrong


if (sum == num) {
printf("%d\n", num);
}
}

return 0;
}
Q11.

# include<stdio.h>
int main(){
for(;;){
printf("Hello World \n");
}
return 0;
}
OUTPUT: IT WILL PRINT HELLO WOLRD INREPEAT BECAUSE FOR(;;) DEMONSTRATE INFINITE LOOP.

Q12

#include <stdio.h>

int main() {
int num, sum = 0, remainder;

// Input a number
printf("Enter an integer: ");
scanf("%d", &num);

// Calculate the sum of digits


while (num != 0) {
remainder = num % 10; // Extract the last digit
sum += remainder; // Add the digit to the sum
num = num / 10; // Remove the last digit
}
// Display the result
printf("Sum of digits: %d\n", sum);

return 0;
}

CHAPTER FUNCTIONIN C

MCQS

Q NO ANSWER WHY DO YOU THINK THE ANSWER IS RIGHT ?


1 A In C, a function declaration (also known as a function prototype)
specifies the function's return type, name, and parameters.
2 B The void keyword is used in C to indicate that a function does not
return any value.
3 B A function prototype in C is a declaration of a function that specifies:
The return type of the function.
The name of the function.
The types of parameters (arguments) the function accepts.
4 B In C, a function is called by using its name followed by parentheses
(). Inside the parentheses, the arguments (values) are passed to the
function, separated by commas.
5 B Recursion in C programming occurs when a function calls itself
directly or indirectly. This technique is used to solve problems that
can be broken down into smaller, similar subproblems.
6 B In C, if the return type of a function is not explicitly specified, the
compiler assumes it to be int by default. This is part of the implicit
int rule in older versions of the C language (pre-C99).
7 A A function prototype in C specifies:
The return type of the function.
The name of the function.
The types and names of parameters (arguments) the function
accepts.
8 B A function definition in C consists of:
The function header: Specifies the return type, function name, and
parameters.
The function body: A block of code enclosed in curly braces {} that
defines what the function does.
9 C Function Header: The first line of a function definition must include
the return type, function name, and parameters to define the
function's signature.
Compiler Requirements: The compiler uses this information to
understand how the function should be called and what it returns.
10 B The add function doubles its input.
The doubleadd function calls add twice, effectively quadrupling its
input.
For doubleadd(2), the result is 8.
11 A The subtract function takes two integers, x and y, and returns their
difference (x - y).
When subtract(10, 6) is called, it computes 10 - 6 = 4 and returns 4.
The printf statement in main prints the result, which is 4.
12 C The multiply function takes two integers, a and b, and returns their
product (a * b).
When multiply(3, 4) is called, it computes 3 * 4 = 12 and returns 12.
The printf statement in main prints the result, which is 12.
13 B The static variable num retains its value between function calls.
Each call to printnum prints the current value of num and then
decrements it by 1.
The program outputs the values 16, 15, and 14 in sequence.
14 B The test function takes an integer x and returns its double (x * 2).
When test(5) is called, it computes 5 * 2 = 10 and returns 10.
The printf statement in main prints the result, which is 10
15 BOTH A AND B Always specify the return type in function declarations and
definitions.
Remember that a function can only return one value directly. Use
pointers or structures for multiple values.
Understand the difference between pass by value and pass by
reference in C.
16 B when we call fibonacci (4), it tells us there are 3 ways to reach the
4th step
17 A From the fourth call: sumdigits(1) returns 1 + 0 = 1
From the third call: sumdigits(12) returns 2 + 1 = 3
From the second call: sumdigits(123) returns 3 + 3 = 6
From the first call: sumdigits(1234) returns 4 + 6 = 10
So, when you call sumdigits(1234), the final return value is 10.
18 A This means that when you pass an argument to a function, a
copy of the argument's value is made. Any changes made to
the parameter inside the function do not affect the original
argument outside the function.
19 C A global variable in C is accessible from any function in the program
after its declaration. This means that once a global variable is
defined, it can be used in any function, not just the one where it was
declared or the main function.
20 A A local variable is a variable that is declared within a function (or a
block of code) and can only be accessed within that function. It is not
accessible outside of the function where it is declared.
21 A A static variable in C retains its value even after the function in
which it is declared has exited. This means that the next time
the function is called, the static variable will still hold the value
it had when the function last returned. Static variables are
initialized only once, and if not explicitly initialized, they are
automatically initialized to zero.
22 C A register variable is a type of local variable that is stored in a
CPU register instead of in RAM. This allows for faster access
to the variable, as registers are much quicker to access than
memory. However, the number of registers is limited, so the
compiler may choose not to store a register variable in a
register if it deems it unnecessary.
23 A The inline keyword in C is used to suggest to the compiler that it
should attempt to expand the function's code inline at the point of
each call, rather than performing a traditional function call. This can
reduce the overhead associated with function calls (such as pushing
arguments onto the stack and jumping to the function's code),
potentially leading to faster execution times, especially for small,
frequently called functions. However, it's important to note that the
compiler may choose to ignore the inline suggestion.
24 C In the provided code snippet, printf("%p\n", main); prints the
memory address of the main function. The %p format specifier is
used to print a pointer (in this case, the address of the function). So,
when you run this code, it will output the memory address where
the main function is located in memory.

You might also like