Professional Documents
Culture Documents
TOP 15Q_POP
TOP 15Q_POP
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
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;
}
A+B=
+ Addition between 2 operands.
10
A*B=
* Multiplication between 2 operands.
25
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);
}
#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;
}
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;
}
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;
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;
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;
}
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};
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}
};
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));
}
return 0;
}
#include <stdio.h>
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>
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;
● }
Key Points:
Example in C:
#include <stdio.h>
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:
Example in C:
#include <stdio.h>
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;
}
int main() {
// Input: Get the number from the user
int number;
printf("Enter a number: ");
scanf("%d", &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)
Example:
#include <stdio.h>
#include <string.h>
int main() {
const char *str = "Hello, World!";
size_t length = strlen(str);
return 0;
}
Syntax:
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;
}
Syntax:
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello, ";
const char *str2 = "World!";
strcat(str1, str2);
return 0;
}
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
strlwr(str);
return 0;
}
Syntax:
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
strupr(str);
printf("Uppercased string: %s\n", str);
return 0;
}
int main() {
int n;
printf("Name: ");
scanf("%s", students[i].name);
printf("Marks: ");
scanf("%d", &students[i].marks);