FPL programms
FPL programms
FPL programms
Algorithm:
Program:
#include <math.h>
#include <stdio.h>
int main() {
int number, squareRoot;
printf("Enter a number: ");
scanf("%d", &number);
// computing the square root
squareRoot = sqrt(number);
printf("Square root of %.d = %.d", number, squareRoot);
return 0;
}
Output:
Enter a number: 25
Square root of 25 = 5
-------------------------------------------------------------------------
b)square of number.
Algorithm:
1. Declare an integer variable to give input for square calculation.
Program:
#include<stdio.h>
int main()
{
int number, square;
printf("Please Enter any integer Value : ");
scanf("%d", &number);
square = number * number;
printf("square of a given number %d is = %d", number, square);
return 0;
}
Output:
Please Enter any integer Value : 5
square of a given number 5 is = 25
c)cube of a number.
Algorithm:
3. if input number is 0 and 1 then flag set to 1 and display it is prime no.
Program:
#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
// 0 and 1 are not prime numbers
// change flag to 1 for non-prime number
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i) {
// if n is divisible by i, then n is not prime
// change flag to 1 for non-prime number
if (n % i == 0) {
flag = 1;
break;
}
}
// flag is 0 for prime numbers
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
return 0;
}
Output:
Enter a positive integer: 3
3 is a prime number.
e)factorial of a number.
Algorithm:
4. calculate fact=fact * i
5. Display the result.
Program:
#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;
}
Output:
Enter a number: 5
Factorial of 5 is: 120
f)prime factors
Algorithm:
Program:
#include <stdio.h>
int main() {
int num, i;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Factors of %d are: ", num);
for (i = 1; i <= num; ++i) {
if (num % i == 0) {
printf("%d ", i);
}
}
return 0;
}
Output:
Enter a positive integer: 15
Factors of 15 are: 1 3 5 15
Algorithm:
Program:
#include <stdio.h>
int main()
{
int a[10], min,max,size,i;
printf("Enter the number of elements in array\n");
scanf("%d",&size);
printf("Enter %d integers\n", size);
for ( i = 0 ; i< size ; i++ )
scanf("%d", &a[i]);
min = a[0];
for ( i = 0 ; i < size ; i++ )
{
if ( a[i] < min )
{
min = a[i];
}
}
printf("Minimum element %d\n", min);
max=a[0];
for ( i = 1 ; i < size ; i++ )
{
if ( a[i] > max )
{
max = a[i];
}
}
printf("Maximum element %d\n", max);
return 0;
}
Output:
Enter the number of elements in array
3
Enter 3 integers
1
7
9
Minimum element 1
Maximum element 9
#include <stdio.h>
int main()
{
int num;
int i,a[10],key,element_found = 0;
printf("Enter number of elements");
scanf("%d",&num);
printf("\nEnter all the elements");
for (i = 0; i < num; i++)
{
scanf("%d", &a[i]);
}
printf("\nEnter the key element that you would like to be searched: ");
scanf("%d", &key);
/* Linear search starts */
for (i = 0; i < num ; i++)
{
if (key == a[i] )
{
element_found = 1;
break;
}
}
if (element_found == 1)
printf("we got the element");
else
printf("we haven’t got element");
return 0;
}
Algorithm:
#include<stdio.h>
int main() {
int arr[10],i,avg,sum=0,n;
printf("\n Enter size of array");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
sum =sum+arr[i];
}
printf("\n sum is=%d",sum);
avg = sum/3;
printf("The average of given numbers : %d", avg);
return 0;
}
Output:
Enter size of array3
4
5
6
Program:
#include <stdio.h>
int main()
{
float basic, da, hra,gross;
printf("Enter salary");
scanf("%f", &basic);
da = basic * 1.5;
hra = basic * 0.2;
gross = basic + hra + da;
printf("GROSS SALARY OF EMPLOYEE = %f", gross);
return 0;
}
Output:
nter salary
1000
GROSS SALARY OF EMPLOYEE = 2700.000000
EXPERIMENT 5: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.
Algorithm:
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.
Program:
#include <stdio.h>
int main()
{
int num;
printf("Enter your mark ");
scanf("%d",&num);
printf(" You entered %d", num); // printing outputs
if(num >= 75)
{
printf(" You got Distinction"); // printing outputs
}
else if ( num >=60)
{ // Note the space between else & if
printf(" You got First Class");
}
else if ( num >=50)
{
printf(" You got Second Class");
}
else if ( num < 40)
{
printf(" You Failed in this exam");
}
return 0;
}
Output:
Enter your mark 79
You entered 79 You got Distinction
EXPERIMENT 6: To accept two numbers from user and compute smallest divisor and Greatest
Common Divisor of these two numbers.
Program:
#include<stdio.h>
int main()
if(n1 % i == 0 && n2 % i == 0)
gcd = i;
return 0;
Output:
EXPERIMENT 7: 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.
Algorithm:
5.Display length.
Program:
#include<stdio.h>
#include<string.h>
int main() {
char str[100];
int len;
scanf("%s",str);
len = strlen(str);
printf("\nLength of the given string is %d", len);
return(0);
Output:
Algorithm:
2.Compare two strings using strcmp() function and store result in cmp variable.
Program:
#include<stdio.h>
#include<string.h>
int main() {
char str1[10],str2[10];
int cmp;
scanf("%s",str1);
scanf("%s",str2);
cmp = strcmp(str1,str2);
if (cmp==0)
else
return(0);
Output:
Enter the first String : vedant
c)string reverse
Algorithm:
4.Display result.
#include <stdio.h>
#include <string.h>
int main()
return 0;
Output:
EXPERIMENT 8: Create Structure EMPLOYEE for storing details (Name, Designation, gender, Date
of Joining and Salary), and store the data and update the data in structure
Algorithm:
int main() {
struct Employee emp;
printf("Enter Employee Name: ");
scanf("%s", emp.name);
printf("Enter Employee designation: ");
scanf("%s", emp.des);
printf("Enter Employee gender: ");
scanf("%s", emp.gender);
printf("Enter Employee date: ");
scanf("%d", &emp.date);
printf("Enter Employee salary: ");
scanf("%f", &emp.salary);
printf("\nEmployee Details:\n");
printf("gender: %s\n", emp.gender);
printf("Name: %s\n", emp.name);
printf("Name: %s\n", emp.des);
printf("Name: %d\n", emp.date);
printf("Salary: %.2f\n", emp.salary);
return 0;
}
Output:
Enter Employee Name: sss
Enter Employee designation: manager
Enter Employee gender: female
Enter Employee date: 21061984
Enter Employee salary: 30000
Employee Details:
gender: female
Name: sss
Name: manager
Name: 21061984
Salary: 30000.00