FPL programms

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

EXPERIMENT 1: To accept the number and Compute a) square root of

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


factorial of number f) prime factors.
a)square root of number.

Algorithm:

1. Declare an integer variable to give input for squareroot calculation.

2. Declare a integer variable to store the output value.

3.Perform squareroot operation and store result into another variable.

4. Print the result.

5. Exit the program

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.

2. Declare a integer variable to store the output value.

3. perform square operation and store result into another variable.

4. Print the result.

5. Exit the program

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:

1. Declare an integer variable to give input for square calculation.

2. Declare a integer variable to store the output value.

3. perform square operation and store result into another variable.

4. Print the result.

5. Exit the program


Program:
#include <stdio.h>
int main() {
int n = 5;
printf("Cube of %d = %d", n, (n*n*n));
return 0;
}
Output:
Cube of 5 = 125

d)check for prime

1. Declare an integer variable to give input for checking prime number.

2. Declare a flag variable to set the initial value=0 .

3. if input number is 0 and 1 then flag set to 1 and display it is prime no.

4. if n is divisible by i, then n is not prime.

5 .if n is not divisible by i then n is prime.

6. Display the result.

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:

1. Declare an integer variable to calculate factorial.

2. Declare a fact variable and assign to 1.

3. Use for loop to calculate factorial.

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:

1. Declare an integer variable for calculating prime factors up to that no.

2. Use for loop to calculate prime factors.

3. Use condition as if (num % i == 0) then display prime no.


4. Repeat loops until it reached to the entered value.
5. Display the result.

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

EXPERIMENT 2: To accept from user the number of Fibonacci numbers to be


generated and print the Fibonacci series.

Algorithm:

1. Declare an integer variable n1=0 and n2=1

2. Enter how many elements you want in series.

3. Perform addition of n1 and n2 and make it as next term


4. Repeat the process till the last element in series.
5. Display the result
Program:
#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);//printing 0 and 1
for(i=2;i<number;i++)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}
Output:
Enter the number of elements:7
0112358

EXPERIMENT 3:In array do the following: 1. Find given element in array 2.


Find Max element 3. Find Min element 4. Find frequency of given element in
array 5. Find Average of elements in Array.
3 and 4:Finding Minimum and Maximum element
Algorithm:

1. Declare an Array with size,max,min variable.

2. Enter the size of array and no of elements in array.

3. initialize first element is array as min.


4. Compare every element of array with min and display minimum no.
5. initialize first element is array as max.
6. Compare every element of array with max and display maximum no.

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

1.search the element in array.


Algorithm:

1. Declare an Array with size ,key and element_found 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.
Program:

#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:

1. Declare an Array with size and sum ,avg variable.

2. Enter the size of array and no of elements in array .

3. Perform addition of all elements.


4. Perform average of it.
5. Display sum and average both.

#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

sum is=15The average of given numbers : 5

EXPERIMENT 4:Write a C program for employee salary calculation given,


Basic, H.R.A. 20 % of Basic and D.A. 150 % of Basic
Algorithm:

1. Declare all variables da,hra,gross.

2. Enter the salary from user .

3. Perform calculation on basic salary when HRA is 20% and DA is 150%.


4. Calculate gross salary.
5. Display gross salary.

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:

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.

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()

int n1 = 60, n2 = 48, gcd = 1;


for(int i = 1; i <= n1 || i <= n2; i++) {

if(n1 % i == 0 && n2 % i == 0)

gcd = i;

printf("The GCD of %d and %d is: %d", n1, n2, gcd);

return 0;

Output:

The GCD of 60 and 48 is: 12

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.

a)Calculate length of string

Algorithm:

1.Declare string variable with size.

2.Enter the string.

3.Calculate length of the string using strlen() function.

4.Store length in len integer variable

5.Display length.

Program:

#include<stdio.h>

#include<string.h>

int main() {

char str[100];

int len;

printf("\nEnter the String : ");

scanf("%s",str);

len = strlen(str);
printf("\nLength of the given string is %d", len);

return(0);

Output:

Enter the String : vedant

Length of the given string is 6

b) Equality check of two Strings

Algorithm:

1.Declare two string variables with their size.

2.Compare two strings using strcmp() function and store result in cmp variable.

3.if cmp contains 0 then strings are same.

4.if cmp contains other than 0 strings are not same.

Program:

#include<stdio.h>

#include<string.h>

int main() {

char str1[10],str2[10];

int cmp;

printf("\nEnter the first String : ");

scanf("%s",str1);

printf("\nEnter the second String : ");

scanf("%s",str2);

cmp = strcmp(str1,str2);

if (cmp==0)

printf("\n strings are equal");

else

printf("\n strings are not equal");

return(0);

Output:
Enter the first String : vedant

Enter the second String : sonal

strings are not equal

c)string reverse

Algorithm:

1.Declare string variable with size.

2.Enter the string.

3.Use strrev()function on entered string.

4.Display result.

#include <stdio.h>

#include <string.h>

int main()

char str[10] ="payal";

printf("Original String: %s",str);

printf("Reversed String: %s",strrev(str));

return 0;

Output:

Original String Payal

Reversed String layaP

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:

1.Declare structure variable with all members.

2.Create object of structure.

3.Enter data using structure object and structure member.

4.Display data using structure object and structure member.


#include <stdio.h>
struct Employee
{
char name[50];
char des[50];
char gender[50];
int date;
float salary;
};

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

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