0% found this document useful (0 votes)
36 views38 pages

PPS Practical File

pps practical file

Uploaded by

yajatangrish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views38 pages

PPS Practical File

pps practical file

Uploaded by

yajatangrish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CT Group of Institutions, Maqsudan

Experiment Title: Familiarisation with programming


environment

Laboratory: Programming for Problem Solving


(BTPS102-18)

Experiment No: 1 Class: B.TECH( AI&DS) Semester:1st

Practical 1: Familiarisation with programming environment

C programming

C is a procedural programming language. It was initially developed by Dennis Ritchie in the year 1972. It was
mainly developed as a system programming language to write an operating system.

Features of C Programming

1. Procedural Language: In a procedural language like C step by step, predefined instructions are carried
out. A C program may contain more than one function to perform a particular task. There are other
programming paradigms as well in the programming world with object-oriented programming being the most
popular.

2. Fast and Efficient: Newer languages like Java, python offer more features than c programming
language but due to additional processing in these languages, their performance rate gets down effectively. C
is also statically typed and compiled that make C further faster than newer languages.

3. Modularity: Modularity in C programming refers to the practice of breaking down a large program into
smaller, self-contained, and manageable units called modules. Each module is designed to perform a specific
task or provide a particular set of functionalities, promoting organization, reusability, and maintainability of
the codebase.

4. Statically Type: C programming language is a statically typed language. Meaning the type of variable is
checked at the time of compilation but not at run time. This means each time a programmer types a program
they have to mention the type of variables used.

5. General-Purpose Language: From system programming to photo editing software, the C programming
language is used in various applications. Some of the common applications where it’s used are as follows:
 Operating systems: Windows, Linux, iOS, Android, macOS

 Databases: PostgreSQL, Oracle, MySQL, MS SQL Server, etc.

6. Memory Management: C provides manual memory management through functions like malloc(), calloc(),
realloc(), and free(). These let programmers allocate, resize, and release memory as needed, offering powerful
control but also requiring careful handling to avoid memory leaks.

7. Libraries with Rich Functions: Robust libraries and functions in C help even a beginner coder to code
with ease.

8. Middle-Level Language: As it is a middle-level language so it has the combined form of both capabilities
of assembly language and features of the high-level language.

9. Portability: C language is lavishly portable as programs that are written in C language can run and compile
on any system with either no or small changes.

10. Easy to Extend: Programs written in C language can be extended means when a program is already
written in it then some more features and operations can be added to it.

Structure of C Program

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.
There are 6 basic sections responsible for the proper execution of a program. Sections are mentioned below:

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;

Example: Below C program to find the sum of 2 numbers:

// Documentation

/**

* file: sum.c

* author: you

* description: program to find sum.

*/

// Link
#include <stdio.h>

// Definition

#define X 20

// Global Declaration

int sum(int y);

// Main() Function

int main(void)

int y = 55;

printf("Sum: %d", sum(y));

return 0;

// Subprogram

int sum(int y)

return y + X;

Output

Sum: 75

Examples of C Compilers

Several prominent C compilers are available for various operating systems and development environments.
Here are some notable examples:

GNU Compiler Collection (GCC): This is a widely used, open-source compiler suite that supports multiple
programming languages, including C. GCC is the standard compiler on many Unix-like systems, including
Linux.

Clang (LLVM): Clang is a C, C++, and Objective-C front end for the LLVM compiler infrastructure. It is
known for its fast compilation times, excellent error diagnostics, and modular design.
Microsoft Visual C++ (MSVC): This is Microsoft's proprietary C and C++ compiler, primarily used for
developing applications on the Windows platform. It is integrated within the Visual Studio IDE.

Tiny C Compiler (TCC): TCC is a small, fast C compiler designed for environments with limited resources.
It is known for its rapid compilation speed and ability to compile C code into executable programs very
quickly.

Intel C++ Compiler (ICC): Developed by Intel, this compiler is optimized for Intel processors and is often
used for high-performance computing applications to maximize performance on Intel hardware.

Borland C++ / Turbo C: Historically significant compilers, particularly Turbo C, were popular in the late 20th
century for MS-DOS and early Windows development due to their speed and ease of use. While less common
for modern development, they remain notable examples.

MinGW (Minimalist GNU for Windows): This is a free and open-source software development environment
for creating native Microsoft Windows applications using GCC. It provides a set of tools, including the GCC
C compiler, for Windows.

How to run a C program

Running a C program typically involves two main steps: compilation and execution.

1. Write your C code:

Create a new file with a .c extension (e.g., myprogram.c) using a text editor or an Integrated Development
Environment (IDE) like VS Code, Code::Blocks, or Dev-C++.

Write your C program within this file.

2. Compile the C code:

Using a terminal/command prompt:

Navigate to the directory where you saved your C file using the cd command.

Use a C compiler (like GCC) to compile the source code into an executable file. The command is typically:

Code

gcc myprogram.c -o myprogram

(Replace myprogram.c with your file name and myprogram with your desired executable name. If -o is
omitted, the executable will be named a.out on Linux/macOS or a.exe on Windows by default.)

Using an IDE:
IDEs usually have a "Build" or "Compile" option that automates this process. Refer to your IDE's
documentation for specific instructions.

3. Run the executable:

Using a terminal/command prompt:

After successful compilation, an executable file (e.g., myprogram or myprogram.exe) will be created in the
same directory.

To run it:

On Linux/macOS:

Code

./myprogram

On Windows.

Code

myprogram.exe

(Or simply myprogram if the executable is in your system's PATH.)

Using an IDE:

IDEs usually have a "Run" or "Execute" option that directly runs the compiled program.

Example (using GCC in a terminal):

Create hello.c.

#include <stdio.h>

int main() {

printf("Hello, World!\n");

return 0;

compile.

Code

gcc hello.c -o hello


run.

Code

./hello

This will display "Hello, World!" in the terminal.

Write a program to print your name

#include<stdio.h>

int main(){

printf(“My name is Ridhima Thakur”);

return 0;

OUTPUT:
CT Group of Institutions, Maqsudan

Experiment Title: Simple computational problems


using arithmetic expressions.

Laboratory: Programming for Problem Solving


(BTPS102-18)

Experiment No: 2 Class: B.TECH (AI&DS) Semester:1st

Practical 2: Simple computational problems using arithmetic expressions

A) Write a program to to perform various arithmetic operations by taking input from user

#include <stdio.h>

int main() {

float num1, num2;

printf("Enter first number: ");

scanf("%f", &num1);

printf("Enter second number: ");

scanf("%f", &num2);

printf("\n--- Results ---\n");

printf("Addition = %.2f\n", num1 + num2);

printf("Subtraction = %.2f\n", num1 - num2);

printf("Multiplication = %.2f\n", num1 * num2);

printf("Division = %.2f\n", num1 / num2);

return 0;

}
B) Write a program to find area and perimeter of rectangle by taking input from user

#include<stdio.h>

int main(){

float l, b, area, perimeter;

printf("Enter length of rectangle:\n");

scanf("%f",&l);

printf("Enter breadth of rectangle:\n");

scanf("%f",&b);

area= l*b;

perimeter=2*(l+b);

printf("Area of the rectangle is %.2f\n",area);

printf("Perimeter of the rectangle is %.2f", perimeter);

return 0;}
C) Write a program to calculate total marks and percentage of a student from any five subjects
by taking input from user

#include <stdio.h>

int main() {

float sub1, sub2, sub3, sub4, sub5;

float total, percentage;

printf("Enter marks of five subjects:\n");

printf("Subject 1:");

scanf("%f”, &sub1);

printf("Subject 2:");

scanf("%f", &sub2);

printf("Subject 3:");

scanf("%f", &sub3);

printf("Subject 4:");

scanf("%f", &sub4);

printf("Subject 5:");

scanf("%f", &sub5);

total = sub1 + sub2 + sub3 + sub4 + sub5;

percentage = (total / 500) * 100;

printf("\nTotal Marks = %.2f\n", total);

printf("Percentage = %.2f%%\n", percentage);

return 0;

}
D) Write a program to convert temperature from celsius to fahrenheit and fahrenheit to celsius

#include <stdio.h>

int main() {

float temp, convertedTemp;

int choice;

printf("Temperature Conversion Menu:\n");

printf("1. Celsius to Fahrenheit\n");

printf("2. Fahrenheit to Celsius\n");

printf("Enter your choice (1 or 2): ");

scanf("%d", &choice);

if (choice == 1) {

printf("Enter temperature in Celsius: ");

scanf("%f", &temp);

convertedTemp = (temp *(9 / 5)) + 32;

printf("Temperature in Fahrenheit = %.2f°F\n", convertedTemp);

else if (choice == 2) {

printf("Enter temperature in Fahrenheit: ");


scanf("%f", &temp);

convertedTemp = (5/9)*(temp-32);

printf("Temperature in Celsius = %.2f°C\n", convertedTemp);

else {

printf("Invalid choice! Please run the program again.\n");}

return 0;}
CT Group of Institutions, Maqsudan

Experiment Title: Branching and logical expressions

Laboratory: Programming for Problem Solving


(BTPS102-18)

Experiment No: 3 Class: B.TECH (AI&DS) Semester:1st

Practical 3: Branching and logical expressions

A) Write a program to check whether a number is positive or negative

#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num > 0) {

printf("The number is positive.\n");

else if (num < 0) {

printf("The number is negative.\n");

else {

printf("The number is zero.\n");

return 0;

}
B) Write a program to check whether a number is odd or even

#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num % 2 == 0) {

printf("The number is even.\n");

else {

printf("The number is odd.\n");

return 0;

}
C) Write a program to read the age of a candidate and his nationality and determine whether
he/she is eligible to cast vote or not using nested if statement.

#include <stdio.h>

int main() {

int age;

char nationality;

printf("Enter your age: ");

scanf("%d", &age);

printf("Are you a citizen? (y/n): ");

scanf(" %c", &nationality);

if (nationality == 'y' || nationality == 'Y') { // Check nationality

if (age >= 18) { // Check age

printf("You are eligible to cast your vote.\n");

} else {

printf("You are not eligible to vote. Minimum age is 18.\n");

} else {

printf("You are not eligible to vote. Only citizens can vote.\n");

return 0;

}
D) Write a program to find the largest number from given three number by using if else if

statement

#include <stdio.h>

int main() {

float num1, num2, num3;

printf("Enter three numbers: ");

scanf("%f %f %f", &num1, &num2, &num3);

if (num1 > num2 && num1 > num3) {

printf("The largest number is %.2f\n", num1);

else if (num2 > num1 && num2 > num3) {

printf("The largest number is %.2f\n", num2);

else if (num3 > num1 && num3 > num2) {

printf("The largest number is %.2f\n", num3);

else {

printf("All numbers are equal or some are same.\n");

return 0; }
E) Write a program to perform arithmetic operations using switch statement

#include <stdio.h>

int main() {

float num1, num2, result;

char op;

printf("Enter first number: ");

scanf("%f", &num1);

printf("Enter an operator (+, -, *, /, %%): ");

scanf(" %c", &op);

printf("Enter second number: ");

scanf("%f", &num2);

switch (op) {

case '+':

result = num1 + num2;

printf("Result = %.2f\n", result);

break;

case '-':

result = num1 - num2;

printf("Result = %.2f\n", result);

break;

case '*':

result = num1 * num2;

printf("Result = %.2f\n", result);

break;

case '/':

if (num2 == 0)

printf("Error! Division by zero is not allowed.\n");


else {

result = num1 / num2;

printf("Result = %.2f\n", result);

break;

default:

printf("Invalid operator!\n");

return 0;

}
CT Group of Institutions, Maqsudan

Experiment Title: Loops (while and for loops)

Laboratory: Programming for Problem Solving


(BTPS102-18)

Experiment No: 4 Class: B.TECH (AI&DS) Semester:1st

Practical 4: Loops (while and for loops)

A) Write a program to find sum of n natural numbers using for loop

#include <stdio.h>

int main() {

int n, sum = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

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

sum += i; // Add i to sum

printf("Sum of first %d natural numbers = %d\n", n, sum);

return 0;

}
B) Write a program to print multiplication table of any number by using whole and do while
loop

#include <stdio.h>

int main() {

int num, i;

printf("Enter a number to print its multiplication table: ");

scanf("%d", &num);

printf("\nMultiplication table using while loop:\n");

i = 1;

while (i <= 10) {

printf("%d x %d = %d\n", num, i, num * i);

i++;

printf("\nMultiplication table using do-while loop:\n");

i = 1;

do {

printf("%d x %d = %d\n", num, i, num * i);

i++;

} while (i <= 10);

return 0; }

}
C) Write a program to calculate factorial of any given number

#include <stdio.h>

int main() {

int n;

int factorial = 1;

printf("Enter a positive integer: ");

scanf("%d", &n);

if (n > 0) {

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

factorial *= i;

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

} else if (n == 0) {

printf("Factorial of 0 = 1\n");

} else {
printf("Factorial is not defined for negative numbers.\n");

return 0;

D) Write a program to check whether a given number is prime or not

#include <stdio.h>

int main() {

int n, i, count = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

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

count += (n % i == 0); // Increment count if i divides n

// If count is 2 (divisible by 1 and itself), it's prime

printf("%d is %s prime number.\n", n, (count == 2) ? "a" : "not a");

return 0; }
E) Write a program to print Fibonacci series upto n terms.

#include <stdio.h>

int main() {

int n, first = 0, second = 1, next;

printf("Enter the number of terms: ");

scanf("%d", &n);

printf("Fibonacci series up to %d terms:\n", n);

for (int i = 1; i <= n; i++)

printf("%d ", first);

next = first + second;

first = second;

second = next;

printf("\n");

return 0;

}
CT Group of Institutions, Maqsudan

Experiment Title: 1D Arrays

Laboratory: Programming for Problem Solving


(BTPS102-18)

Experiment No: 5 Class: B.TECH (AI&DS) Semester:1st

Practical 5: 1D Arrays

A) Write a program to read and display array elements

#include <stdio.h>

int main() {

int n;

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

int arr[n]; // Declare array of size n

// Read array elements

printf("Enter %d elements:\n", n);

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

scanf("%d", &arr[i]);

// Display array elements

printf("The array elements are:\n");

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

printf("%d ", arr[i]);

}
printf("\n");

return 0;

B) Write a program to find the sum of any n numbers by using array

#include <stdio.h>

int main() {

int n, sum = 0;

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];

// Read array elements

printf("Enter %d numbers:\n", n);

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

scanf("%d", &arr[i]);

// Calculate sum

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

sum += arr[i];
}

printf("Sum of the numbers = %d\n", sum);

return 0;

}
CT Group of Institutions, Maqsudan

Experiment Title: 2D Arrays and Strings

Laboratory: Programming for Problem Solving


(BTPS102-18)

Experiment No: 6 Class: B.TECH (AI&DS) Semester:1st

Practical 6: 2D Arrays and Strings

A) Write a program to read and display 2D array elements

#include <stdio.h>

int main() {

int rows, cols;

printf("Enter the number of rows: ");

scanf("%d", &rows);

printf("Enter the number of columns: ");

scanf("%d", &cols);

int arr[rows][cols];

// Read 2D array elements

printf("Enter elements of %dx%d array:\n", rows, cols);

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

for (int j = 0; j < cols; j++) {

scanf("%d", &arr[i][j]);

// Display 2D array elements


printf("The 2D array elements are:\n");

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

for (int j = 0; j < cols; j++) {

printf("%d ", arr[i][j]);

printf("\n");

return 0;

}
B) Write a program to perform a arithmetic operation (addition) of two matrix

#include <stdio.h>

int main() {

int rows, cols;

printf("Enter the number of rows: ");

scanf("%d", &rows);

printf("Enter the number of columns: ");

scanf("%d", &cols);

int matrix1[rows][cols], matrix2[rows][cols], sum[rows][cols];

// Read first matrix

printf("Enter elements of first matrix:\n");

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

for (int j = 0; j < cols; j++) {

scanf("%d", &matrix1[i][j]);

// Read second matrix

printf("Enter elements of second matrix:\n");

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

for (int j = 0; j < cols; j++) {

scanf("%d", &matrix2[i][j]);

// Perform addition

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

for (int j = 0; j < cols; j++) {

sum[i][j] = matrix1[i][j] + matrix2[i][j];


}

// Display sum matrix

printf("Sum of the two matrices:\n");

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

for (int j = 0; j < cols; j++) {

printf("%d ", sum[i][j]);

printf("\n");

return 0;

}
CT Group of Institutions, Maqsudan

Experiment Title: Functions

Laboratory: Programming for Problem Solving


(BTPS102-18)

Experiment No: 7 Class: B.TECH (AI&DS) Semester:1st

Practical 7: Functions

A) Write a program to perform arithmetic operation by using function

#include <stdio.h>

// Function prototypes

float add(float a, float b);

float subtract(float a, float b);

float multiply(float a, float b);

float divide(float a, float b);

int main() {

float num1, num2;

char op;

printf("Enter first number: ");

scanf("%f", &num1);

printf("Enter an operator (+, -, *, /): ");

scanf(" %c", &op);

printf("Enter second number: ");

scanf("%f", &num2);

switch(op) {
case '+':

printf("Result = %.2f\n", add(num1, num2));

break;

case '-':

printf("Result = %.2f\n", subtract(num1, num2));

break;

case '*':

printf("Result = %.2f\n", multiply(num1, num2));

break;

case '/':

if(num2 != 0)

printf("Result = %.2f\n", divide(num1, num2));

else

printf("Error! Division by zero.\n");

break;

default:

printf("Invalid operator!\n");

return 0;

// Function definitions

float add(float a, float b) {

return a + b;

float subtract(float a, float b) {


return a - b;

float multiply(float a, float b) {

return a * b;

float divide(float a, float b) {

return a / b;

B) Write a program to swap two numbers by using call by value and call by reference method

#include <stdio.h>

// Function for call by value

void swapByValue(int a, int b) {

int temp = a;

a = b;

b = temp;

printf("Inside swapByValue: a = %d, b = %d\n", a, b);

// Function for call by reference


void swapByReference(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

int main() {

int x, y;

printf("Enter two numbers: ");

scanf("%d %d", &x, &y);

// Swap using call by value

printf("\nBefore swapByValue: x = %d, y = %d\n", x, y);

swapByValue(x, y);

printf("After swapByValue: x = %d, y = %d\n", x, y); // values remain unchanged

// Swap using call by reference

printf("\nBefore swapByReference: x = %d, y = %d\n", x, y);

swapByReference(&x, &y);

printf("After swapByReference: x = %d, y = %d\n", x, y); // values swapped

return 0; }
C) Write a program to find a factorial of any number by using functions

#include <stdio.h>

// Function to calculate factorial

int factorial(int n) {

int fact = 1;

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

fact *= i;

return fact;

int main() {

int num;

printf("Enter a positive integer: ");

scanf("%d", &num);

if (num < 0) {

printf("Factorial is not defined for negative numbers.\n");

} else {

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

return 0;

You might also like