0% found this document useful (0 votes)
215 views35 pages

FPL Prac Mannual

Uploaded by

sahilbutale06
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)
215 views35 pages

FPL Prac Mannual

Uploaded by

sahilbutale06
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
You are on page 1/ 35

Rajgad Dnyanpeeth’s

RAJGAD DNYANPEETH TECHNICAL CAMPUS


SHRI CHHATRAPATI SHIVAJIRAJE COLLEGE OF ENGINEERING
S.No.237,Satara-Pune,NH-4,Dhangawadi,Tal:Bhor,Dist:Pune(Maharashtra)
Tal-Bhor Dist-Pune

Fundamentals of Programming Language


Sr.No Practical- no List of Practicals
1 Practical-1 To accept the number and Compute basic mathematical
operations.
2 Practical -2 To accept from user the number of Fibonacci numbers to be
generated and print the Fibonacci series
3
Practical -3 To accept an object mass in kilograms and velocity in
meters per second and display its Momentum.

Practical -4 In array doing the array operations.


4

5
Practical -5 Write a C program for employee salary calculation given,
Basic, H.R.A. 20 % of Basic and D.A. 150 % of Basic.

6
Practical -6 To accept a student's marks for five subjects, compute
his/her result.

7 Practical -7 To accept two numbers from user and compute smallest


divisor and Greatest Common Divisor of these two numbers.
8
Practical -8 Write a C program that accepts a string from the user and
performs the string operations-

RDTC/SCSCOE/FE/CHEMpg.1
Rajgad Dnyanpeeth’s
RAJGAD DNYANPEETH TECHNICAL CAMPUS

SHRI CHHATRAPATI SHIVAJIRAJE COLLEGE OF ENGINEERING

S.No.237,Satara-Pune,NH-4,Dhangawadi,Tal:Bhor,Dist:Pune(Maharashtra)

Name of The Student:

Class: Div: Batch:

Date of Performance:
Signature of staff with date

Practical No: 01 Marks obtained:

TITLE:- To accept the number and Compute a) square root of


number, b) Square of number, c) Cube of number d) check for
prime, d) factorial of number e) prime factors.

OBJECTIVES: 1. To understand different types of operators.


2. To understand basic logic building and syntax.

OUTCOMES:

After this experiment, students will acquire knowledge of operators in


C language and how to use them.

AIM: (A) STUDY OF IF-ELSE STATEMENTS IN C.


(B) DECLARATION OF VARIABLES IN C.
(C) ACCEPTING AND DISPLAYING INPUT VALUES.
(D) CALCULATE DIFFERENT OPERATION ON NUMBER.

THEORY:

This assignment will read the number and perform different operations
on that number.
Variable:
In programming, a variable is a container (storage area) to hold data. To indicate the
storage area, each variable should be given a unique name (identifier). Variable names are
just the symbolic representation of a memory location.

For Loop:

The for loop in C Language provides a functionality/feature to repeat a set of statements a


defined number of times. The for loopis in itself a form of an entry-controlled loop.

Unlike the while loop and do…while loop, the for loop contains the initialization,
condition, and updating statements as part of its syntax. It is mainly used to traverse
arrays, vectors, and other data structures.

Syntax of for Loop

for( initialization; check/test expression; updation )


{
// body consisting of multiple statements
}

Flowchart:

ALGORITHM:

1. Start
2. Declare an integer variable to give input for square root, square, checking for prime,
factorial, prime factors calculation.
3. Declare an integer variable to store the output value.
4. Perform square root, square operation and store result into another variable.
5. Declare a flag variable to set the initial value=0.
6. If input number is 0 and 1 then flag set to 1 and display it is prime no.
7. If n is divisible by i, then n is not prime.
8. If n is not divisible by i then n is prime.
9. calculate fact=fact * i
10. Use the condition as if (num % i == 0) then display prime no.
11. Display the result.
12. End

CONCLUSION: Thus, we have successfully calculated the gross salary of an


employee in python using decision control statement

Code:

A) Square Root Of Number:

#include <stdio.h>
#include <math.h>

int main()
{
double number, squareRoot;

// Input a number from the user

printf("Enter a number: ");

scanf("%lf", &number);

// Check if the number is non-negative


if (number < 0) {
printf("Square root of a negative number is not defined.\n");
} else {
// Calculate the square root
squareRoot = sqrt(number);

// Output the result


printf("The square root of %.2lf is %.2lf\n", number, squareRoot);

return 0; }
// OUTPUT:
admin1@admin1-ThinkCentre-M92p:~$ gcc squareeroot.c -o squareeroot -lm
admin1@admin1-ThinkCentre-M92p:~$ ./squareeroot
Enter a number: 36
The square root of 36.00 is 6.00

B) Square Of Number:

#include <stdio.h>
int main()
{
int number, square;

// Ask the user for input

printf("Enter a number: ");

scanf("%d", &number);

// Calculate the square


square = number * number;
// Display the result
printf("The square of %d is %d\n", number, square);

return 0; }

// OUTPUT:
admin1@admin1-ThinkCentre-M92p:~$ touch squaree.c admin1@admin1-
ThinkCentre-M92p:~$ gcc squaree.c -o squaree
admin1@admin1-ThinkCentre-M92p:~$ ./squaree
Enter a number: 25
The square of 25 is 625

C) Cube Of Number:

#include <stdio.h>
int main()
{
double number, cube;

// Input a number from the user

printf("Enter a number: ");

scanf("%lf", &number);

// Calculate the cube

cube = number * number * number;


// Output the result
printf("The cube of %.2lf is %.2lf\n", number, cube);

return 0; }

// OUTPUT:
admin1@admin1-ThinkCentre-M92p:~$ touch cubee.c admin1@admin1-
ThinkCentre-M92p:~$ gcc cubee.c -o cubee admin1@admin1-ThinkCentre-
M92p:~$ ./cubee
Enter a number: 12
The cube of 12.00 is 1728.0

D) Check For Prime:

#include <stdio.h>

int main() {
int number, i, isPrime = 1; // Assume number is prime

// Input a number from the user

printf("Enter a number: ");

scanf("%d", &number);

// Check for prime


if (number <= 1) {
isPrime = 0; // Numbers less than 2 are not prime
} else {
for (i = 2; i * i <= number; i++) {
if (number % i == 0) {
isPrime = 0; // Found a divisor, not prime
break;
}
}
}
// Print Output the result
if (isPrime) {
printf("%d is a prime number.\n", number);
} else {
printf("%d is not a prime number.\n", number); }

return 0; }

//OUTPUT:
admin1@admin1-ThinkCentre-M92p:~$ touch primeornot.c admin1@admin1-ThinkCentre-
M92p:~$ gcc primeornot.c -o primeornot
admin1@admin1-ThinkCentre-M92p:~$ ./primeornot Enter a number: 56
56 is not a prime number.
E) Factorial Of Number:

#include <stdio.h>
int main()
{ int number;
long long factorial = 1; // Using long long to handle large results
// Input a number from the user

printf("Enter a positive integer: ");

scanf("%d", &number);

// Check if the number is negative


if (number < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
int i = 1; // Start from 1 while (i <= number) {
factorial *= i; // Multiply factorial by i
i++; // Increment i
}
// Output the result
printf("Factorial of %d is %lld\n", number, factorial); }

return 0; }

//OUTPUT:
admin1@admin1-ThinkCentre-M92p:~$ touch factorial.c admin1@admin1-ThinkCentre-
M92p:~$ gcc factorial.c -o factorial
admin1@admin1-ThinkCentre-M92p:~$ ./factorial Enter a positive
integer: 13 Factorial of 13 is 6227020800.

F) Prime Factors:
#include <stdio.h>
void primeFactors(int number)
{
printf("Prime factors of %d are: ", number);
while (number % 2 == 0) { printf("2 ");
number /= 2;
}
// Check for odd factors from 3 to sqrt(number)
for (int i = 3; i * i <= number; i += 2) {
while (number % i == 0) { printf("%d ", i);
number /= i;
}
}
// This condition is to check if number is a prime number greater than 2
if (number > 2) {
printf("%d ", number);
}
printf("\n");
} int main() { int number;
// Input a number from the user
printf("Enter a number: "); s
canf("%d", &number);
primeFactors(number);
return 0;
}

// OUTPUT:
admin1@admin1-ThinkCentre-M92p:~$ touch prime_no.c admin1@admin1-
ThinkCentre-M92p:~$ gcc prime_no.c -o prime_no
admin1@admin1-ThinkCentre-M92p:~$ ./prime_no
Enter a number: 60
Prime factors of 60 are: 2 2 3 5
Rajgad Dnyanpeeth’s

RAJGAD DNYANPEETH TECHNICAL CAMPUS

SHRI CHHATRAPATI SHIVAJIRAJE COLLEGE OF ENGINEERING

S.No.237,Satara-Pune,NH-4,Dhangawadi,Tal:Bhor,Dist:Pune(Maharashtra)

Name of The Student:

Class: Div: Batch:

Date of Performance:
Signature of staff with date

Practical No: 02 Marks obtained:

TITLE: To accept from the user the number of Fibonacci numbers to be generated
and print the Fibonacci series.

OBJECTIVES: 1. To study Decision Control Statements

2. To understand and apply if and else statements.

OUTCOMES:

After this experiment, students will acquire knowledge of decision control structures in
python and its application.

AIM: (A) STUDY OF IF-ELSE STATEMENTS IN C

(B) DECLARATION OF VARIABLES IN C

(C) ACCEPTING AND DISPLAYING INPUT VALUES (D)UNDERSTAND

AND CALCULATE FIBONACCI SERIES.

THEORY:

Fibonacci numbers in the following integer sequence, called the Fibonacci sequence:
0,1,1,2,3,5,8,13,21,34,55,89,144…..

The first two numbers in the Fibonacci sequence are 0 and 1 and each subsequent number is
the sum of the previous two. Mathematically Fn = Fn-1 + Fn-2

With first two numbers

F0 =0, F1 =1 Following numbers can be calculated

F2 = F0 + F1 = 0+1=1

F3 = F1 + F2 = 1+1=2
F4 = F2 + F3 = 2+1=3
F5 = F3 + F4 = 2+3=5 likewise

ALGORITHM:
Step 1: start
Step 2: declare f1,f2,f3,n,i
Step3: initialize f1=0,f2=1
Step 4: accept the value of n from user
Step 5: print values of f1 and f2
Step 6: for i=2 to n
Step 7:f3=f2+f1
Step 8: print the value of f3
Step 9: assign f2 to f1 and f3 to f2
Step 10:end for
Step 11: stop
FLOWCHART:
CONCLUSION:

Thus, we have successfully generated Fibonacci series in python using decision


control statements.

2. To accept from user the number of Fibonacci numbers to be generated and print the
Fibonacci series:
#include <stdio.h>
int main() {
int i, n;
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
// get no. of terms from user
printf("Enter the number of terms: \n ");
scanf("%d", &n);
// print the first two terms t1 and t2
printf("Fibonacci Series: %d, %d, ", t1, t2);
// print 3rd to nth terms
for (i = 3; i <= n; ++i) {
printf("%d \n", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
//Output:-
Enter the number of terms:
3
Fibonacci Series: 0, 1, 1
Rajgad Dnyanpeeth’s

RAJGAD DNYANPEETH TECHNICAL CAMPUS

SHRI CHHATRAPATI SHIVAJIRAJE COLLEGE OF ENGINEERING

S.No.237,Satara-Pune,NH-4,Dhangawadi,Tal:Bhor,Dist:Pune(Maharashtra)

Name of The Student:

Class: Div: Batch:

Date of Performance:
Signature of staff with date

Practical No: 03 Marks obtained:

TITLE:To accept an object mass in kilograms and velocity in meters per second and display its
Momentum. Momentum is calculated as e=mc2 where m is the mass of the object and c is its velocity:

OBJECTIVES:
1. To study Decision control statements
2.To understand double keyword .

OUTCOMES:

After this experiment, students will acquire knowledge of calculating the speed of a
light.

THEORY:

In physics, the energy–momentum relation, or relativistic dispersion relation, is


the relativistic equation relating total energy (which is also called relativistic energy) to invariant
mass (which is also called rest mass) and momentum. It is the extension of mass–energy
equivalence for bodies or systems with non-zero momentum.

This equation holds for a body or system, such as one or more particles, with total energy E,
invariant mass m0, and momentum of magnitude p; the constant c is the speed of light. It assumes
the special relativity case of flat spacetime and that the particles are free. Total energy is the sum
of rest energy and relativistic kinetic energy:Invariant mass is mass measured in a center-of-
momentum frame. For bodies or systems with zero momentum, it simplifies to the mass–energy
equation , where total energy in this case is equal to rest energy

CONCLUSION: Thus, we have successfully determined the momentum using the


formula E=MC2

Code-
#include <stdio.h>
#define SPEED_OF_LIGHT 299792458 // Speed of light in meters per second

double calculateEnergy(double mass) {


return mass * SPEED_OF_LIGHT * SPEED_OF_LIGHT; // E = mc^2
}

int main() {
double mass; // Mass in kilograms
printf("Enter mass in kg: ");
scanf("%lf", &mass);

double energy = calculateEnergy(mass);


printf("Energy (E) = %lf Joules\n", energy);

return 0;
}
//Output
Enter mass in kg: 2
Energy (E) = 179751035747363520.000000 Joules.
Rajgad Dnyanpeeth’s

RAJGAD DNYANPEETH TECHNICAL CAMPUS

SHRI CHHATRAPATI SHIVAJIRAJE COLLEGE OF ENGINEERING

S.No.237,Satara-Pune,NH-4,Dhangawadi,Tal:Bhor,Dist:Pune(Maharashtra)

Name of The Student:

Class: Div: Batch:

Date of Performance:
Signature of staff with date

Practical No: 04 Marks obtained:

TITLE: A Study of Array Data Type

2. Find a given element in array


3. Find Max element
4. Find Min element
5. Find frequency of given element in array 5. Find Average of elements in
Array.

OBJECTIVES:
1. To study Decision control statements
2. To understand and apply if-else and for statements.

OUTCOMES:

After this experiment, students will acquire knowledge of decision control


structures in C and its application.

AIM: (A) STUDY OF IF-ELSE AND FOR STATEMENTS IN C.


(B) DECLARATION OF VARIABLES IN C.
(C) ACCEPTING AND DISPLAYING INPUT VALUES.
(D) CALCULATE MINIMUM, MAXIMUM, AVG. AND
FREQUENCY FROM ARRAY.
THEORY:
An array in C is a fixed-size collection of similar data items stored in
contiguous memory locations. It can be used to store the collection of
primitive data types such as int, char, float, etc., and also derived and user-
defined data types such as pointers, structures, etc.

Syntax of Array Declaration:

data_typearray_name [size1] [size2]...[sizeN];

Where N is the number of dimensions.

The ‘data type’ in the declaration refers to the type of each element in
the array. ‘array_name’ refers to the name by which the memory
locations will be identified. ‘MAX_SIZE’ indicates the maximum
number of elements that can be stored in the array.

For example, an array with name ‘a’ having maximum 5 integer


elements can be declared as int a[5];
This declaration allocates 5 consecutive memory locations that can
store 5 integer values. The elements are referred to using the
combination of array name and index value. The array index is
referred to as ‘subscript’. For example, elements are referred to as
a[0], a[1], a[2], …, a[MAX_SIZE-1]. Following figure shows the
scenario of the above declaration in memory:

Address: 3001 3003 3005 3007 3009

Index: 0 1 2 3 4

The number of memory locations required to hold an array depends on its type
and size. For a one dimensional array, its total size is,
Total Size=Size of array*Size of (array data type).
In above declaration, size of array ‘a’ is 5*2=10 bytes.
Array Initialization

An integer array can be initialized during its declaration.


For example, int marks[4]={67,80,55,79};
char grades[4]={‘A’, ‘B’, ‘C’, ‘D’};
The elements will be stored in the memory as shown in the figure below

Address: 5010 5012 5013 5015


67 80 55 79
Index: 0 1 2 3

ALGORITHM:

1. Declare an Array with size, key and element found, sum, avg
variable. 2. Enter the size of array and no of elements in array
3. Enter the key element to be searched.
4. Compare the key element with every element
5. if element matched then display element found and if not then
display element not found.
6. Perform addition of all elements.
7. Perform an average of it.
8. Display sum and average both.

CONCLUSION: Thus, we have successfully determined about array in C using decision


control statements.

Code:-

1. Find given element in array:

#include <stdio.h>
int main() {
int array[100], n, search, i;
int found = 0; // Flag to indicate if the element is found
// Input the number of elements in the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
// Input the elements of the array
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &array[i]);
}
// Input the element to search for
printf("Enter the element to search for: ");
scanf("%d", &search);
// Search for the element in the array
for (i = 0; i < n; i++) {
if (array[i] == search) {
printf("Element %d found at index %d.\n", search, i);
found = 1; // Set the flag to indicate that the element was found
break; // Exit the loop once the element is found
}
}
if (!found) {
printf("Element %d not found in the array.\n", search);
}
return 0;
}
//OUTPUT:
admin1@admin1-ThinkCentre-M92p:~$ gccelement.c -o element
admin1@admin1-ThinkCentre-M92p:~$ ./element
Enter the number of elements in the array: 10 12 13 15 18 19 20
Enter 10 elements:
10 12 15 17 18 12 14 15 19 16
Enter the element to search for: Element 18 found at index 3.
admin1@admin1-ThinkCentre-M92p:~$ gccelement.c -o element
admin1@admin1-ThinkCentre-M92p:~$ ./element
Enter the number of elements in the array: 10 12 15 20 18 90 40 50 42 30
Enter 10 elements:
1235620749
Enter the element to search for: Element 2 not found in the array.

2. Find Max and Min element:

#include <stdio.h>
int main() {
int array[100], n, i;
int max, min;
// Input the number of elements in the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
// Input the elements of the array
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &array[i]);
}
// Initialize max and min to the first element of the array
max = min = array[0];
// Loop through the array to find max and min
for (i = 1; i < n; i++) {
if (array[i] > max) {
max = array[i];
}
if (array[i] < min) {
min = array[i];
}
}
// Print the results
printf("Maximum element: %d\n", max);
printf("Minimum element: %d\n", min);
return 0;
}
//OUTPUT:
admin1@admin1-ThinkCentre-M92p:~$ touch max_min_ele.c
admin1@admin1-ThinkCentre-M92p:~$ gccmax_min_ele.c -o max_min_ele
admin1@admin1-ThinkCentre-M92p:~$ ./max_min_ele
Enter the number of elements in the array: 12 14 52 56 14 89 45 62 45 20
Enter 12 elements:
12 14 52 78 47 45 85 46 25 36 12 0
Maximum element: 89
Minimum element: 12

3. Find frequency of given element in array:

#include <stdio.h>
int main() {
int array[100], n, search, frequency = 0;
// Input the number of elements in the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
// Input the elements of the array
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &array[i]);
}
// Input the element to find the frequency of
printf("Enter the element to find its frequency: ");
scanf("%d", &search);
// Calculate the frequency of the element
for (int i = 0; i < n; i++) {
if (array[i] == search) {
frequency++;
}
}
// Print the frequency
if (frequency > 0) {
printf("Element %d occurs %d times in the array.\n", search, frequency);
} else {
printf("Element %d is not present in the array.\n", search);
}
return 0;
}
//OUTPUT:
admin1@admin1-ThinkCentre-M92p:~$ touch frquency_ele.c
admin1@admin1-ThinkCentre-M92p:~$ gccfrquency_ele.c -o frquency_ele
admin1@admin1-ThinkCentre-M92p:~$ ./frquency_ele
Enter the number of elements in the array: 12 14 52 62 85 47 92 60 32 10 11 45
Enter 12 elements:
12 14 52 62 85 47 92 60 32 10 11 45
Enter the element to find its frequency: Element 14 occurs 1 times in the array.

4. Find Average of elements in Array:

#include <stdio.h>
int main() {
int n, i;
float num[100], sum = 0.0, avg;
printf("Enter the numbers of elements: ");
scanf("%d", &n);
while (n > 100 || n < 1) {
printf("Error! number should in range of (1 to 100).\n");
printf("Enter the number again: ");
scanf("%d", &n);
}
for (i = 0; i < n; ++i) {
printf("%d. Enter number: ", i + 1);
scanf("%f", &num[i]);
sum += num[i];
}
avg = sum / n;
printf("Average = %.2f", avg);
return 0;
}
//OUTPUT:
admin1@admin1-ThinkCentre-M92p:~$ touch average_ele.c
admin1@admin1-ThinkCentre-M92p:~$ gccaverage_ele.c -o average_ele
admin1@admin1-ThinkCentre-M92p:~$ ./average_ele
Enter the numbers of elements: 5
1. Enter number: 10
2. Enter number: 20
3. Enter number: 30
4. Enter number: 40
5. Enter number: 50
Average = 30.00
Rajgad Dnyanpeeth’s

RAJGAD DNYANPEETH TECHNICAL CAMPUS

SHRI CHHATRAPATI SHIVAJIRAJE COLLEGE OF ENGINEERING

S.No.237,Satara-Pune,NH-4,Dhangawadi,Tal:Bhor,Dist:Pune(Maharashtra)

Name of The Student:

Class: Div: Batch:

Date of Performance:
Signature of staff with date

Practical No: 05 Marks obtained:

TITLE: Write a C program for employee salary calculation given, Basic, H.R.A.
20% Basic and D.A. 150 % of Basic.

OBJECTIVES:
1. To study Decision Control Statements
2. To understand and apply if and else statements.
OUTCOMES:

After this experiment, students will acquire knowledge of decision control


structures in C and its application.

AIM: (A) STUDY OF IF-ELSE AND WHILE STATEMENTS IN C


(B)DECLARATION OF VARIABLES IN C
(C)ACCEPTING AND DISPLAYING
(D) CALCULATE SALARY OF EMPLOYEE

THEORY:

This assignment will read Basic Salary of an employee, calculate other


parts of the salary on percent basic and finally print the Salary of the employee.
Here, we are reading the basic salary of the employee, and calculating HRA, DA,
salary of the employee.
Variable:
In programming, a variable is a container (storage area) to hold data. To indicate the storage
area, each variable should be given a unique name. Variable names are just the symbolic
representation of a memory location.

For example: int age=24;

ALGORITHM:
1. Declare all variables da, hra, gross.
2. Enter the salary from the user.
3. Perform calculation on basic salary when HRA is 20% and DA is 150%.
4. Calculate gross salary.
5. Display gross salary.

CONCLUSION:

Thus, we have successfully calculated the gross salary of an employee in C


using decision control statements.
Code:-
#include <stdio.h>
int main() {
float basicSalary, hra, da, totalSalary;
// Input the basic salary
printf("Enter the Basic Salary: ");
scanf("%f", &basicSalary);
// Calculate H.R.A and D.A
hra = 0.20 * basicSalary; // 20% of Basic
da = 1.50 * basicSalary; // 150% of Basic
// Calculate total salary
totalSalary = basicSalary + hra + da;
// Display the results
printf("\n--- Salary Details ---\n");
printf("Basic Salary: %.2f\n", basicSalary);
printf("H.R.A (20%%): %.2f\n", hra);
printf("D.A (150%%): %.2f\n", da);
printf("Total Salary: %.2f\n", totalSalary);
return 0;
}
// OUTPUT:
admin1@admin1-ThinkCentre-M92p:~$ touch practical5.c
admin1@admin1-ThinkCentre-M92p:~$ gcc practical5.c -o practical5
admin1@admin1-ThinkCentre-M92p:~$ ./practical5
Enter the Basic Salary: 25000
--- Salary Details ---
Basic Salary: 25000.00
H.R.A (20%): 5000.00
D.A (150%): 37500.00
Total Salary: 67500.00
Rajgad Dnyanpeeth’s
RAJGAD DNYANPEETH TECHNICAL CAMPUS

SHRI CHHATRAPATI SHIVAJIRAJE COLLEGE OF ENGINEERING

S.No.237,Satara-Pune,NH-4,Dhangawadi,Tal:Bhor,Dist:Pune(Maharashtra)

Name of The Student:

Class: Div: Batch:

Date of Performance:
Signature of staff with date

Practical No: 06 Marks obtained:

EXPERIMENT NO: 6
TITLE: To accept a student's marks for five subjects, compute his/her result.
Student is passing if he/she scores marks equal to and above 40 in each course. If
student scores aggregate greater than 75%, then the grade is distinguished. If
aggregate is 60>= and <75 then the Grade of first division. If aggregate is 50>= and
<60 then the grade is second division. If aggregate is 40>= and <50 then the grade is
third division.

OBJECTIVES:
1. To study Decision Control Statements
2. To understand and apply an if-else statement.

OUTCOMES:

After this experiment, students will acquire knowledge of decision control


structures in C and its application.

THEORY:

The if-else statement in C is a flow control statement used for decision-making


in the C program. It is one of the core concepts of C programming. It is an
extension of the if in C that includes an else block along with the already
existing if block.
if Statement:
Theif statementin C is used to execute a block of code based on a specified
condition.The syntax of the if statement in C is:

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

if-else Statement:

The if-else statement is a decision-making statement that is used to decide whether the
part of the code will be executed or not based on the specified condition (test
expression). If the given condition is true, then the code inside the if block is executed,
otherwise the code inside the else block is executed.
Syntax of if-else:

if (condition) {
// code executed when the condition is true
}
else {
// code executed when the condition is false
}
if-else-if Ladder in C

Theif else if statementsare used when the user has to decide among multiple options. The
C if statements are executed from the top down. As soon as one of the conditions
controlling the if is true, the statement associated with that if is executed, and the rest of
the C else-if ladder is bypassed. If none of the conditions is true, then the final else
statement will be executed. if-else-if ladder is similar to the switch statement.
Syntax of if-else-if Ladder
if (condition)
statement;else if
(condition)
statement;
.
.
elsestatement;

Flowchart of if-else-if Ladder


Algorithm:

1. Declare num variable.

2. Enter the marks from user .

3. Check the condition if marks >75 and marks <60 then it is distinction.

4. Check the condition if marks >60 and marks <50 then it is first class.

5. Check the condition if marks >50 and marks <40 then it is second class.

6. if marks <40 then it is fail.

CONCLUSION:
Thus, we have successfully compute the marks and display its grade using decision
control statement.

Code:-
#include <stdio.h>
int main() {
float marks[5];
float total = 0.0;
float average;
int passing = 1; // Flag to check if the student is passing
int subjects = 5;
// Input marks for each subject
printf("Enter marks for %d subjects:\n", subjects);
for (int i = 0; i < subjects; i++) {
printf("Subject %d: ", i + 1);
scanf("%f", &marks[i]);
// Check if the student is passing in each subject
if (marks[i] < 40) {
passing = 0;
}
// Calculate total marks
total += marks[i];
}
// Calculate average
average = total / subjects;
// Determine grade
char *grade;
if (average > 75) {
grade = "Distinction";
} else if (average >= 60) {
grade = "First Division";
} else if (average >= 50) {
grade = "Second Division";
} else if (average >= 40) {
grade = "Third Division";
} else {
grade = "Fail";
}
// Display results
printf("\n--- Result Summary ---\n");
printf("Total Marks: %.2f\n", total);
printf("Average Marks: %.2f\n", average);
printf("Grade: %s\n", passing ? grade : "Fail");

return 0;
}
//OUTPUT:
admin1@admin1-ThinkCentre-M92p:~$ touch practical6.c
admin1@admin1-ThinkCentre-M92p:~$ gcc practical6.c -o practical6
admin1@admin1-ThinkCentre-M92p:~$ ./practical6
Enter marks for 5 subjects:
Subject 1: 75
Subject 2: 63
Subject 3: 52
Subject 4: 90
Subject 5: 70
--- Result Summary ---
Total Marks: 350.00
Average Marks: 70.00
Grade: First Division
Rajgad Dnyanpeeth’s
RAJGAD DNYANPEETH TECHNICAL CAMPUS

SHRI CHHATRAPATI SHIVAJIRAJE COLLEGE OF ENGINEERING

S.No.237,Satara-Pune,NH-4,Dhangawadi,Tal:Bhor,Dist:Pune(Maharashtra)

Name of The Student:

Class: Div: Batch:

Date of Performance:
Signature of staff with date

Practical No: 07 Marks obtained:

Title: To accept two numbers from the user and compute the smallest divisor
and Greatest Common Divisor of these two numbers.

OBJECTIVES: 1. To understand different types of operators.


2. To understand decision control statements.

OUTCOMES:

After this experiment, students will acquire knowledge of decision control


statements in C language and how to use them.

AIM: (A) STUDY OF FOR LOOP IN C.


(B)DECLARATION OF VARIABLES IN C.
(C)ACCEPTING AND DISPLAYING INPUT VALUES.
(D)CALCULATE DIFFERENT OPERATION ON NUMBER.

THEORY:

This assignment will read the number and perform different operations on
that number.GCD stands for Greatest Common Divisor and is also known as
HCF (Highest Common Factor). The GCD of two numbers is the largest
positive integer that completely divides both numbers without leaving a
remainder.
Variable:
In programming, a variable is a container (storage area) to hold data. To indicate the storage
area, each variable should be given a unique name (identifier). Variable names are just the
symbolic representation of a memory location.

For Loop:

The for loop in C Language provides a functionality/feature to repeat a set of statements a


defined number of times. The for loopis in itself a form of an entry-controlled loop.
Unlike the while loop and do…while loop, the for loop contains the initialization,
condition, and updating statements as part of its syntax. It is mainly used to traverse
arrays, vectors, and other data structures.
Syntax of for Loop

for(initialization; check/test expression; updation )


{
// body consisting of multiple statements
}

Flowchart:
Algorithm:

● Step 1: Find the product of a and b.


● Step 2: Find theLeast Common Multiple(LCM) of a and b.
● Step 3: Divide the product of the numbers by the LCM of the numbers.
● Step 4: The obtained value after division is the greatest common divisor of (a, b).

CONCLUSION:
Thus, we have successfully calculated GCD and LCM in C using decision control
statements.

Code:-

#include <stdio.h>
// Function to find the smallest divisor greater than 1
int smallest_divisor(int num) {
for (int i = 2; i<= num / 2; i++) {
if (num % i == 0) {
return i; // Return the smallest divisor
}
}
return num; // If no divisor found, the number is prime
}
// Function to find the GCD using the Euclidean algorithm
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a; // GCD
}
int main() {
int num1, num2;
// Input two numbers
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// Calculate smallest divisors
int divisor1 = smallest_divisor(num1);
int divisor2 = smallest_divisor(num2);
// Calculate GCD
int gcd_value = gcd(num1, num2);
// Display results
printf("\n--- Results ---\n");
printf("Smallest divisor of %d: %d\n", num1, divisor1);
printf("Smallest divisor of %d: %d\n", num2, divisor2);
printf("GCD of %d and %d: %d\n", num1, num2, gcd_value);
return 0;
}
//OUTPUT:
admin1@admin1-ThinkCentre-M92p:~$ touch practical7.c
admin1@admin1-ThinkCentre-M92p:~$ gcc practical7.c -o practical7
admin1@admin1-ThinkCentre-M92p:~$ ./practical7
Enter two numbers: 58 63
--- Results ---
Smallest divisor of 58: 2
Smallest divisor of 63: 3
GCD of 58 and 63: 1
Rajgad Dnyanpeeth’s
RAJGAD DNYANPEETH TECHNICAL CAMPUS

SHRI CHHATRAPATI SHIVAJIRAJE COLLEGE OF ENGINEERING

S.No.237,Satara-Pune,NH-4,Dhangawadi,Tal:Bhor,Dist:Pune(Maharashtra)

Name of The Student:

Class: Div: Batch:

Date of Performance:
Signature of staff with date

Practical No: 08 Marks obtained:

TITLE: Write a C program that accepts a string from the user and performs the
following string operations- i. Calculate length of string ii. String reversal iii.
Equality check of two Strings iii. Check palindrome ii. Check substring

OBJECTIVES:
1. To understand different types of built in functions.
2. To understand different syntax of built in functions..

OUTCOMES:

After this experiment, students will acquire knowledge of built in functions in C


language and how to use them.

AIM: (A) STUDY OF STRING IN C.


(B) DECLARATION OF STRING VARIABLES IN C.
(C) ACCEPTING AND DISPLAYING STRING INPUT.
(D) PERFORM DIFFERENT OPERATION ON STRING.

THEORY:
The C string functions are built-in functions that can be used for various operations
and manipulations on strings. These string functions can be used to perform tasks
such as string copy, concatenation, comparison, length, etc. The <string.h>header
file contains these string functions.
String Functions in C
1.strlen() Function
Thestrlen() functioncalculates the length of a given string. It doesn’t count the null
character ‘\0’.
Syntax
int strlen(const char *str);

2. strcmp() Function

Thestrcmp()is a built-in library function in C. This function takes two strings as


arguments and compares these two strings lexicographically.
Syntax
int strcmp(const char *str1, const char *str2);

3strchr() Function
Thestrchr() function in Cis a predefined function used for string handling. This function is
used to find the first occurrence of a character in a string.Syntax
char *strchr(const char *str, int c);
4 strrev() Function

It is a built-in function in C and is defined in string.h header file. The strrev() function is
used to reverse the given string.
Syntax:
char *strrev(char *str);

Algorithm:

1. Declare string variables with size.


2. Enter the string.
3. Calculate length, reverse,equal,palindrome and substring of the string using built in
function.
4. Compute the result.
5. Display the result.

i. CONCLUSION: Thus , we have successfully performed operations on string.


Code- Calculate length of string:

#include <stdio.h>
int main() {
char str[100]; // Declare an array to hold the string
int length = 0; // Initialize length counter
// Prompt the user for input
printf("Enter a string: ");
fgets(str, sizeof(str), stdin); // Read a string from the user
// Calculate the length of the string
while (str[length] != '\0') {
// Check for the newline character added by fgets
if (str[length] == '\n') {
break;
}
length++;
}
// Output the length of the string
printf("The length of the string is: %d\n", length);
return 0;
}
//OUTPUT:
admin1@admin1-ThinkCentre-M92p:~$ touch string_length.c
admin1@admin1-ThinkCentre-M92p:~$ gccstring_length.c -o string_length
admin1@admin1-ThinkCentre-M92p:~$ ./string_length
Enter a string: 12 15 14 18
The length of the string is: 11

ii. String reversal :

#include <stdio.h>
#include <string.h>
int main() {
char str[100]; // Declare an array to hold the string
char reversed[100]; // Array to hold the reversed string
int length, i;
// Prompt the user for input
printf("Enter a string: ");
fgets(str, sizeof(str), stdin); // Read a string from the user
// Remove the newline character added by fgets if present
length = strlen(str);
if (str[length - 1] == '\n') {
str[length - 1] = '\0';
length--; // Update length to exclude the newline
}
// Reverse the string
for (i = 0; i< length; i++) {
reversed[i] = str[length - 1 - i];
}
reversed[length] = '\0'; // Null-terminate the reversed string
// Output the reversed string
printf("Reversed string: %s\n", reversed);
return 0;
}
//OUTPUT:
admin1@admin1-ThinkCentre-M92p:~$ touch string_rev.c
admin1@admin1-ThinkCentre-M92p:~$ gccstring_rev.c -o string_rev
admin1@admin1-ThinkCentre-M92p:~$ ./string_rev
Enter a string: 12 25 23 24 46 47
Reversed string: 74 64 42 32 52 21

iii. Equality check of two Strings:

#include <stdio.h>
#include <string.h>
int main() {
char str1[100]; // Declare an array to hold the first string
char str2[100]; // Declare an array to hold the second string
// Prompt the user for the first string
printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin); // Read the first string
// Remove the newline character added by fgets if present
str1[strcspn(str1, "\n")] = '\0'; // This removes the newline character
// Prompt the user for the second string
printf("Enter the second string: ");
fgets(str2, sizeof(str2), stdin); // Read the second string
// Remove the newline character added by fgets if present
str2[strcspn(str2, "\n")] = '\0'; // This removes the newline character
// Check for equality of the two strings
if (strcmp(str1, str2) == 0) {
printf("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}
return 0;
}
//OUTPUT:
admin1@admin1-ThinkCentre-M92p:~$ touch string_equ.c
admin1@admin1-ThinkCentre-M92p:~$ gccstring_equ.c -o string_equ
admin1@admin1-ThinkCentre-M92p:~$ ./string_equ
Enter the first string: 12 14 15 16 32
Enter the second string: 14 52 12 58 75
The strings are not equal.
iv. Check palindrome:

#include <stdio.h>
#include <string.h>
int main() {
char str[100]; // Declare an array to hold the string
int length, i, isPalindrome = 1; // Initialize variables

// Prompt the user for input


printf("Enter a string: ");
fgets(str, sizeof(str), stdin); // Read a string from the user

// Remove the newline character added by fgets if present


length = strlen(str);
if (str[length - 1] == '\n') {
str[length - 1] = '\0'; // Replace newline with null terminator
length--; // Update length to exclude the newline
}

// Check if the string is a palindrome


for (i = 0; i< length / 2; i++) {
if (str[i] != str[length - 1 - i]) {
isPalindrome = 0; // Set flag to 0 if mismatch found
break;
}
}

// Output the result


if (isPalindrome) {
printf("The string is a palindrome.\n");
} else {
printf("The string is not a palindrome.\n");
}

return 0;
}

//OUTPUT:
admin1@admin1-ThinkCentre-M92p:~$ touch palindrome.c
admin1@admin1-ThinkCentre-M92p:~$ gccpalindrome.c -o palindrome
admin1@admin1-ThinkCentre-M92p:~$ ./palindrome
Enter a string: 52 12 18 96 32
The string is not a palindrome.

You might also like