Question 1:
a. Write an algorithm to convert a decimal number to its equivalent hexadecimal number. Also, draw its corresponding flowchart.
Answer:
Step 1: Start
Step 2: Read the decimal number from the user, say ‘n’
Step 3: Declare the hexadecimal Number say 's' in String Data Type
Step 4: Initialize s[];
Step 5: Repeat while n != 0:
Step 5.1: Extract the remainder by: remainder = n % 16
Step 5.2: Check the remender if remender is 10 assaing A if Remender is 11 assign B if remender is 12 assign C and so on.......
Step 5.2.1: Assign : s[]="A";
Step 5.2.2: Assign : Otherwise s=d+s;
Step 5.3: d = d/16
Step 6: Display the Hexadecimal number
Step 7: Stop
Program
b. Write a program to generate Fibonacci series using recursion.
Answer:
#include<stdio.h>
void Fibonacci(int n)
{
static int a=0,b=1,c;
if(n>0)
{
c= a + b;
a=b;
b=c;
printf("%d ",c);
Fibonacci(n-1);
}
}
int main()
{
int n;
printf("Please enter your preferred number of elements here: ");
scanf("%d",&n);
printf("The Fibonacci Series will be: ");
printf("%d %d ",0,1);
Fibonacci(n-2); //We have used n-2 because we have 2 numbers already printed here
return 0;
}
Output:
Please enter your preferred number of elements here: 10
The Fibonacci Series will be: 0 1 1 2 3 5 8 13 21 34
c. What are the rules of using Big-O notation ? How the performance of the algorithms are measured ?
Answer: Big-O notation is used to describe the upper bound of the time complexity of an algorithm. It gives an estimate of how the number of operations an algorithm performs increases as the input size increases.
The basic rules for using Big-O notation are:
- Big-O notation is used to describe the upper bound of an algorithm’s running time. This means that the algorithm will not take longer than the time described by the Big-O notation.
- The performance of an algorithm is measured by counting the number of basic operations (such as additions, multiplications, etc.) that are executed as the algorithm runs. The goal is to minimize the number of basic operations needed to solve a problem.
- To use Big-O notation, you need to identify the most costly operation in the algorithm (i.e. the operation that is executed the most number of times) and use that as the basis for the notation. For example, if the most costly operation is a for-loop that iterates n times, the notation would be O(n).
- A common example of an algorithm with good performance is a linear search. A linear search checks each element in an array, one by one, until it finds the desired element. The running time of a linear search is O(n) because the number of operations is directly proportional to the number of elements in the array.
- An example of an algorithm with poor performance is a bubble sort. A bubble sort repeatedly iterates through an array and compares adjacent elements, swapping them if they are in the wrong order. The running time of a bubble sort is O(n2) because the number of operations is proportional to the square of the number of elements in the array.
It’s worth noting that Big-O notation only gives an upper bound on the time complexity of an algorithm, and does not take into account factors such as the specific implementation or hardware used.
d. Write a program to search an element in a given list of 20 elements using linear search.
Answer:
#include <stdio.h>
int main()
{
int arr[20], i, n, se;
printf("Enter The No Of Term ");
scanf("%d", &n);
printf("Enter %d elements of the list: ",n);
for(i=0; i<n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to be searched: ");
scanf("%d", &se);
for(i=0; i<n; i++)
{
if(arr[i] == se)
{
printf("Element %d found at position %d\n", se, i+1);
return 0;
}
}
printf("Element not found\n");
return 0;
}
Output:
Enter The No Of Term 5
Enter 5 elements of the list: 25
35
61
10
95
Enter the element to be searched: 10
Element 10 found at position 4
Question 2:
a. Write a program to take a list of N numbers, separate even and odd numbers and put them in two separate files (even_file and odd_file). Use file handling concept.
Answer:
#include <stdio.h>
int main() {
int N, i, num;
printf("Enter the number of elements in the list: ");
scanf("%d", &N);
FILE *even_file = fopen("even_file.txt", "w");
FILE *odd_file = fopen("odd_file.txt", "w");
for(i=0; i<N; i++) {
printf("Enter element %d: ", i+1);
scanf("%d", &num);
if(num % 2 == 0) {
fprintf(even_file, "%d\n", num);
} else {
fprintf(odd_file, "%d\n", num);
}
}
fclose(even_file);
fclose(odd_file);
return 0;
}
Output:
// even_file.txt Contain
62
62
20
120
320
62
// odd_file.txt Contain
25
35
85
95
45
45
b. Write a program to perform the following operation on matrices :
D = A + (B * C) where A, B and C are matrices of 3 × 3 size and D is the resultant matrix.
Answer:
#include <stdio.h>
int main() {
int A[3][3], B[3][3], C[3][3], D[3][3], i, j, k;
printf("Enter elements of matrix A (3x3):\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d", &A[i][j]);
}
}
printf("Enter elements of matrix B (3x3):\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d", &B[i][j]);
}
}
printf("Enter elements of matrix C (3x3):\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d", &C[i][j]);
}
}
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
D[i][j] = A[i][j];
for(k=0; k<3; k++)
{
D[i][j] = D[i][j]+B[i][k] * C[k][j];
}
}
}
printf("Resultant matrix D (3x3):\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%d ", D[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Enter elements of matrix A (3x3):
4
5
3
3
6
7
2
3
0
Enter elements of matrix B (3x3):
1
2
5
8
9
6
4
30
75
Enter elements of matrix C (3x3):
48
75
96
32
15
45
62
0
85
Resultant matrix D (3x3):
426 110 614
1047 741 1690
5804 753 8109
Question 3:
a. Write a program to convert lower case letters to upper case in a given string.
Answer:
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[100];
int i;
printf("Enter a string: ");
scanf("%[^\n]s", str);
for(i=0; str[i]!='\0'; i++)
{
if(islower(str[i]))
{
str[i] = toupper(str[i]);
}
}
printf("Converted string: %s\n", str);
return 0;
}
Output:
Enter a string: Beeown.com
Converted string: BEEOWN.COM
b. Explain switch statement with the help of a program segment. Also write its syntax.
Answer: A switch statement in C is a control flow statement that allows a program to have multiple branches based on the value of an expression. It is often used as an alternative to a series of if-else statements.
Here is an example of a program segment that uses a switch statement:
#include <stdio.h>
#include <ctype.h>
int main()
{
char grade;
printf("Enter your grade: ");
scanf(" %c", &grade);
switch(grade)
{
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Good job!\n");
break;
case 'C':
printf("Average performance.\n");
break;
case 'D':
case 'F':
printf("You need to improve.\n");
break;
default:
printf("Invalid grade.\n");
}
return 0;
}
In this example, the program takes an input grade from the user and assigns it to the variable grade
. The switch statement is then used to branch the program’s execution based on the value of grade
. Each case in the switch statement represents a possible value of the expression passed to the switch statement, in this case grade
. If the value of grade
is ‘A’, the program will execute the first case and print “Excellent!”. If the value of grade
is ‘B’, the program will execute the second case and print “Good job!”. If the value of grade
is ‘C’, the program will execute the third case and print “Average performance.”. If the value of grade
is ‘D’ or ‘F’, the program will execute the fourth case and print “You need to improve.”. If the value of grade
is none of the specified cases, the program will execute the default case and print “Invalid grade.”.
The break
statement is used to exit the switch statement once a match is found and the corresponding code is executed. If it’s not used, the execution will continue to the next case, which might result in unexpected behavior.
The syntax of switch statement is:
switch(expression) {
case value1:
// code to be executed
break;
case value2:
// code to be executed
break;
// ...
default:
// code to be executed if no cases match
}
- The switch to a keyword is followed by an expression in parentheses. This expression is evaluated, and its value is used to determine which case to execute.
- The case a keyword is followed by a value and a colon. This value represents a possible value of the expression passed to the switch statement.
- The code to be executed if the case is matched is written after the colon.
- The break a statement is used to exit the switch statement once a match is found and the corresponding code is executed
- The default case is optional, it’s executed when the expression value doesn’t match any of the cases.
c. Write a program to award grades to students depending upon the criteria
mentioned below :
Marks less than 40, ‘E’ grade.
Marks above 40 but less than 50, ‘D’ grade.
Marks above 50 but less than 60, ‘C’ grade.
Marks above 60 but less than 75, ‘B’ grade.
Marks greater than 75, ‘A’ grade.
Answer:
#include <stdio.h>
int main()
{
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
if (marks < 40)
{
printf("Grade: E\n");
}
else if (marks < 50)
{
printf("Grade: D\n");
}
else if (marks < 60)
{
printf("Grade: C\n");
}
else if (marks < 75)
{
printf("Grade: B\n");
}
else
{
printf("Grade: A\n");
}
return 0;
}
Output:
Enter marks: 85
Grade: A
Question 4:
a. Write a program to demonstrate passing a structure to a function.
Answer:
#include <stdio.h>
struct Student
{
int id;
char name[20];
float marks;
};
void displayStudent(struct Student s)
{
printf("ID: %d\n", s.id);
printf("Name: %s\n", s.name);
printf("Marks: %f\n", s.marks);
}
int main()
{
struct Student s1;
printf("Enter student information:\n");
printf("ID: ");
scanf("%d", &s1.id);
printf("Name: ");
scanf("%s", s1.name);
printf("Marks: ");
scanf("%f", &s1.marks);
displayStudent(s1);
return 0;
}
Output:
Enter student information:
ID: 4536
Name: Manoj
Marks: 459
ID: 4536
Name: Manoj
Marks: 459.000000
b. Write a program to evaluate the following :
c=ab
Answer:
#include <stdio.h>
#include <math.h>
int main() {
int a, b, c;
printf("Enter the value of a: ");
scanf("%d", &a);
printf("Enter the value of b: ");
scanf("%d", &b);
c = pow(a,b);
printf("c = %d\n", c);
return 0;
}
Enter the value of a: 2
Enter the value of b: 3
c = 8
c. What are global variables and static variables ? Explain with the help of an example.
Answer: In C programming, a global variable is a variable that is declared outside of any function and is accessible to all functions and files in the program. Global variables are created in the data segment of memory and retain their value throughout the lifetime of the program.
A static variable, on the other hand, is a variable that is declared with the static keyword. A static variable retains its value between function calls. A static variable inside a function has a single copy for the entire program, whereas a non-static variable inside a function has a different copy for each function call.
Here is an example to illustrate the difference between global and static variables:
#include <stdio.h>
//global variable
int global_var = 0;
void func1() {
//static variable
static int static_var = 0;
//local variable
int local_var = 0;
global_var++;
static_var++;
local_var++;
printf("Global variable: %d\n", global_var);
printf("Static variable: %d\n", static_var);
printf("Local variable: %d\n", local_var);
}
int main() {
func1(); // prints 1 1 1
func1(); // prints 2 2 1
func1(); // prints 3 3 1
return 0;
}
In this example, the global variable global_var
is defined outside of any function, so it’s accessible to all functions in the program. The static variable static_var
is defined inside the func1()
function. The variable local_var
is defined inside the func1()
function and is not static.
When func1()
is called, the value of the global variable global_var
is incremented by 1 each time, and the value of the static variable static_var
is also incremented by 1 each time, but the value of the local variable local_var
is set to 1 each time. As the static variable is retained between the function calls and the local variable is not.
d. How ‘# define’ is used to create functional macros ? Illustrate with the help of a C program segment.
Answer: In C programming, the #define
preprocessor directive is used to create functional macros. A macro is a fragment of code that has been given a name and can be reused multiple times. Functional macros are macros that take one or more arguments and return a value, similar to a function.
Here is an example of a functional macro in C:
#define SQUARE(x) (x * x)
int main()
{
int num = 5;
int result = SQUARE(num);
printf("The square of %d is %d\n", num, result);
return 0;
}
In this example, the macro SQUARE
takes one argument x
and returns the square of x
. In the main function, the macro is called with the variable num
as an argument, and the result is assigned to a variable result
.
When the program runs, it prints:
The square of 5 is 25
Question 5:
a. Write a program to concatenate two strings without using the strcat( ) function.
Answer:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100], str2[100], str3[200];
int i, j;
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
for (i = 0; str1[i] != '\0'; i++)
{
str3[i] = str1[i];
}
for (j = 0; str2[j] != '\0'; j++)
{
str3[i + j] = str2[j];
}
str3[i + j] = '\0';
printf("Concatenated string: %s\n", str3);
return 0;
}
Output:
Enter the first string: Manoj
Enter the second string: Mahato
Concatenated string: ManojMahato
b. Differentiate between sequential and random access files.
Answer: Sequential access files are files that can only be read or written in a linear, sequential order. When you read a sequential file, you must start reading from the beginning of the file and read each byte in order. When you write to a sequential file, you must start writing at the end of the file and add new data to the file in the order in which it is written. Examples of sequential files include text files, log files, and audio files.
Random access files, on the other hand, allow you to read or write data at any location in the file without the need to read or write the data sequentially. This type of file allows you to jump to any location within the file and read or write data. This makes it possible to update a specific record in a file or to access a specific piece of data in a file without having to read through the entire file. Examples of random access files include binary files and indexed files.
In C, the fseek()
function is typically used to move the file pointer to a specific location in a random access file, while the fread()
and fwrite()
functions are used to read and write data from and to the file.
It’s worth mentioning that, in order to access a random access file, you’ll need to open it in a mode that allows for both reading and writing, for example “rb+” for binary files.
c. Write short notes on the following :
i. Structure
ii. Union
Answer i: A structure is a user-defined data type that groups together a collection of variables of different types. Structures are used to store information that belongs together, such as the data of a person (name, age, address, etc.).
To define a structure, the keyword “struct” is used, followed by the name of the structure and a list of variables enclosed in curly braces {}. For example:
struct person {
char name[50];
int age;
char address[100];
};
Answer ii: A union is a user-defined data type that allows a single memory location to store different data types. Unlike structures, which group variables of different types together, a union shares the same memory location among its variables.
To define a union, the keyword “union” is used, followed by the name of the union and a list of variables enclosed in curly braces {}. For example:
union data {
int i;
float f;
char str[20];
};