Practicals of FPL
Practicals of FPL
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;
}
Page 3 of 19
}
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);
}
}
}
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 “);
}
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);
}
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);
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;
Page 8 of 19
ctr++; // Increment counter for matching elements
fr1[j] = 0; // Mark the duplicate element's frequency as 0
}
}
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
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);
avg = sum / n;
printf("Average = %.2f", avg);
}
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;
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”);
}
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;
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
void main()
{
char s[] = "College of Engineering";
}
Output: Length of string = 22
#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;
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");
#include <stdio.h>
#include <string.h>
void main( )
{
char string1[20];
int i, length;
int flag = 0;
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;
}
}
#include <stdio.h>
#include <string.h>
void main()
{
char txt[] = "College of Engineering";
char pat[] = "of";
printf("%d", findSubstring(txt, pat));
}
Page 16 of 19
// Mismatch found
if (txt[i + j] != pat[j])
{
break;
}
}
// 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.
// 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