0% found this document useful (0 votes)
13 views19 pages

Practicals of FPL

The document contains a series of C programming experiments aimed at teaching various arithmetic operations, including calculating square roots, squares, cubes, prime checking, factorials, Fibonacci series, momentum, and array manipulations. Each experiment includes specific code examples, input/output scenarios, and explanations of the algorithms used. Additionally, it covers employee salary calculations, student grading based on marks, finding the greatest common divisor, and string operations such as length calculation, reversal, and equality checking.

Uploaded by

sayyedmoiz2006
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)
13 views19 pages

Practicals of FPL

The document contains a series of C programming experiments aimed at teaching various arithmetic operations, including calculating square roots, squares, cubes, prime checking, factorials, Fibonacci series, momentum, and array manipulations. Each experiment includes specific code examples, input/output scenarios, and explanations of the algorithms used. Additionally, it covers employee salary calculations, student grading based on marks, finding the greatest common divisor, and string operations such as length calculation, reversal, and equality checking.

Uploaded by

sayyedmoiz2006
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/ 19

Experiment No.

1
Aim: To understand the arithmetic operations

Program No. 1: Write a c program to accept the number from keyboard and compute
square root of numbers.
Solution:-
// C program for the above approach
#include <math.h>
#include <stdio.h>
void main ( )
{
float n;
printf(“\n Enter the any number=”);
scanf(“%f”,&n);
// Function call
printf("%f ", sqrt(n));
}
Input: N = 16
Output: 4
Program 2: Write a c program to accept the number from keyboard and compute square
of numbers.
#include <stdio.h>
int main()
{
int num;
double n;
printf("\n\n Function : find square of any number :\n");
printf("------------------------------------------------\n");
printf("Input any number for square : ");
scanf("%d", &num);
n = num * num;
printf("The square of %d is : %.2f\n", num, n);
}
Function: find square of any number:
------------------------------------------------
Input any number for square: 10
The square of 10 is: 100.00

Page 1 of 19
Program 3: Write a c program to accept the number from keyboard and compute cube of
numbers.
#include <stdio.h>
int main()
{
int num;
double n;
printf("\n\n Function : find cube of any number :\n");
printf("------------------------------------------------\n");
printf("Input any number for cube : ");
scanf("%d", &num);
n = num * num *num;
printf("The cube of %d is : %.2f\n", num, n);
}
Function: find cube of any number:
------------------------------------------------
Input any number for cube: 10
The cube of 10 is : 1000.00
Program No. 4: Write a c program to accept the number from keyboard and check the
entered number is prime or no.
#include <stdio.h>
void main( )
{
int i, num, temp = 0;
// read input from user.
printf(“Enter the number you want to Check for Prime: “);
scanf(“%d”, &num);
// Run loop for num/2
for (i = 2; i <= num / 2; i++)
{
// check if given number is divisible by any number.
if (num % i == 0)
{
temp++;
break;
}
}
// check for the value of temp and num.
if (temp == 0 && num != 1)
printf(“%d is a Prime number”, num);
else
printf(“%d is not a Prime number”, num);

Page 2 of 19
}
INPUT: Enter the number you want to check for Prime: 11
OUTPUT: 11 is a Prime number

Program No. 4: Write a c program to accept the number from keyboard and find the
factorial of entered number.

#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}

Program No. 4: Write a c program to accept the number from keyboard and find the prime
factors of entered number.
/ C Program to print all prime factors
#include <stdio.h>
void main()
{
int n;
printf(“\n Enter a number=”);
scanf(“%d”,&n);
while (n % 2 == 0)
{
printf("%d ", 2);
n = n / 2;
}

// n must be odd at this point. So we can skip


// one element (Note i = i +2)
for (int i = 3; i * i <= n; i = i + 2)
{
// While i divides n, print i and divide n
while (n % i == 0)
{
printf("%d ", i);
n = n / i;
}

Page 3 of 19
}

// This condition is to handle the case when n


// is a prime number greater than 2
if (n > 2)
printf("%d ", n);
}
Input: Enter a number=12
Output: 2 2 3

Page 4 of 19
Experiment No. 2
Aim: To understand and generate Fibonacci numbers from an entered number
The Fibonacci series is the sequence where each number is the sum of the previous two
numbers of the sequence. The first two numbers are 0 and 1 which are used to generate the
whole series.
#include <stdio.h>
void main( )
{
int n;
printf(“\n Enter any number=”);
scanf(“%d”, &n);
if (n < 1)
printf("Invalid Number of terms\n");
else
{
// First two terms of the series
int prev1 = 1;
int prev2 = 0;
// for loop that prints n terms of fibonacci series
for (int i = 1; i <= n; i++)
{
// Print current term and update previous terms
if (i > 2)
{
int curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
printf("%d ", curr);
}
else if (i == 1)
printf("%d ", prev2);
else (i == 2)
printf("%d ", prev1);
}
}
}

Input: Enter any number=5


Output:0 1 1 2 3

Page 5 of 19
Experiment No. 3
Aim: To accept an object mass in kilograms and velocity in meters per second and display its
Momentum. Momentum is calculated as p = mv where m is the mass of the object and v is its
velocity.

#include <stdio.h>
void main()
{
double mass;
double distance;
double velocity;
printf(“\n Enter the mass of an object=”);
scanf(“%lf”,&mass);
printf(“\n Enter the distance=”);
scanf(“%lf”,&distance);
for ( int time = 1 ; time <= 5 ; time++ )
{
printf(“\t Time=%lf”, time);
velocity=distance/time;
printf(“\t velocity =%lf”, velocity);
printf(“\t momentum =%lf”, velocity*mass);
printf(“\n “);
}

Inputs: 1) Enter the mass of an object=100


2) Enter the distance=200
Output: Time=1 velocity =200 momentum = 20000

Time=2 velocity =100 momentum = 10000

Time=3 velocity =66.66666 momentum = 6666.666

Time=4 velocity =50 momentum =5000

Time=5 velocity =40 momentum =5000

Page 6 of 19
Experiment No. 4
Aim: To understand the array and implement the algorithms

Program 1: Write a c program to enter the elements in an array and find given element in
array.

#include <stdio.h>

void main()
{
int arr[] = {5, 2, 8, 12, 3};
int n = sizeof(arr) / sizeof(arr[0]); // Calculate the number of elements in the array
int target = 8;
result=-1;
for (int i = 0; i< n; i++)
{
if (arr[i] == target)
{
result=arr[i];
}
}

if (result == -1)
printf("Element not found\n");
else
printf("Element found = %d\n", result);
}

Output: Element found =8

Program 2: Write a c program to enter the elements in an array and find largest and
smallest element from given element in array.

#include<stdio.h>
void main( )
{
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
int mini = arr[0];
int maxi = arr[0];
for (int i = 0; i < n; i++)
{
if (arr[i] < mini)
{
mini = arr[i];
}

Page 7 of 19
else if (arr[i] > maxi)
{
maxi = arr[i];
}
}
printf(“The smallest number is =%d”, mini);
printf(“The largest number is=%d”,maxi);

Output: The smallest number is: 1


The largest number is=5

Program 3: Write a c program to enter the elements in an array and find frequency of given
element in array

#include <stdio.h>
void main()
{
int arr1[100], fr1[100];
int n, i, j, ctr;

// Prompt user for input


printf("\n\nCount frequency of each element of an array:\n");
printf("------------------------------------------------\n");
printf("Input the number of elements to be stored in the array :");
scanf("%d", &n);

// Input elements for the array


printf("Input %d elements in the array :\n", n);
for (i = 0; i < n; i++)
{
printf("element - %d : ", i);
scanf("%d", &arr1[i]);
fr1[i] = -1; // Initialize frequency array with -1
}

// Count the frequency of each element in the array


for (i = 0; i < n; i++)
{
ctr = 1; // Initialize counter for each element
for (j = i + 1; j < n; j++)
{
if (arr1[i] == arr1[j])
{

Page 8 of 19
ctr++; // Increment counter for matching elements
fr1[j] = 0; // Mark the duplicate element's frequency as 0
}
}

// If frequency array value is not marked as 0, set it to the counter


if (fr1[i] != 0)
{
fr1[i] = ctr;
}
}

// Print the frequency of each element in the array


printf("\nThe frequency of all elements of the array : \n");
for (i = 0; i < n; i++)
{
if (fr1[i] != 0)
{
printf("%d occurs %d times\n", arr1[i], fr1[i]);
}
}

Output:
Count frequency of each element of an array:
------------------------------------------------
Input the number of elements to be stored in the array :3
Input 3 elements in the array:
element - 0 : 25
element - 1 : 12
element - 2 : 43

The frequency of all elements of array :


25 occurs 1 times
12 occurs 1 times
43 occurs 1 times

Program 4: Write a c program to enter the elements in an array and find frequency of given
element in array
#include <stdio.h>
void main()
{
int n, i;
float num[100], sum = 0.0, avg;

Page 9 of 19
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);
}

Output: Enter the numbers of elements: 6


1. Enter number: 45.3
2. Enter number: 67.5
3. Enter number: -45.6
4. Enter number: 20.34
5. Enter number: 33
6. Enter number: 45.6
Average = 27.69

Page 10 of 19
Experiment No. 5
Aim: Write a C program for employee salary calculation given, Basic, H.R.A. 20 % of Basic and
D.A. 150 % of Basic.

#include<stdio.h>
void main()
{
float basic_salary, gross_salary, hra,da;

printf("Enter the basic salary: ");


scanf("%f", &basic_salary);
hra = (basic_salary * 20)/100;
da = (basic_salary * 150)/150;
gross_salary = basic_salary + hra + da;
printf("Gross Salary is: %.2f\n", gross_salary);

Input: Enter the basic salary=10000

Output: Gross Salary is:27,000

Page 11 of 19
Experiment No. 6
Aim: 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.

#include<stdio.h>
void main( )
{
int phy_marks, che_marks, math_marks, aggregate;
printf(“\n Enter the marks of Physics=”);
scanf(“%d”, & phy_marks);
printf(“\n Enter the marks of Chemistry=”);
scanf(“%d”, & che_marks);
printf(“\n Enter the marks of Mathematics=”);
scanf(“%d”, & math_marks);
aggregate= (phy_marks+che_marks+math_marks)/3
if(phy_marks>=40 && che_marks>=40&&math_marks>=40)
{
if(aggregate>=75)
printf(“\n Distinction”);
ifelse(aggregate>=60)
printf(“\n First Division”);
ifelse(aggregate>=50)
printf(“\n Second Division”);
ifelse(aggregate>=40)
printf(“\n third division ”);
}
printf(“\n The student is fail”);
}

Inputs: Enter the marks of Physics= 80


Enter the marks of Chemistry=80
Enter the marks of Mathematics=80
Output: Distinction

Page 12 of 19
Experiment No. 7
Aim: To accept two numbers from user Greatest Common Divisor of these two numbers.

#include <stdio.h>
void main()
{
int n1, n2, i, gcd;

printf("Enter two integers: ");


scanf("%d %d", &n1, &n2);

for(i=1; i <= n1 && i <= n2; ++i)


{
// Checks if i is factor of both integers
if(n1%i==0 && n2%i==0)
gcd = i;
}

printf("Greatest Common Divisor of %d and %d is %d", n1, n2, gcd);

Input: Enter two integers: 81 153


GCD = 9

Page 13 of 19
Experiment No. 8
Aim: Write a C program that accepts a string from the user and performs the following string
operations

Program 1: Calculate length of string


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

void main()
{
char s[] = "College of Engineering";

// Find length of string using strlen()


printf("Length of string =%lu", strlen(s));

}
Output: Length of string = 22

Program 2: String reversal

#include <stdio.h>
#include <string.h>
void main()
{
char s[100] = "abcde";
int l = 0;
int r = strlen(s) - 1;
char t;
// Swap characters till l and r meet
while (l < r)
{
// Swap characters
t = s[l];
s[l] = s[r];
s[r] = t;

// Move pointers towards each other


l++;
r--;
}
printf("Reverse =%s", s);
}

Output: Reverse: edcba

Page 14 of 19
Program3: Equality check of two Strings

#include <stdio.h>
#include<string.h>
void main( )
{
char str1[20];
char str2[20];
int value;
printf("Enter the first string : ");
scanf("%s",str1);
printf("Enter the second string : ");
scanf("%s",str2);
// comparing both the strings using strcmp() function
value=strcmp(str1,str2);
if(value==0)
printf("strings are same");
else
printf("Strings are not same");

Input: Enter the first string: College of Engineering


Enter the second string : Engineering college

Output: Strings are not same

Program4: Check palindrome

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

void main( )
{
char string1[20];
int i, length;
int flag = 0;

// Prompt the user for input


printf("Enter a string: ");
scanf("%s", string1);

// Calculate the string length


length = strlen(string1);

Page 15 of 19
// Compare characters from the start and end of the string
// and stop if a mismatch is found or the middle of the string is reached.
for (i = 0; i < length / 2; i++) {
if (string1[i] != string1[length - i - 1]) {
flag = 1;
break;
}
}

// Output the result


if (flag) {
printf("%s is not a palindrome\n", string1);
} else {
printf("%s is a palindrome\n", string1);
}

Input: Enter a string: wow


Output: wow is a palindrome

Program5: Check substring

#include <stdio.h>
#include <string.h>
void main()
{
char txt[] = "College of Engineering";
char pat[] = "of";
printf("%d", findSubstring(txt, pat));
}

// Function to find if pat is a substring of txt


int findSubstring(char *txt, char *pat)
{
int n = strlen(txt);
int m = strlen(pat);

// Iterate through txt


for (int i = 0; i <= n - m; i++)
{
// Check for substring match
int j;
for (j = 0; j < m; j++)
{

Page 16 of 19
// Mismatch found
if (txt[i + j] != pat[j])
{
break;
}
}

// If we completed the inner loop, we found a match


if (j == m) {

// Return starting index


return i;
}
}

// No match found
return -1;
}

Output : 1

Page 17 of 19
Experiment No. 9
Aim: Create Structure EMPLOYEE for storing details, and store the data and update the data in
structure.

// C program to print details of employees using Structure


#include <stdio.h>
struct employee
{
string ename;
int age, phn_no;
int salary;
};

// Function to display details of all employees


void display(struct employee emp[], int n)
{
Printf("Name\tAge\tPhone Number\tSalary\n");
for (int i = 0; i < n; i++) {
printf(“\n %s \t %d \t %d \t %d”,emp[i].ename, emp[i].age, emp[i].phn_no,emp[i].salary”
}
}

// Driver code
int main()
{
int n = 3;
// Array of structure objects
struct employee emp[n];

// Details of employee 1
emp[0].ename = "Chirag";
emp[0].age = 24;
emp[0].phn_no = 1234567788;
emp[0].salary = 20000;

// Details of employee 2
emp[1].ename = "Arnav";
emp[1].age = 31;
emp[1].phn_no = 1234567891;
emp[1].salary = 56000;

// Details of employee 3
emp[2].ename = "Shivam";
emp[2].age = 45;
emp[2].phn_no = 1100661111;
emp[2].salary = 30500;

Page 18 of 19
display(emp, n);
}

Output
Name Age Phone Number Salary
Chirag 24 1234567788 20000
Arnav 31 1234567891 56000
Shivam 45 1100661111 30500

Page 19 of 19

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy