URR-24 Write Your Roll Number
Dear Student! You should not write anything on this question paper except your roll number. If violated, you will be booked under
malpractice rules which may result in cancellation of all exams which you registered.
KAKATIYA INSTITUTE OF TECHNOLOGY & SCIENCE, WARANGAL
(An Autonomous Institute under Kakatiya University, Warangal)
FACULTY OF ENGINEERING AND TECHNOLOGY
B.TECH. MINOR EXAMINATION-II
SEMESTER I
BRANCH CSE
COURSE CODE U24CS104
COURSE NAME PROGRAMMING FOR PROBLEM SOLVING WITH C
Time: 45 Minutes]
[Max. Marks : 25
Date: 04.12.2024
Instructions to the Students:
1. Read the questions and answer them as per the requirement.
2. Time management is most important. Carry your watch to the exam. (Digital watches are not al-
lowed in the exam hall). For example: 15 minutes for each 10 marks question and 1- 2 minutes for each 1
mark/2-mark question.
3. Carry your own calculator for the examination. Do not borrow from others in the examination hall.
If you borrow calculator, it will be treated as exam malpractice.
4. Do not indulge in any kind of malpractice. Your exams will be cancelled, and you will be
awarded zero in all examinations.
Short answer questions (covering entire syllabus of Unit-III)
Q. No. Marks CDLL COs
1. (a) Define scope, lifetime and visibility of a variable [1] R CO3
Ans: Scope: The part of a program where a variable is accessible. For example, variables defined inside a
function have a local scope.
Lifetime: The duration of time when a variable exists in a program's memory
Visibility: Also known as scope, this is the program region where a variable is accessible
(b) Write the differences between predefined and user-defined functions in C [1] R CO3
Ans: Predefined functions ( like printf() and scanf() ) are defined in standard library, while user-defined
functions are created by the programmer.
(c) Define a string. Write the syntax to declare a string and give an example. [1] R CO3
Ans: A String in C programming is a sequence of characters terminated with a null character ‘\0’. The C
String is stored as an array of characters.
Syntax to declare a string :
char string_name[size];
Example :
char str[] = "c programming";
Course Coordinator Signature with Date: Page 1 of 6
(d) Analyze the following program and write the output [2] U CO3
1. #include <stdio.h>
2. #include <string.h>
3. int main() {
4. char str1[50] = "KITS";
5. char str2[50] = "KITSWgl";
6. strcpy(str1, str2);
7. printf("Output1: %s\n", str1);
8. strcat(str1, str2);
9. printf("Output2: %d\n", strlen(str1));
10. return 0;
11. }
Ans: Output1: KITSWgl
Output2: 14
Explanation:
At Line 6, str1 will become KITSWgl by applying strcpy( ), the same is printed in line 7.
At line 8, str1 will become KITSWglKITSWgl by applying strcat( ). In line 9, it prints the length of the
str1 which is 14
Long answer questions (covering entire syllabus of Unit-III)
2 (a) (i) Analyze the following program and write the output [5] [10] Ap CO3
1. #include <stdio.h>
2. int main()
3. {
4. char string[] = "c programming for problem solving";
5. int i;
6. for (i = 0; string[i] != '\0'; i++)
7. {
8. if (i % 3 != 0) {
9. continue;
10. }
11. printf("%c", string[i]);
12. }
13. return 0;
14. }
(ii) Develop C program that reads a string from the user and find the length of
that string without using strlen( ) function [5]
Ans: (i)
Output : crrmgopbmoi
Explanation: As the continue statement is used at line 9 on condition (i % 3 != 0), it takes the control
to the beginning of the loops and skips printing the character & all other characters will be printed
Course Coordinator Signature with Date: Page 2 of 6
(ii)
#include <stdio.h>
int main() {
char str[100];
int length = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
while (str[length] != '\0') {
length++;
}
length--;
printf("The length of the string is: %d\n", length);
return 0;
}
(b) (i) Develop a C program using recursive function to print the factorial of a given [10] Ap CO3
number [5]
(ii) Develop a C program that defines a function to find the count of even
numbers and count of odd numbers of an array called from the main() function.
The function prototype should be as gven below. [5]
void evenOdd(int a[ ], int n);
Ans: (i)
#include <stdio.h>
int factorial(unsigned int n) {
if (n == 1) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
(ii)
#include <stdio.h>
void evenOdd(int a[], int n) {
int even_count = 0, odd_count = 0;
for (int i = 0; i < n; i++) {
if (a[i] % 2 == 0) {
even_count++;
} else {
odd_count++;
}
}
printf("Count of even numbers: %d\n", even_count);
printf("Count of odd numbers: %d\n", odd_count);
}
Course Coordinator Signature with Date: Page 3 of 6
int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
evenOdd(arr, n);
return 0;
}
OR
(c) (i) Develop a C program to count number of vowels and consonants in a given [10] Ap CO3
string [5]
(ii) Develop a C program to print the characters of a given string in reverse order
[5]
Ans: (i)
#include <stdio.h>
int main() {
char str[100];
int vowels = 0, consonants = 0;
char ch;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
for (int i = 0; str[i] != '\0'; i++) {
ch = str[i];
// Check for vowels (both uppercase and lowercase)
if ((ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') ||
(ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')) {
vowels++;
}
// Check for consonants (both uppercase and lowercase)
else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
consonants++;
}
}
printf("Number of vowels: %d\n", vowels);
printf("Number of consonants: %d\n", consonants);
return 0;
}
(ii)
#include <stdio.h>
Course Coordinator Signature with Date: Page 4 of 6
int main() {
char str[100];
int length = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
while (str[length] != '\0') {
length++;
}
// Exclude the newline character if present
if (str[length - 1] == '\n') {
length--;
}
// Print the string in reverse order
printf("Reversed string: ");
for (int i = length - 1; i >= 0; i--) {
putchar(str[i]);
}
printf("\n");
return 0;
}
(d) (i) Develop a function in C that takes two integers as arguments and returns the [10] Ap CO3
greatest common divisor (GCD) of the two numbers. [5]
(ii). Analyze the following program and write the output [5]
#include<stdio.h>
int c=12, d=5;
void fun1(int a, int b)
{
int c;
c=a; a=b; b=c;
++d;
printf("%d\n", c); // a value is copied to c and printed here
}
int main()
{
int a=4, b=5;
fun1(a , b);
printf("%d\n", c); // global c value is printed
printf("%d\n", d); //incremented global d value is printed
printf(“%d”,d-a-c); // 6-4-12 which is -10
return 0;
}
Ans: (i)
#include <stdio.h>
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
Course Coordinator Signature with Date: Page 5 of 6
}
return a;
}
int main() {
int num1, num2, result;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
result = gcd(num1, num2);
printf("GCD of %d and %d is: %d\n", num1, num2, result);
return 0;
}
(ii)
Output :
4
12
6
-10
* Question Paper Ends *
Paper Set by:
S.No. Faculty Name with Designation Dept. Signature with date
1. Dr. P. Niranjan, Professor CSE
2. Dr. P. Vijay Kumar, Associate Professor CSE
3. Sri. D. Naveen Kumar, Assistant Professor CSE
4. Smt. R. Radhika, Assistant Professor CSE
5. Sri. A. Venkatesh, Assistant Professor CSE
Course Coordinator Signature with Date: Page 6 of 6