B.
Tech 1st Year – Sessional Test 1 Answers
Q1. Apply conditional operator to find largest and smallest
element among any three given numbers.
#include <stdio.h>
int main() {
int a, b, c, largest, smallest;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
largest = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
smallest = (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c);
printf("Largest = %d\n", largest);
printf("Smallest = %d\n", smallest);
return 0;
}
Q2. Develop a C program to calculate the distance travelled by a
particle using s = ut + ½ at².
#include <stdio.h>
int main() {
float u, a, t, s;
printf("Enter initial velocity u (m/s): ");
scanf("%f", &u);
printf("Enter acceleration a (m/s^2): ");
scanf("%f", &a);
printf("Enter time t (seconds): ");
scanf("%f", &t);
s = (u * t) + (0.5 * a * t * t);
printf("Distance travelled = %.2f m\n", s);
return 0;
}
Q3. Compare and contrast entry-controlled loops and
exit-controlled loops with examples.
#include <stdio.h>
int main() {
int i = 1;
printf("Entry-controlled loop (while):\n");
while(i <= 3) {
printf("%d ", i);
i++;
}
printf("\nExit-controlled loop (do-while):\n");
i = 1;
do {
printf("%d ", i);
i++;
} while(i <= 3);
return 0;
}
Q4. Develop a C program to find Min and Max of a given 1-D
integer array.
#include <stdio.h>
int main() {
int n, i, min, max;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for(i = 0; i < n; i++) scanf("%d", &arr[i]);
min = max = arr[0];
for(i = 1; i < n; i++) {
if(arr[i] < min) min = arr[i];
if(arr[i] > max) max = arr[i];
}
printf("Min = %d\nMax = %d\n", min, max);
return 0;
}
Q5. Build a program to find 2’s complement of the given binary
number.
#include <stdio.h>
#include <string.h>
int main() {
char bin[50];
int i;
printf("Enter binary number: ");
scanf("%s", bin);
for(i = 0; bin[i] != '\0'; i++)
bin[i] = (bin[i] == '0') ? '1' : '0';
for(i = strlen(bin) - 1; i >= 0; i--) {
if(bin[i] == '0') {
bin[i] = '1';
break;
} else {
bin[i] = '0';
}
}
printf("2's Complement = %s\n", bin);
return 0;
}
Q6. Develop a C program to find and print the transpose of a
given matrix.
#include <stdio.h>
int main() {
int r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
int a[r][c];
printf("Enter matrix elements:\n");
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
scanf("%d", &a[i][j]);
printf("Transpose:\n");
for(i = 0; i < c; i++) {
for(j = 0; j < r; j++)
printf("%d ", a[j][i]);
printf("\n");
}
return 0;
}
Q7. Develop a C program to sort a list of integers using Selection
Sort.
#include <stdio.h>
int main() {
int n, i, j, minIndex, temp;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for(i = 0; i < n; i++) scanf("%d", &arr[i]);
for(i = 0; i < n-1; i++) {
minIndex = i;
for(j = i+1; j < n; j++)
if(arr[j] < arr[minIndex])
minIndex = j;
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
printf("Sorted array: ");
for(i = 0; i < n; i++) printf("%d ", arr[i]);
return 0;
}
Q8. Build a program for concatenation of two strings (with and
without library functions).
#include <stdio.h>
#include <string.h>
int main() {
char str1[50], str2[50];
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
char libConcat[100];
strcpy(libConcat, str1);
strcat(libConcat, str2);
printf("Concatenated (library) = %s\n", libConcat);
int i, j;
for(i = 0; str1[i] != '\0'; i++);
for(j = 0; str2[j] != '\0'; j++, i++)
str1[i] = str2[j];
str1[i] = '\0';
printf("Concatenated (manual) = %s\n", str1);
return 0;
}
Q9. Build a program to read temperature in Fahrenheit and output
equivalent in Celsius and vice versa.
#include <stdio.h>
int main() {
float temp;
char choice;
printf("Enter temperature: ");
scanf("%f", &temp);
printf("Convert to (C/F): ");
scanf(" %c", &choice);
if(choice == 'C' || choice == 'c')
printf("Celsius = %.2f\n", (temp - 32) * 5/9);
else if(choice == 'F' || choice == 'f')
printf("Fahrenheit = %.2f\n", (temp * 9/5) + 32);
else
printf("Invalid choice!\n");
return 0;
}
Q10. Write a C program to evaluate the expression 3 * 8 / 4 % 4 * 5
from left to right.
#include <stdio.h>
int main() {
int result = 3 * 8 / 4 % 4 * 5;
printf("Result = %d\n", result);
return 0;
}
Q11. Compare and contrast between = and ==, and a+=1 and
a=a*n+1 with examples.
#include <stdio.h>
int main() {
int a = 5, b = 5;
if(a == b)
printf("a and b are equal\n");
a = b;
printf("After a = b, a = %d\n", a);
int n = 3;
a += 1;
printf("a += 1 gives a = %d\n", a);
a = a * n + 1;
printf("a = a*n+1 gives a = %d\n", a);
return 0;
}
Q12. Develop a program to find length of a given string without
using library function.
#include <stdio.h>
int main() {
char str[100];
int i, length = 0;
printf("Enter a string: ");
scanf("%s", str);
for(i = 0; str[i] != '\0'; i++)
length++;
printf("Length = %d\n", length);
return 0;
}
Q13. Build a program to eliminate duplicate elements in an array.
#include <stdio.h>
int main() {
int n, i, j, k;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements: ");
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
for(i = 0; i < n; i++) {
for(j = i + 1; j < n; ) {
if(arr[i] == arr[j]) {
for(k = j; k < n - 1; k++)
arr[k] = arr[k + 1];
n--;
} else {
j++;
}
}
}
printf("Array after removing duplicates: ");
for(i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Q14. Develop a C program to multiply two matrices.
#include <stdio.h>
int main() {
int r1, c1, r2, c2, i, j, k;
printf("Enter rows and cols of first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and cols of second matrix: ");
scanf("%d %d", &r2, &c2);
if(c1 != r2) {
printf("Matrix multiplication not possible.\n");
return 0;
}
int a[r1][c1], b[r2][c2], res[r1][c2];
printf("Enter first matrix:\n");
for(i = 0; i < r1; i++)
for(j = 0; j < c1; j++)
scanf("%d", &a[i][j]);
printf("Enter second matrix:\n");
for(i = 0; i < r2; i++)
for(j = 0; j < c2; j++)
scanf("%d", &b[i][j]);
for(i = 0; i < r1; i++)
for(j = 0; j < c2; j++) {
res[i][j] = 0;
for(k = 0; k < c1; k++)
res[i][j] += a[i][k] * b[k][j];
}
printf("Resultant matrix:\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++)
printf("%d ", res[i][j]);
printf("\n");
}
return 0;
}
Q15. Develop a C program to perform binary search on a sorted
array.
#include <stdio.h>
int main() {
int n, i, search, low, high, mid;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements in ascending order: ");
for(i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter element to search: ");
scanf("%d", &search);
low = 0;
high = n - 1;
while(low <= high) {
mid = (low + high) / 2;
if(arr[mid] == search) {
printf("Element found at position %d\n", mid + 1);
return 0;
} else if(arr[mid] < search)
low = mid + 1;
else
high = mid - 1;
}
printf("Element not found\n");
return 0;
}
Q16. List and describe string handling functions with examples.
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello", str2[50] = "World";
printf("strlen(str1) = %lu\n", strlen(str1));
strcpy(str2, str1);
printf("After strcpy, str2 = %s\n", str2);
strcat(str1, " C");
printf("After strcat, str1 = %s\n", str1);
printf("strcmp("abc", "abd") = %d\n", strcmp("abc", "abd"));
return 0;
}
Q17. Using switch statement write a C program to develop a
simple calculator.
#include <stdio.h>
int main() {
char op;
float num1, num2;
printf("Enter operator (+, -, *, /): ");
scanf(" %c", &op);
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
switch(op) {
case '+': printf("Result = %.2f\n", num1 + num2); break;
case '-': printf("Result = %.2f\n", num1 - num2); break;
case '*': printf("Result = %.2f\n", num1 * num2); break;
case '/':
if(num2 != 0)
printf("Result = %.2f\n", num1 / num2);
else
printf("Division by zero error\n");
break;
default: printf("Invalid operator\n");
}
return 0;
}
Q18. What is infinite for loop? Give example and how it can be
terminated.
#include <stdio.h>
int main() {
int count = 0;
for(;;) { // Infinite loop
printf("Count = %d\n", count);
count++;
if(count == 5) break; // Termination condition
}
return 0;
}
Q19. Compare and contrast selection sort and bubble sort, and
write bubble sort program.
#include <stdio.h>
int main() {
int n, i, j, temp;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements: ");
for(i = 0; i < n; i++) scanf("%d", &arr[i]);
for(i = 0; i < n-1; i++) {
for(j = 0; j < n-i-1; j++) {
if(arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("Sorted array: ");
for(i = 0; i < n; i++) printf("%d ", arr[i]);
return 0;
}
Q20. State limitations of 2D arrays and write a program for
addition of two 3D arrays.
#include <stdio.h>
int main() {
int x[2][2][2] = {{{1,2},{3,4}}, {{5,6},{7,8}}};
int y[2][2][2] = {{{1,1},{1,1}}, {{1,1},{1,1}}};
int z[2][2][2];
int i, j, k;
for(i = 0; i < 2; i++)
for(j = 0; j < 2; j++)
for(k = 0; k < 2; k++)
z[i][j][k] = x[i][j][k] + y[i][j][k];
printf("Sum of two 3D arrays:\n");
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
for(k = 0; k < 2; k++)
printf("%d ", z[i][j][k]);
printf("\n");
}
printf("\n");
}
return 0;
}