0% found this document useful (0 votes)
7 views20 pages

Fundamentals of Programming Languages_Lab R Docx

Uploaded by

patilrohini0224
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views20 pages

Fundamentals of Programming Languages_Lab R Docx

Uploaded by

patilrohini0224
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Fundamentals of Programming Languages L-2 Laboratory

⮚ Program 1 a :
Write a program in C to accept the number and compute a square root, square and cube of the number.
Code :
#include <stdio.h>
#include <math.h>
int main()
{
double num;
printf("Enter a number: "); // Input from user
scanf("%lf", &num); // Calculate square root
double sqrt_result = sqrt(num);
double square_result = num * num;
double cube_result = num * num * num;
printf("Square root of %.2f is %.2f\n", num, sqrt_result);
printf("Square of %.2f is %.2f\n", num, square_result);
printf("Cube of %.2f is %.2f\n", num, cube_result);
return 0;
}

⮚ Program 1 b :
Write a program in C to accept the number and check for the prime number.
Code :
#include <stdio.h>
int main()
{
int num, i;
int is_prime = 1;
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1)
{
is_prime = 0;
}
else
{
for (i = 2; i * i <= num; i++)
{
if (num % i == 0)
{
is_prime = 0;
break;
}
}
}
// Output result
if (is_prime)
{
printf("%d is a prime number\n", num);
Fundamentals of Programming Languages L-3 Laboratory

}
else
{
printf("%d is not a prime number\n", num);
}

return 0;
}

⮚ Program 1 c :
Write a program in C to accept the number and find factorial of number.
Code :
#include <stdio.h>
int main()
{
int num, i;
unsigned long long factorial = 1;
printf("Enter a number: ");
scanf("%d", &num);
for (i = 1; i <= num; i++)
{
factorial *= i;
}
printf("Factorial of %d is %llu\n", num, factorial);
return 0;
}

⮚ Program 1 d :
Write a program in C to accept the number and find prime factors of number.
Code :
#include <stdio.h>
int main()
{
int num, factor;
printf("Enter a number: ");
scanf("%d", &num);
factor = 2;
printf("Prime factors of %d are: ", num);

while (num > 1)


{
if (num % factor == 0)
{
printf("%d ", factor);
num = num / factor;
}
else
{
Fundamentals of Programming Languages L-4 Laboratory

factor++;
}
}
printf("\n");
return 0;
}



Fundamentals of Programming Languages L-5 Laboratory

⮚ Program 2 :
Write a program in C to accept from user the number of fibonacci numbers to be generated and print the fibonacci series.
Code :
#include <stdio.h>
int main()
{
int n, count = 0;
unsigned long first = 0, second = 1, next;
printf("Enter the number (positive integer) of Fibonacci numbers to be generated: ");
scanf("%d", &n);
printf("Fibonacci series: ");
while (count < n)
{
next = first + second;
printf("%d ", first);
first = second;
second = next;
count++;
}
return 0;
}

⮚ Program 3 :
Write a program in C to accept an object's mass in kilograms and velocity in meters per second and display its momentum.
Also, calculate energy as e = mc2 where m is the mass of the object and c is the speed of light.

Code :
#include <stdio.h>
#include <math.h>
int main()
{
double mass, velocity, momentum, energy, c;
printf("Enter the mass of the object in kilograms: ");
scanf("%lf", &mass);
printf("Enter the velocity of the object in meters per second: ");
scanf("%lf", &velocity);
c= 3 * pow(10, 8);
momentum = mass * velocity;
energy = mass * c * c;
printf("The momentum of the object is: %.2lf kg*m/s\n", momentum);
printf("The energy is: %.2e Joule\n", energy);
return 0;
Fundamentals of Programming Languages L-6 Laboratory

⮚ Program 4 :
In array do the following using C programs :
a. Find the given element in the array.
b. Find max element.
c. Find min element.
d. Find the frequency of the given element in the array.
e. Find the average of elements in the array.

⮚ Program 4 a :
Write a C program to find the given element in the array.
Code :
#include <stdio.h>
int main()
{
int n, i, key, found = 0;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the element to search for: ");
scanf("%d", &key);

for (i = 0; i < n; i++)


{
if (arr[i] == key)
{
printf("Element %d found at position %d\n", key, i);
found = 1;
break;
}
}
if (!found)
{
printf("Element %d is not found in the array.\n", key);
}
return 0;
}

⮚ Program 4 b :
Fundamentals of Programming Languages L-7 Laboratory

Write a C program to find the maximum element in the array.


Code :
#include <stdio.h>
int main()
{
int n, i;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
int max = arr[0];
for (i = 1; i < n; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
printf("The maximum element in the array is: %d\n", max);
return 0;
}

⮚ Program 4 c :
Write a C program to find the minimum element in the array.
Code :
#include <stdio.h>
int main()
{
int n, i;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
int min = arr[0];
for (i = 1; i < n; i++)
{
if (arr[i] < min)
{
min = arr[i];
}
}
Fundamentals of Programming Languages L-8 Laboratory

printf("The minimum element in the array is: %d\n", min);


return 0;
}

⮚ Program 4 d :
Write a C program to the frequency of the given element in the array.
Code :
#include <stdio.h>
int main()
{
int n, i, key, frequency = 0;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the element to search for: ");
scanf("%d", &key);

for (i = 0; i < n; i++)


{
if (arr[i] == key)
{
frequency++;
}
}
if (!frequency)
{
printf("Element %d not found in the array.\n", key);
}
else
{
printf("The given element %d is found %d times\n", key, frequency);
}
return 0;
}

⮚ Program 4 e :
Write a C program to the average of elements in the array.
Code :
#include <stdio.h>
int main()
{
int n, i;
float sum = 0.0, average;
Fundamentals of Programming Languages L-9 Laboratory

// Input the number of elements in the array


printf("Enter the number of elements in the array: ");
scanf("%d", &n);
float arr[n];
// Input the elements of the array
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++)
{
scanf("%f", &arr[i]);
sum += arr[i];
}

// Calculate average
average = sum / n;

// Output the average of the elements


printf("The average of the elements in the array is: %.2f\n", average);
return 0;
}



Fundamentals of Programming Languages L - 10 Laboratory

⮚ Program 5 :
Write a C program for employee salary calculation given, basic, H.R.A. 20 % of basic and D.A. 150 % of basic.
Code :
#include <stdio.h>
int main()
{
float basic, hra, da, gross_salary;
// Input the basic salary
printf("Enter the basic salary: ");
scanf("%f", &basic);
// Calculate H.R.A. and D.A.
hra = 0.20 * basic;
da = 1.50 * basic;
// Calculate the gross salary
gross_salary = basic + hra + da;

// Output the gross salary


printf("The gross salary is: %.2f\n", gross_salary);
return 0;
}

⮚ Program 6 :
Write a C program to accept a student's marks for five subjects, and compute his/her result. Student is passing if he/she
scores marks equal to and above 40 in each course. If the student scores aggregate greater than 75 %, then the grade is
distinguished. If an aggregate is 60>= and <75 then the grade of the first division. If an aggregate is 50>= and <60, then the
grade is second division. If an aggregate is 40>= and <50, then the grade is third division.
Code :
#include <stdio.h>
int main()
{
float marks[5];
float total = 0.0, aggregate;
int i;
int pass = 1;
printf("Enter the marks for five subjects out of 100:\n");
for (i = 0; i < 5; i++)
{
printf("Subject %d: ", i + 1);
scanf("%f", &marks[i]);
if (marks[i] < 40)
{
pass = 0; // If marks in any subject are less than 40, set pass to 0
Fundamentals of Programming Languages L - 11 Laboratory

}
total += marks[i];
}
aggregate = total/5;
if (!pass) // Determine result and grade
{
printf("Result: Fail\n");
}
else
{
printf("Result: Pass\n");
printf("Aggregate Percentage: %.2f%%\n", aggregate);
if (aggregate >= 75)
{
printf("Grade: Distinguished\n");
} else if (aggregate >= 60)
{
printf("Grade: First Division\n");
} else if (aggregate >= 50)
{
printf("Grade: Second Division\n");
} else if (aggregate >= 40)
{
printf("Grade: Third Division\n");
}
}
return 0;
}

⮚ Program 7 :
Write a C program to accept two numbers from the user and compute the smallest divisor and greatest common divisor of
these two numbers.
Code :
#include <stdio.h>
// Function to find the smallest divisor greater than 1
int smallest_divisor(int num)
{
for (int i = 2; i <= num; i++)
{
if (num % i == 0)
{
return i;
}
}
return num; // Should never reach here since every
// number is divisible by itself
}

// Function to find the Greatest Common Divisor (GCD)


Fundamentals of Programming Languages L - 12 Laboratory

// using the Euclidean algorithm


int gcd(int a, int b)
{
while (b != 0)
{
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main()
{
int num1, num2;
// Input two numbers from the user
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Calculate the smallest divisors
int smallest_div1 = smallest_divisor(num1);
int smallest_div2 = smallest_divisor(num2);
// Calculate the GCD
int gcd_result = gcd(num1, num2);
// Output the results
printf("Smallest divisor of %d greater than 1 is: %d\n", num1, smallest_div1);
printf("Smallest divisor of %d greater than 1 is: %d\n", num2, smallest_div2);
printf("Greatest Common Divisor of %d and %d is: %d\n", num1, num2, gcd_result);
return 0;
}

⮚ Program 8 a :
Write a C program that accepts a string from the user and performs the following string operations - i. Calculate the
length of the string ii. Check palindrome iii. String reversal.
Code :
#include <stdio.h>
// Function to calculate the length of the string
int string_length(char str[])
{
int length = 0;
while (str[length] != '\0')
{
length++;
}
return length;
}
// Function to reverse the string
void string_reverse(char str[])
{
Fundamentals of Programming Languages L - 13 Laboratory

int length = string_length(str);


for (int i = 0; i < length / 2; i++)
{
char temp = str[i];
str[i] = str[length - i - 1];
str[length - i - 1] = temp;
}
}
// Function to check if the string is a palindrome
int is_palindrome(char str[])
{
int length = string_length(str);
for (int i = 0; i < length / 2; i++)
{
if (str[i] != str[length - i - 1]) {
return 0; // Not a palindrome
}
}
return 1; // Is a palindrome
}

int main()
{
char str[100];
// Input the string from the user
printf("Enter a string: ");
gets(str);
// Calculate length of the string
int length = string_length(str);
printf("Length of the string: %d\n", length);
// Check if the string is a palindrome
if (is_palindrome(str))
{
printf("The string is a palindrome.\n");
} else
{
printf("The string is not a palindrome.\n");
}
// Reverse the string
string_reverse(str);
printf("Reversed string: %s\n", str);
return 0;
}

⮚ Program 8 b :
Write a C program that accepts two strings from the user and performs the following string
operations- i. Equality check of two strings ii. Check substring
Code :
#include <stdio.h>
Fundamentals of Programming Languages L - 14 Laboratory

#include <string.h>
#define MAX_LEN 100

int main()
{
char str1[MAX_LEN], str2[MAX_LEN];
// Input two strings from the user
printf("Enter the first string: ");
fgets(str1, MAX_LEN, stdin);
str1[strcspn(str1, "\n")] = '\0';
printf("Enter the second string: ");
fgets(str2, MAX_LEN, stdin);
str2[strcspn(str2, "\n")] = '\0';
// Equality check
if (strcmp(str1, str2) == 0)
{
printf("The strings are equal.\n");
} else
{
printf("The strings are not equal.\n");
}

// Substring check
if (strstr(str1, str2) != NULL)
{
printf("The second string is a substring of the first string.\n");
}
else
{
printf("The second string is not a substring of the first string.\n");
}
return 0;
}

⮚ Program 8 c :
Write a C program that accepts two strings and checks for their equality without using the standard library function.
Code :
#include<stdio.h>
void main()
{
int i=0, k=0;
char str1[20], str2[20];
printf("Enter the string1: ");
gets(str1);
printf("Enter the string2: ");
gets(str2);
while(str1[i]!='\0')
{
if (str1[i] == str2[i])
{
i++;
}
else
Fundamentals of Programming Languages L - 15 Laboratory

{
k=1;
break;
}

}
if (k ==1 || str1[i] != str2[i])
{
printf("Strings are not equal.");
}
else
{
printf("Strings are equal.");
}
}

⮚ Program 8 d :
Write a C program that accepts two strings and checks whether string2 is a substring of string1 without using the standard
library function.
Code :
#include <stdio.h>
#include<string.h>
// Function to check if str2 is a substring of str1
int is_substring(char str1[], char str2[])
{
int len1 = strlen(str1);
int len2 = strlen(str2);

// If str2 is longer than str1, it can't be a substring


if (len2 > len1)
{
return 0;
}

for (int i = 0; i <= len1 - len2; i++)


{
int j;

for (j = 0; j < len2; j++)


{
if (str1[i + j] != str2[j])
{
break;
}
}

if (j == len2)
{
return 1;
Fundamentals of Programming Languages L - 16 Laboratory

}
}
return 0;
}

int main()
{
char str1[100], str2[100];
// Input strings from the user
printf("Enter the first string: ");
gets(str1);
printf("Enter the second string: ");
gets(str2);
// Check if str2 is a substring of str1
if (is_substring(str1, str2))
{
printf("'%s' is a substring of '%s'\n", str2, str1);
}
else
{
printf("'%s' is not a substring of '%s'\n", str2, str1);
}
return 0;
}

⮚ Program 9 :
Write a C program that creates a structure EMPLOYEE for storing details (Name, Designation, Gender, Date of Joining
and Salary), and store the data and update the data in the structure.
Code :
#include <stdio.h>
#include <string.h>

// Define a structure to store date information


struct Date
{
int day;
int month;
int year;
};

// Define a structure to store employee information


struct EMPLOYEE
{
char name[100];
char designation[100];
char gender[10];
struct Date date_of_joining;
float salary;
};
Fundamentals of Programming Languages L - 17 Laboratory

// Function to update employee details


void update_employee(struct EMPLOYEE *emp)
{
printf("Enter updated details:\n");

printf("Enter Name: ");


scanf(" %[^\n]", emp->name); // Use " %[^\n]" to read a string with spaces

printf("Enter Designation: ");


scanf(" %[^\n]", emp->designation);

printf("Enter Salary: ");


scanf("%f", &emp->salary);
}

// Function to display employee details


void display_employee(struct EMPLOYEE emp)
{
printf("\nEmployee Details:\n");
printf("Name: %s\n", emp.name);
printf("Designation: %s\n", emp.designation);
printf("Gender: %s\n", emp.gender);
printf("Date of Joining: %02d/%02d/%04d\n", emp.date_of_joining.day,
emp.date_of_joining.month, emp.date_of_joining.year);
printf("Salary: %.2f\n", emp.salary);
}
int main()
{
struct EMPLOYEE emp;

// Input initial employee details


printf("Enter initial employee details:\n");

printf("Enter Name: ");


scanf(" %[^\n]", emp.name);

printf("Enter Designation: ");


scanf(" %[^\n]", emp.designation);

printf("Enter Gender: ");


scanf(" %[^\n]", emp.gender);

printf("Enter Date of Joining (dd mm yyyy): ");


scanf("%d %d %d", &emp.date_of_joining.day, &emp.date_of_joining.month,
&emp.date_of_joining.year);

printf("Enter Salary: ");


scanf("%f", &emp.salary);
Fundamentals of Programming Languages L - 18 Laboratory

// Display initial employee details


display_employee(emp);

// Update employee details


update_employee(&emp);

// Display updated employee details


display_employee(emp);

return 0;
}

⮚ Program 10 : Write a C program to create structure STORE to keep track of Products (Product Code, Name and
price). Display menu of all products to users. Generate bills as per order.
Code :
#include <stdio.h>
#include <string.h>

// Define a structure to store product information


struct Product
{
int product_code;
char name[100];
float price;
};
// Define a structure to represent the store
struct STORE
{
struct Product products[100];
int product_count;
};

// Function to add a new product to the store


void add_product(struct STORE *store)
{
if (store->product_count >= 100)
{
printf("Store is full, cannot add more products.\n");
return;
}
struct Product new_product;
printf("Enter Product Code: ");
scanf("%d", &new_product.product_code);
printf("Enter Product Name: ");
scanf(" %[^\n]", new_product.name);
printf("Enter Product Price: ");
scanf("%f", &new_product.price);

store->products[store->product_count] = new_product;
store->product_count++;
Fundamentals of Programming Languages L - 19 Laboratory

printf("Product added successfully!\n");


}

// Function to display all products in the store


void display_products(struct STORE store)
{
if (store.product_count == 0)
{
printf("No products in the store.\n");
return;
}

printf("\nProducts in Store:\n");
for (int i = 0; i < store.product_count; i++)
{
struct Product product = store.products[i];
printf("Product Code: %d\n", product.product_code);
printf("Name: %s\n", product.name);
printf("Price: %.2f\n", product.price);
printf("----------------------\n");
}
}

// Function to generate a bill based on user order


void generate_bill(struct STORE store)
{
int order_code, quantity;
float total = 0.0;

printf("Enter product code to order (0 to finish): ");


while (scanf("%d", &order_code) && order_code != 0)
{
int found = 0;
for (int i = 0; i < store.product_count; i++)
{
if (store.products[i].product_code == order_code)
{
found = 1;
printf("Enter quantity: ");
scanf("%d", &quantity);
total += store.products[i].price * quantity;
printf("Added %d of %s to the bill. Total so far: %.2f\n", quantity, store.products[i].name, total);
break;
}
}
if (!found)
{
printf("Product code %d not found.\n", order_code);
Fundamentals of Programming Languages L - 20 Laboratory

}
printf("Enter product code to order (0 to finish): ");
}
printf("Total bill amount: %.2f\n", total);
}

int main()
{
struct STORE store;
store.product_count = 0;

int choice;

// Add some products to the store for demonstration


strcpy(store.products[0].name, "Apple");
store.products[0].product_code = 1;
store.products[0].price = 25.0;
strcpy(store.products[1].name, "Banana");
store.products[1].product_code = 2;
store.products[1].price = 2.5;
strcpy(store.products[2].name, "Orange");
store.products[2].product_code = 10;
store.products[2].price = 0.8;
store.product_count = 3;

do {
printf("\nStore Management System\n");
printf("1. Display Products\n");
printf("2. Generate Bill\n");
printf("3. Add Product\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{

case 1:
display_products(store);
break;
case 2:
generate_bill(store);
break;
case 3:
add_product(&store);
break;
case 4:
printf("Exiting...\n");
break;
Fundamentals of Programming Languages L - 21 Laboratory

default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 4);

return 0;
}



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