Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

C Program Solution

Uploaded by

bdserver2407
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

C Program Solution

Uploaded by

bdserver2407
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

GROUP – A

9's complement of (77.85)10 is:


Answer: a) 22.14

Which of the following can be used to represent signed numbers?


Answer:
a) Sign Magnitude System
b) Signed 1's Complement System
c) Signed 2's Complement

char str[100]="Hello World"; How many bytes are allocated by the above declaration?
Answer: a) 100

What is the size of the character data type?


Answer: a) 1 byte

What will be the output of the following code?

int main() {

int i = 5;

printf("%d,%d", sizeof(++i), i);

return 0;

Explanation:
The sizeof operator is evaluated at compile time and does not evaluate its operand, so ++i is not
incremented. The size of an int in most systems is 2 bytes.
Answer: b) Compilation error (if sizeol is a typo)
Otherwise, the correct result is 2,5.

What is the purpose of a compiler in the context of software development?


Answer: b) Translates code to machine language

The smallest integer that can be represented using 16-bits in signed 2's complement system is:
Answer: b) -32768

2D Array index starts from:


Answer: b) [0][1]

Static memory allocation allocates memory during ______ time.


Answer: a) Compile

What is the default storage class of variables in C language?


Answer: a) auto

What is the primary function of the CPU in a computer system?


Answer: b) Processing
Which number system is commonly used in computer science for representing data and
instructions?
Answer: b) Binary

Group B (Short Answer Questions)

Answer any three of the following:

13. Write a C program to print all the prime numbers > 100 and < 200.

#include <stdio.h>

int isPrime(int num) {

if (num <= 1) return 0;

for (int i = 2; i * i <= num; i++) {

if (num % i == 0) return 0;

return 1;

int main() {

printf("Prime numbers between 100 and 200:\n");

for (int i = 101; i < 200; i++) {

if (isPrime(i)) {

printf("%d ", i);

return 0;

14. Write a non-recursive C program to calculate the factorial of a given number.

#include <stdio.h>

int main() {

int num;

unsigned long long factorial = 1;

printf("Enter a number: ");


scanf("%d", &num);

if (num < 0) {

printf("Factorial of a negative number doesn't exist.\n");

} else {

for (int i = 1; i <= num; i++) {

factorial *= i;

printf("Factorial of %d = %llu\n", num, factorial);

return 0;

15. If the starting address of a 1D character array of size 10 is 2000, what will be the address of
a[7]?

The size of a char is 1 byte. Therefore:

• The address of a[0] = 2000

• Address of a[7] = 2000 + 7×17 \times 17×1 = 2007

16. Write a C program that converts a given long integer (representing time in seconds) into hours,
minutes, and seconds.

#include <stdio.h>

int main() {

long seconds;

int hours, minutes;

printf("Enter time in seconds: ");

scanf("%ld", &seconds);

hours = seconds / 3600;

seconds %= 3600;

minutes = seconds / 60;


seconds %= 60;

printf("Time: %d hours %d minutes %ld seconds\n", hours, minutes, seconds);

return 0;

17a. Draw the flowchart for the area and perimeter of a rectangle.

Steps:

1. Start

2. Input length and breadth

3. Compute area = length × breadth

4. Compute perimeter = 2 × (length + breadth)

5. Output area and perimeter

6. End

7. 17b. What is an assembler?


8. An assembler is a tool that converts assembly language code (low-level
programming) into machine code (binary format) that a computer can execute. It
translates human-readable mnemonics like MOV, ADD, etc., into machine instructions.

Group C (Long Answer Questions)

Answer any three of the following:

18a. Find the value of X in each of the following:

(i) (111111110001011111)2=(X)8(111111110001011111)_2 = (X)_8(111111110001011111)2=(X)8


Convert binary to octal: Group binary digits in sets of 3 (from right to left).

(111 111 110 001 011 111)2=(776 057)8(111\,111\,110\,001\,011\,111)_2 = (776\,057)_8


(111111110001011111)2=(776057)8

X = 776057

(ii) (123)10=(173)X(123)_{10} = (173)_X(123)10=(173)X


Find XXX by converting 123123123 from base 10 to base XXX:

123=1⋅X2+7⋅X1+3⋅X0 ⟹ X=8123 = 1 \cdot X^2 + 7 \cdot X^1 + 3 \cdot X^0 \implies X = 8


123=1⋅X2+7⋅X1+3⋅X0⟹X=8

X=8

(iii) (A09)16=(X)8(A09)_{16} = (X)_8(A09)16=(X)8


Convert hexadecimal to decimal, then to octal:
(A09)16=2560+0+9=256910256910=(5021)8(A09)_{16} = 2560 + 0 + 9 = 2569_{10} 2569_{10} =
(5021)_8 (A09)16=2560+0+9=256910256910=(5021)8

X = 5021

(iv) (135)8=(X)4(135)_8 = (X)_4(135)8=(X)4


Convert octal to decimal, then to base 4:

(135)8=1⋅64+3⋅8+5=93109310=(1121)4(135)_8 = 1 \cdot 64 + 3 \cdot 8 + 5 = 93_{10} 93_{10} =


(1121)_4 (135)8=1⋅64+3⋅8+5=93109310=(1121)4

X = 1121

18b. Analyze the difference between 2's complement and 1's complement with examples.

1. 1’s Complement:

o Represents negative numbers by inverting all the bits of the positive number.

o Example: For +5=00000101+5 = 00000101+5=00000101:


−5-5−5 in 1’s complement = 111110101111101011111010.

2. 2’s Complement:

o Obtained by adding 1 to the 1’s complement of the number.

o Example: For +5=00000101+5 = 00000101+5=00000101:


−5-5−5 in 2’s complement = 111110111111101111111011.

Key Difference:

• 2’s complement eliminates the need for separate “+0” and “-0” representations.

• Arithmetic operations are easier in 2’s complement.

18c. Write a C program to check whether a number is a palindrome or not.

#include <stdio.h>

int main() {

int num, reversed = 0, original, remainder;

printf("Enter a number: ");

scanf("%d", &num);

original = num;

while (num != 0) {
remainder = num % 10;

reversed = reversed * 10 + remainder;

num /= 10;

if (original == reversed) {

printf("The number is a palindrome.\n");

} else {

printf("The number is not a palindrome.\n");

return 0;

19. Write a C program that takes a data type in the command line and computes its size.

This requires a C program with sizeof() to determine the size of types. Let me know if you'd like this
example elaborated.

20a. Write a C program to find the largest and smallest number among a list of numbers taken as
input.

#include <stdio.h>

int main() {

int n, num, max, min;

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

scanf("%d", &n);

printf("Enter number 1: ");

scanf("%d", &num);

max = min = num;

for (int i = 2; i <= n; i++) {


printf("Enter number %d: ", i);

scanf("%d", &num);

if (num > max) max = num;

if (num < min) min = num;

printf("Largest = %d\nSmallest = %d\n", max, min);

return 0;

20b. Write a program in C to calculate the Fibonacci series up to a user-given number using
recursion.

#include <stdio.h>

int fibonacci(int n) {

if (n <= 1) return n;

return fibonacci(n - 1) + fibonacci(n - 2);

int main() {

int n;

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

scanf("%d", &n);

printf("Fibonacci series: ");

for (int i = 0; i < n; i++) {

printf("%d ", fibonacci(i));

return 0;

20c. What is an operator? What are the different types of operators used in C?

An operator is a symbol used to perform operations on variables or values.


Types of Operators:

1. Arithmetic Operators: +, -, *, /, %

2. Relational Operators: ==, !=, >, <, >=, <=

3. Logical Operators: &&, ||, !

4. Bitwise Operators: &, |, ^, ~, <<, >>

5. Assignment Operators: =, +=, -=, etc.

6. Miscellaneous Operators: sizeof, ? :, &, *

You might also like