Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

DEPARTMENT OF COMPUTER SCIENCE

ICTE New Delhi | Affiliated to VTU, Belagavi |Accredited by NBA


Virgo Nagar, Bengaluru-560049
Pass package Questions (MAKEUP-EXAM)

Sem/Sec: II Subject:POP USING C (G and H section )


Sl.no Questions CO1

1 Explain basic structure of c program with example CO1

The basic structure of a C program is divided into 6 parts which makes it easy
to read, modify, document, and understand in a particular format. C program
must follow the below-mentioned outline in order to successfully compile and
execute. Debugging is easier in a well-structured C program.
Sections of the C Program
There are 6 basic sections responsible for the proper execution of a program.
Sections are mentioned below:
1. Documentation
2. Preprocessor Section
3. Definition
4. Global Declaration
5. Main() Function
6. Sub Programs

1. Documentation

This section consists of the description of the program, the name of the
program, and the creation date and time of the program. It is specified at the
start of the program in the form of comments. Documentation can be
represented as:
// description, name of the program, programmer name, date, time etc.
or
/*
description, name of the program, programmer name, date, time etc.
*/
Anything written as comments will be treated as documentation of the program
and this will not interfere with the given code. Basically, it gives an overview to
the reader of the program.

2. Preprocessor Section

All the header files of the program will be declared in the preprocessor section
of the program. Header files help us to access other’s improved code into our
code. A copy of these multiple files is inserted into our program before the
process of compilation.
Example:
#include<stdio.h>
#include<math.h>

3. Definition

Preprocessors are the programs that process our source code before the process
of compilation. There are multiple steps which are involved in the writing and
execution of the program. Preprocessor directives start with the ‘#’ symbol.
The #define preprocessor is used to create a constant throughout the program.
Whenever this name is encountered by the compiler, it is replaced by the actual
piece of defined code.
Example:
#define long long ll

4. Global Declaration

The global declaration section contains global variables, function declaration,


and static variables. Variables and functions which are declared in this scope
can be used anywhere in the program.
Example:
int num = 18;

5. Main() Function
Every C program must have a main function. The main() function of the
program is written in this section. Operations like declaration and execution
are performed inside the curly braces of the main program. The return type of
the main() function can be int as well as void too. void() main tells the compiler
that the program will not return any value. The int main() tells the compiler
that the program will return an integer value.
Example:
void main()
or
int main()

6. Sub Programs

User-defined functions are called in this section of the program. The control of
the program is shifted to the called function whenever they are called from the
main or outside the main() function. These are specified as per the
requirements of the programmer.
Example:
int sum(int x, int y)
{
return x+y;
}

2 Explain operator with Example . CO1


1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Other Operators
Operators in C
The above operators have been discussed in detail:
Arithmetic Operators in C

The purpose of this operator is to perform mathematical operations like


addition, subtraction, division, multiplication etc.

Operator What it does Example

A+B=
+ Addition between 2 operands.
10

− Subtraction between 2 operands. A−B=0

A*B=
* Multiplication between 2 operands.
25

/ Division between 2 operands. B/A=1

Modulus Operator and remainder of after an integer B%A=


%
division. 0
++ Increases the integer value by one. A++ = 6

— Decreases the integer value by one. B– = 4

Logical Operators in C

In the C programming language, Logical operators are mostly used for decision
making. A logical operator returns either 0 or 1 whether the condition is true
or false.

Relational operators IN c

The relational operators are used to compare two of the available values to
understand what relationship the pairs of values share. For example, equal to,
greater than, less than, etc. Here is a list of all the relational operators used in
the C language: Equal to.
3 Write a C program to compute simple Interest with flowchart CO1

#include<stdio.h>
void main()
{
int p,r,t;
float i;
printf("Enter the Principal, Rate and Time\n");
scanf("%d %d %d",&p,&r,&t); /*Formula for calculating simple interest*/
i=p*r*t/100;
printf("simple interest is : %f",i);
}

Write a C program to calculate area of a circle with Flowchart

#include <stdio.h>
int main(){
int r = 8;
float area = (3.14)*r*r;
printf("The area of the circle is %f",area);
return 0;
}

4 Explain if, if-else, nested if-else and cascaded if-else and switch case with CO2
examples and syntax.
if Statement:
The if statement allows you to execute a block of code only if a certain
condition is true.

Syntax:

Copy code
if (condition) {
// Code to be executed if the condition is true
}

Example:

#include <stdio.h>

int main() {
int x = 5;

if (x > 0) {
printf("x is positive\n");
}

return 0;
}

2. if-else Statement:
The if-else statement allows you to execute one block of code if a condition is
true and another block if the condition is false.

Syntax:

if (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}

Example:

#include <stdio.h>

int main() {
int x = -2;

if (x > 0) {
printf("x is positive\n");
} else {
printf("x is non-positive\n");
}

return 0;
}

3. Nested if-else Statement:


You can use nested if-else statements when you have multiple conditions to
check.

Syntax:

if (condition1) {
// Code to be executed if condition1 is true

if (condition2) {
// Code to be executed if both condition1 and condition2 are true
} else {
// Code to be executed if condition1 is true and condition2 is false
}
} else {
// Code to be executed if condition1 is false
}

Example:

#include <stdio.h>

int main() {
int x = 10, y = 5;

if (x > 0) {
printf("x is positive\n");

if (y > 0) {
printf("y is also positive\n");
} else {
printf("y is non-positive\n");
}
} else {
printf("x is non-positive\n");
}

return 0;
}

4. Cascaded if-else Statement:


Cascaded if-else statements involve multiple if-else blocks in sequence.

Syntax:

if (condition1) {
// Code to be executed if condition1 is true
} else if (condition2) {
// Code to be executed if condition1 is false and condition2 is true
} else {
// Code to be executed if both condition1 and condition2 are false
}

Example:

#include <stdio.h>

int main() {
int x = 0;

if (x > 0) {
printf("x is positive\n");
} else if (x < 0) {
printf("x is negative\n");
} else {
printf("x is zero\n");
}

return 0;
}

5. switch-case Statement:
The switch-case statement is used when you have a variable to test against
multiple values.

Syntax:

switch (expression) {
case value1:
// Code to be executed if expression equals value1
break;
case value2:
// Code to be executed if expression equals value2
break;
// additional cases...
default:
// Code to be executed if expression doesn't match any case
}

Example:

c
Copy code
#include <stdio.h>

int main() {
char grade = 'B';

switch (grade) {
case 'A':
printf("Excellent\n");
break;
case 'B':
printf("Good\n");
break;
case 'C':
printf("Average\n");
break;
default:
printf("Need improvement\n");
}

return 0;
}
5 Demonstrate the working of break and continue statement with a CO2
suitable example

Example of break:
The break statement is used to exit from a loop prematurely. It is commonly
used in switch-case statements and loops (such as for, while, and do-while).
Here's an example using a for loop:

c
Copy code
#include <stdio.h>

int main() {
int i;

for (i = 1; i <= 10; i++) {


if (i == 5) {
printf("Breaking out of the loop at i = %d\n", i);
break; // exit the loop when i is 5
}
printf("%d ", i);
}

return 0;
}

In this example, the loop will print numbers from 1 to 4 and then break out of
the loop when i becomes 5.

Example of continue:
The continue statement is used to skip the rest of the code inside a loop for the
current iteration and move on to the next iteration. Here's an example using a
for loop:

c
Copy code
#include <stdio.h>
int main() {
int i;

for (i = 1; i <= 5; i++) {


if (i == 3) {
printf("Skipping iteration at i = %d\n", i);
continue; // skip the rest of the loop for i = 3
}
printf("%d ", i);
}

return 0;
}

6 List the differences between while loop and do -while loop along with syntax CO2
and example.

while Loop:
Syntax:

while (condition) {
// Code to be executed as long as the condition is true
}

Differences:

​ The while loop checks the condition before the loop body is executed. If
the condition is initially false, the loop body will not be executed at all.

Example:

#include <stdio.h>

int main() {
int i = 1;

while (i <= 5) {
printf("%d ", i);
i++;
}

return 0;
}

In this example, the while loop prints numbers from 1 to 5.

do-while Loop:
Syntax:

do {
// Code to be executed at least once
} while (condition);

Differences:

​ The do-while loop checks the condition after the loop body is executed.
This guarantees that the loop body is executed at least once, regardless
of the condition.

Example:

#include <stdio.h>

int main() {
int i = 1;

do {
printf("%d ", i);
i++;
} while (i <= 5);

return 0;
}
7 Define an array. Explain with suitable examples how to initialize lD CO3
array 2D array and Multidimensional array.

1. One-Dimensional Array:
A one-dimensional array is a simple list of elements.

Syntax:

datatype arrayName[size];

Example:

#include <stdio.h>

int main() {
// Declaration and initialization of a one-dimensional array
int numbers[5] = {1, 2, 3, 4, 5};

// Accessing and printing array elements


for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}

return 0;
}

2. Two-Dimensional Array:
A two-dimensional array is like a table or a matrix with rows and columns.

Syntax:

datatype arrayName[rowSize][columnSize];
Example:

#include <stdio.h>

int main() {
// Declaration and initialization of a two-dimensional array
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

// Accessing and printing array elements


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}

return 0;
}

3. Multidimensional Array:
A multidimensional array is an array with more than two dimensions. The
syntax extends similarly.

Example:

#include <stdio.h>

int main() {
// Declaration and initialization of a three-dimensional array
int cube[2][3][4] = {
{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}},
{{13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24}}
};
// Accessing and printing array elements
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 4; k++) {
printf("%d ", cube[i][j][k]);
}
printf("\n");
}
printf("\n");
}

return 0;
}

8 . CO3
Write a C program to find factorial of a given number using recursion
function

#in#include<stdio.h>

int fact(int);
int main()
{
int x,n;
printf(" Enter the Number to Find Factorial :");
scanf("%d",&n);

x=fact(n);
printf(" Factorial of %d is %d",n,x);

return 0;
}
int fact(int n)
{
if(n==0)
return(1);
return(n*fact(n-1));
}

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


}

return 0;
}

9 What is a function? Explain different classification of user defined CO3


functions based on parameter passing and return type with examples .

A function is a self-contained block of code that performs a specific task. In


programming, functions are used to break down a program into smaller,
manageable pieces, making the code modular, easier to understand, and more
maintainable. Functions can take input parameters, perform some operations,
and optionally return a value.

Classification of User-Defined Functions:

1. Based on Parameter Passing:


● a. Call by Value:
● The actual value is passed to the function.
● Changes made to the parameters inside the function do not affect
the actual values.
● Example:

#include <stdio.h>

// Function to swap two values using call by value


void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 5, y = 10;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(x, y);
printf("After swap: x = %d, y = %d\n", x, y);

return 0;
}

● b. Call by Reference:
● The memory address (reference) of the actual parameters is
passed.
● Changes made to the parameters inside the function affect the
actual values.
● Example:
● c
● Copy code

#include <stdio.h>

// Function to swap two values using call by reference


void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int x = 5, y = 10;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);

return 0;
● }

10 Explain call by value and call by reference in detail CO3


1. Call by Value:
In "Call by Value," the actual value of the argument is passed to the function.
The function receives a copy of the data, and any modifications made inside the
function do not affect the original data.

Key Points:

● Changes inside the function do not affect the original values.


● Arguments are passed by value.
● Simple and straightforward.
● Safer in terms of unintentional side effects.

Example in C:

#include <stdio.h>

// Function to square a number using call by value


void square(int num) {
num = num * num; // Changes made here do not affect the original value
printf("Inside function: %d\n", num);
}

int main() {
int x = 5;
printf("Before function call: %d\n", x);
square(x);
printf("After function call: %d\n", x); // The original value remains
unchanged

return 0;
}

In this example, the square function receives a copy of the variable x. Changes
made to num inside the function do not affect the original value of x.

2. Call by Reference:
In "Call by Reference," the memory address (reference) of the actual argument
is passed to the function. The function can access and modify the data directly
at that memory location.

Key Points:

● Changes inside the function directly affect the original values.


● Arguments are passed by reference using pointers.
● Allows for more efficient memory usage when dealing with large data
structures.
● May lead to unintended side effects if not used carefully.

Example in C:

#include <stdio.h>

// Function to square a number using call by reference


void square(int *num) {
*num = (*num) * (*num); // Changes made here affect the original value
printf("Inside function: %d\n", *num);
}

int main() {
int x = 5;
printf("Before function call: %d\n", x);
square(&x);
printf("After function call: %d\n", x); // The original value is modified

return 0;
}

11 What is recursion ? Write a c-program using functions to calculate factorial CO3

Recursion is a programming concept where a function calls itself in


order to solve a problem. In a recursive function, the problem is
broken down into smaller sub-problems that are solved by calling the
same function. Each recursive call represents a step closer to the base
case, which is a condition that determines when the recursion should
stop.
#include <stdio.h>
// Function to calculate factorial using recursion
int factorial(int n) {
// Base case: factorial of 0 or 1 is 1
if (n == 0 || n == 1) {
return 1;
} else {
// Recursive case: n! = n * (n-1)!
return n * factorial(n - 1);
}
}

int main() {
// Input: Get the number from the user
int number;
printf("Enter a number: ");
scanf("%d", &number);

// Calculate and display the factorial


if (number < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
printf("Factorial of %d = %d\n", number, factorial(number));
}

return 0;
}

12 Mention various operations that can be performed on strings using built-in CO4
functions. Explain all functions(Strlen,strcmp,strcat,strlwr,strupper)

Strings in C are sequences of characters terminated by the null character ('\0').


C provides several built-in functions in the <string.h> header that allow you to
perform various operations on strings. Here are explanations for some
commonly used string functions:

1. strlen (String Length):


Purpose: Returns the length of the given string.
Syntax:

size_t strlen(const char *str);

Example:

#include <stdio.h>
#include <string.h>

int main() {
const char *str = "Hello, World!";
size_t length = strlen(str);

printf("Length of the string: %zu\n", length);

return 0;
}

2. strcmp (String Compare):


Purpose: Compares two strings lexicographically.

Syntax:

int strcmp(const char *str1, const char *str2);

Example:

#include <stdio.h>
#include <string.h>

int main() {
const char *str1 = "abc";
const char *str2 = "def";
int result = strcmp(str1, str2);

if (result == 0) {
printf("Strings are equal\n");
} else if (result < 0) {
printf("String 1 is less than String 2\n");
} else {
printf("String 1 is greater than String 2\n");
}

return 0;
}

3. strcat (String Concatenate):


Purpose: Concatenates (appends) the second string to the end of the first string.

Syntax:

char *strcat(char *dest, const char *src);

Example:

#include <stdio.h>
#include <string.h>

int main() {
char str1[20] = "Hello, ";
const char *str2 = "World!";

strcat(str1, str2);

printf("Concatenated string: %s\n", str1);

return 0;
}

4. strlwr (String to Lowercase):


Purpose: Converts all uppercase letters in the string to lowercase.
Syntax:

char *strlwr(char *str);

Example:

#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello, World!";

strlwr(str);

printf("Lowercased string: %s\n", str);

return 0;
}

5. strupr (String to Uppercase):


Purpose: Converts all lowercase letters in the string to uppercase.

Syntax:

char *strupr(char *str);

Example:

#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello, World!";

strupr(str);
printf("Uppercased string: %s\n", str);

return 0;
}

13 Write a C program to copy one string to another CO4


#include <stdio.h>
int main() {
char s1[1000];
char s2[1000];
printf("Enter any string: ");
gets(s1);
int i;
for(i=0;s1[i]!='\0';i++) {
s2[i]=s1[i];
}
s2[i]='\0';

printf("original string s1='%s'\n",s1);


printf("copied string s2='%s'",s2);
return 0;
}

14 What is a Structure? Explain with syntax and examples ? difference CO5


between structure and union
Structure

struct [structure name]


{
member definition;
member definition;
...
member definition;
};
Union

union [union name]


{
member definition;
member definition;
...
member definition;
};

15 Write a C program to maintain a record of n student using structures with 4 CO5


field Roll no ,Marks,name and grade.Print the name of the student with
marks >=70.
#include <stdio.h>

// Define a structure named "Student"


struct Student {
int rollNumber;
int marks;
char name[50];
char grade;
};

// Function to print names of students with marks >= 70


void printHighScorers(struct Student students[], int n) {
printf("Students with marks >= 70:\n");

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


if (students[i].marks >= 70) {
printf("%s\n", students[i].name);
}
}
}

int main() {
int n;

// Input: Get the number of students from the user


printf("Enter the number of students: ");
scanf("%d", &n);

// Declare an array of type "struct Student"


struct Student students[n];

// Input: Get information for each student


for (int i = 0; i < n; i++) {
printf("\nEnter details for student %d:\n", i + 1);
printf("Roll Number: ");
scanf("%d", &students[i].rollNumber);

printf("Name: ");
scanf("%s", students[i].name);

printf("Marks: ");
scanf("%d", &students[i].marks);

// Assign grade based on marks


if (students[i].marks >= 90) {
students[i].grade = 'A';
} else if (students[i].marks >= 80) {
students[i].grade = 'B';
} else if (students[i].marks >= 70) {
students[i].grade = 'C';
} else {
students[i].grade = 'F';
}
}

// Print names of students with marks >= 70


printHighScorers(students, n);
return 0;
}

You might also like