Fundamentals of Programming Languages_Lab R Docx
Fundamentals of Programming Languages_Lab R Docx
⮚ 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);
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);
⮚ Program 4 b :
Fundamentals of Programming Languages L-7 Laboratory
⮚ 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
⮚ 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);
⮚ 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
// Calculate average
average = sum / n;
⮚
⮚
⮚
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;
⮚ 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
}
⮚ 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 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 (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>
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>
store->products[store->product_count] = new_product;
store->product_count++;
Fundamentals of Programming Languages L - 19 Laboratory
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");
}
}
}
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;
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;
}