0% found this document useful (0 votes)
11 views

Hasnain

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Hasnain

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

/*15.

Develop a C program to display Name, Roll Number, Date of Birth and Date of
Admission details of a student read from the keyboard where the date of birth and date of
admission further consists of three members such as day, month, and year in a separate
structure. Implement using a C structure.

Name: Mohd Hasnain Khan


Roll No.: 43
Section: C1
Course: B.Tech
Branch: CSE

*/

#include <stdio.h>
struct Date {
int day;
int month;
int year;
};
struct Student {
char name[50];
int rollNumber;
struct Date dob; // Date of Birth
struct Date doa; // Date of Admission
};

int main() {
struct Student student;
printf("Enter Name: ");
scanf("%s", student.name);

printf("Enter Roll Number: ");


scanf("%d", &student.rollNumber);

printf("Enter Date of Birth (DD MM YYYY): ");


scanf("%d %d %d", &student.dob.day, &student.dob.month, &student.dob.year);

printf("Enter Date of Admission (DD MM YYYY): ");


scanf("%d %d %d", &student.doa.day, &student.doa.month, &student.doa.year);

printf("\nStudent Details:\n");
printf("Name: %s\n", student.name);
printf("Roll Number: %d\n", student.rollNumber);
printf("Date of Birth: %02d/%02d/%d\n", student.dob.day, student.dob.month,
student.dob.year);
printf("Date of Admission: %02d/%02d/%d\n", student.doa.day, student.doa.month,
student.doa.year);

return 0;
}
Output 1:
Enter Name: Mithun
Enter Roll Number: 69
Enter Date of Birth (DD MM YYYY): 12 03 2005
Enter Date of Admission (DD MM YYYY): 12 08 2023

Student Details:
Name: Mithun
Roll Number: 69
Date of Birth: 12/03/2005
Date of Admission: 12/08/2023

Output 2:
Enter Name: Puneet
Enter Roll Number: 35
Enter Date of Birth (DD MM YYYY): 21 06 1900
Enter Date of Admission (DD MM YYYY): 12 03 2304

Student Details:
Name: Puneet
Roll Number: 35
Date of Birth: 21/06/1900
Date of Admission: 12/03/2304
/*16. Develop a C program to find total and average sales of 'N' employees by reading the
details such as empcode, name, and sales using array of structures.

Name: Mohd Hasnain Khan


Roll No.: 43
Section: C1
Course: B.Tech
Branch: CSE

*/
#include <stdio.h>
struct Employee {
int empCode;
char name[50];
float sales;
};

int main() {
int numEmployees;
float totalSales = 0, averageSales;

printf("Enter the number of employees: ");


scanf("%d", &numEmployees);

struct Employee employees[numEmployees];


for (int i = 0; i < numEmployees; i++) {
printf("\nEnter details for Employee %d:\n", i + 1);
printf("Employee Code: ");
scanf("%d", &employees[i].empCode);

printf("Name: ");
scanf("%s", employees[i].name);

printf("Sales: ");
scanf("%f", &employees[i].sales);

totalSales += employees[i].sales;
}
averageSales = totalSales / numEmployees;
printf("\nTotal Sales: %.2f\n", totalSales);
printf("Average Sales: %.2f\n", averageSales);

return 0;
}
Output 1:
Enter the number of employees: 2

Enter details for Employee 1:


Employee Code: 123123
Name: Harsh
Sales: 1234

Enter details for Employee 2:


Employee Code: vinay
Name: Sales: 1000

Total Sales: 2234.00


Average Sales: 1117.00

Output 2:
Enter the number of employees: 3

Enter details for Employee 1:


Employee Code: 132312
Name: Sumit
Sales: 2321

Enter details for Employee 2:


Employee Code: 21324
Name: Mayank
Sales: 2131

Enter details for Employee 3:


Employee Code: Suraj
Name: Sales: 1313

Total Sales: 5765.00


Average Sales: 1921.67
/*17. Develop a C program to read the attributes of an item from the user such as ItemCode,
ItemName, Quantity and Rate. Implement a C program using a structure to find the total cost
of the inventory of storing N items in the stock.

Name: Mohd Hasnain Khan


Roll No.: 43
Section: C1
Course: B.Tech
Branch: CSE

*/
#include <stdio.h>

// Structure to store item details


struct Item {
int itemCode;
char itemName[50];
int quantity;
float rate;
};

int main() {
int numItems;
float totalCost = 0;

printf("Enter the number of items: ");


scanf("%d", &numItems);

struct Item items[numItems];


for (int i = 0; i < numItems; i++) {
printf("\nEnter details for Item %d:\n", i + 1);
printf("Item Code: ");
scanf("%d", &items[i].itemCode);

printf("Item Name: ");


scanf("%s", items[i].itemName);

printf("Quantity: ");
scanf("%d", &items[i].quantity);

printf("Rate: ");
scanf("%f", &items[i].rate);

totalCost += items[i].quantity * items[i].rate;


}

printf("\nTotal Inventory Cost: %.2f\n", totalCost);

return 0;
}
Output 1:
Enter the number of items: 2

Enter details for Item 1:


Item Code: 12313
Item Name: biscuits
Quantity: 100
Rate: 10.00

Enter details for Item 2:


Item Code: Namkeen
Item Name: Quantity: 96
Rate: 9.5

Total Inventory Cost: 1912.00

Output 2:
Enter the number of items: 3

Enter details for Item 1:


Item Code: Sugar
Item Name: Quantity: 100
Rate: 38.00

Enter details for Item 2:


Item Code: Rice
Item Name: Quantity: 100
Rate: 50

Enter details for Item 3:


Item Code: Jaggery
Item Name: Quantity: 20
Rate: 45.5

Total Inventory Cost: 9710.00


/* 18. Develop a program in C to read a structure in the main program of an Employee that
contains Name, EmpCode and Salary as the members. Write a function display the details of
the employee

Name: Mohd Hasnain Khan


Roll No.: 43
Section: C1
Course: B.Tech
Branch: CSE

*/
#include <stdio.h>

// Structure to store employee details


struct Employee {
char name[50];
char empCode[20];
float salary;
};

void displayEmployeeDetails(struct Employee emp) {


printf("ABC Corporation.\n");
printf("Name: %s\n", emp.name);
printf("EmpCode: %s\n", emp.empCode);
printf("Nett. Salary: Rs. %.2f\n", emp.salary);
}

int main() {
struct Employee emp;

printf("Enter Name: ");


scanf("%s", emp.name);

printf("Enter EmpCode: ");


scanf("%s", emp.empCode);

printf("Enter Salary: ");


scanf("%f", &emp.salary);
displayEmployeeDetails(emp);

return 0;
}
Output 1:
Enter Name: Sumittar
Enter EmpCode: 1212
Enter Salary: 908800
Enter DOB: 14 02 1995

ABC Corporation.
Name: sumittar
EmpCode: 1212
DOB : 14th Feb 1995
Nett. Salary: Rs. 908800.00

Output 2:
Enter Name: Karan
Enter EmpCode: 4543
Enter Salary: 340000
Enter DOB: 12 12 1990

ABC Corporation.
Name: Karan
EmpCode: 4543
DOB: 12th Dec 1990
Nett. Salary: Rs. 340000.00
/* 19. Write a C program to create a file ThreeParas.txt and write any three paragraphs of
textto it. Display the content of the file and the count of frequency of consonants and vowels
present in it to the console.
Note: You may assume a single paragraph in the input stream until you encounter a
newline character ( \n).

Name: Mohd Hasnain Khan


Roll No.: 43
Section: C1
Course: B.Tech
Branch: CSE

*/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

void countVowelsAndConsonants(FILE *file) {


char ch;
int vowels = 0, consonants = 0;

while ((ch = fgetc(file)) != EOF) {


if (isalpha(ch)) {
ch = tolower(ch);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
vowels++;
else
consonants++;
}
}

printf("Vowels: %d\n", vowels);


printf("Consonants: %d\n", consonants);
}

int main() {
FILE *fp;
char text[] = "This is the first paragraph.\nThis is the second paragraph.\nAnd this is the
third paragraph.\n";

fp = fopen("ThreeParas.txt", "w");
if (fp == NULL) {
printf("Error opening file.\n");
return 1;
}
fprintf(fp, "%s", text);
fclose(fp);
fp = fopen("ThreeParas.txt", "r");
if (fp == NULL) {
printf("Error opening file.\n");
return 1;
}
printf("Content of the file:\n");
char c;
while ((c = fgetc(fp)) != EOF) {
putchar(c);
}
fclose(fp);

fp = fopen("ThreeParas.txt", "r");
if (fp == NULL) {
printf("Error opening file.\n");
return 1;
}
printf("\n\nFrequency of vowels and consonants:\n");
countVowelsAndConsonants(fp);
fclose(fp);

return 0;
}

Output1:
Content of the file:
This is the first paragraph.
This is the second paragraph.
And this is the third paragraph.

Frequency of vowels and consonants:


Vowels: 23
Consonants: 50
/* 20. Develop a C program to create a file and copy its contents to another file such that
there is no space between any of the words in the copied file. Display the content of the
newly copied file to the output screen.

Name: Mohd Hasnain Khan


Roll No.: 43
Section: C1
Course: B.Tech
Branch: CSE

*/
#include <stdio.h>
#include <stdlib.h>

int main() {
FILE *sourceFile, *destinationFile;
char sourceFileName[100], destinationFileName[100];
char ch;

printf("Enter the source file name: ");


scanf("%s", sourceFileName);

sourceFile = fopen(sourceFileName, "r");


if (sourceFile == NULL) {
printf("Error opening source file.\n");
return 1;
}

printf("Enter the destination file name: ");


scanf("%s", destinationFileName);

destinationFile = fopen(destinationFileName, "w");


if (destinationFile == NULL) {
printf("Error opening destination file.\n");
fclose(sourceFile);
return 1;
}

while ((ch = fgetc(sourceFile)) != EOF) {


if (ch != ' ' && ch != '\n' && ch != '\t') {
fputc(ch, destinationFile);
}
}

printf("Contents copied successfully without spaces.\n");


fclose(sourceFile);
fclose(destinationFile);
printf("\nContents of %s:\n", destinationFileName);
destinationFile = fopen(destinationFileName, "r");
if (destinationFile == NULL) {
printf("Error opening destination file.\n");
return 1;
}

while ((ch = fgetc(destinationFile)) != EOF) {


putchar(ch);
}

fclose(destinationFile);

return 0;
}

Output :
Enter the source file name: source.txt
Enter the destination file name: copy.txt
Contents copied successfully without spaces.

Contents of copy.txt:
Hi,mynameisXYZ
/*21. Implement a C program to read 20 integers through command line arguments and store
them into a file Numbers.txt in the current working path. Then read the file Numbers.txt and
separate them into two different files odd.txt and even.txt such that odd numbers are copied
to odd.txt and even numbers are copied to even.txt files respectively in the same current
working path.
Name: Mohd Hasnain Khan
Roll No.: 43
Section: C1
Course: B.Tech
Branch: CSE

*/

#include <stdio.h>
#include <stdlib.h>

void main()
{
FILE *numbersFile, *oddFile, *evenFile;
int i = 0, odd, even, integer;

numbersFile = fopen("C:\\Users\\Divya\\OneDrive\\C Projects\\college\\Sem 2\\Lab


Journal\\Numbers.txt", "w");
if (numbersFile == NULL)
{
printf("File not opened....");
}
else
{
// Write the numbers to Numbers.txt
printf("Enetr the numbers: ");
for (int i = 0; i < 20; i++)
{
scanf("%d", &integer);
fprintf(numbersFile, "%d\n", integer);
}
fclose(numbersFile);
}

numbersFile = fopen("C:\\Users\\Divya\\OneDrive\\C Projects\\college\\Sem 2\\Lab


Journal\\Numbers.txt", "r");
if (numbersFile == NULL)
{
printf("File not opened....");
}
else
{
oddFile = fopen("C:\\Users\\Divya\\OneDrive\\C Projects\\college\\Sem 2\\Lab Journal\\
odd.txt", "w+");
evenFile = fopen("C:\\Users\\Divya\\OneDrive\\C Projects\\college\\Sem 2\\Lab Journal\\
even.txt", "w+");
// Read the numbers from Numbers.txt
printf("File: Numbers.txt: ");
while (fscanf(numbersFile, "%d", &integer) != EOF)
{
printf("%d ", integer);
// Write the numbers to odd.txt and even.txt
if (integer % 2 == 0)
{
fprintf(evenFile, "%d\n", integer);
}
else
{
fprintf(oddFile, "%d\n", integer);
}
}
// Reset the file pointer to the beginning of the file
fseek(evenFile, 0, SEEK_SET);
fseek(oddFile, 0, SEEK_SET);
// Read the numbers from odd.txt
printf("\nFile: odd.txt: ");
while (fscanf(oddFile, "%d", &odd) != EOF)
{
printf("%d ", odd);
}
// Read the numbers from even.txt
printf("\nFile: even.txt: ");
while (fscanf(evenFile, "%d", &even) != EOF)
{
printf("%d ", even);
}

fclose(oddFile);
fclose(evenFile);
fclose(numbersFile);
}
}

SAMPLE INPUT:
Enter the numbers: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

OUTPUT:
File: Numbers.txt: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
File: odd.txt: 1 3 5 7 9 11 13 15 17 19
File: even.txt: 2 4 6 8 10 12 14 16 18 20

SAMPLE INPUT:
Enter the numbers: 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200

OUTPUT:
File: Numbers.txt: 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200
File: odd.txt:
File: even.txt: 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200
/* 22. Design a function in python that returns the real quotient and the remainder to the
calling program. Display the quotient and the remainder to the console.
Sample Input:
Enter two numbers: 628 25
Sample Output:
The quotient is: 25.12
The remainder is: 3

Name: Mohd Hasnain Khan


Roll No.: 43
Section: C1
Course: B.Tech
Branch: CSE

*/

def calculate_quotient_and_remainder(dividend, divisor) :


quotient = dividend / divisor
remainder = dividend % divisor
return quotient,remainder

num1,num2 = map(int, input("Enter two numbers: ").split())

quotient,remainder = calculate_quotient_and_remainder(num1, num2)

print("The quotient is:", quotient)


print("The remainder is:", remainder)

Output:
Enter two numbers: 1088 212
The quotient is: 5.13
The remainder is: 28

Output 2:
Enter two numbers: 1232 12
The quotient is: 102.66
The remainder is: 8
/* 23. Design a function in python that returns the concatenated string and its length to the
calling program. Display the joined string and its length to the console.
Sample Input:
Enter the first string: Tic Tac
Enter the second string: Toe
Sample Output:
The joined string: Tic Tac Toe
Length of new string: 11

Name: Mohd Hasnain Khan


Roll No.: 43
Section: C1
Course: B.Tech
Branch: CSE

*/
def concatenate_strings(str1, str2):
joined_string = str1 + " " + str2
length = len(joined_string)
return joined_string, length

# Sample Input
str1 = input("Enter the first string: ")
str2 = input("Enter the second string: ")

# Concatenate strings and get length


joined_string, length = concatenate_strings(str1, str2)

# Display results
print("The joined string:", joined_string)
print("Length of new string:", length)

Output 1:
Enter the first string: tic tac
Enter the second string: toe
The joined string: tic tac toe
Length of new string: 11

Output 2:
Enter the first string: akkad bakkad bambe
Enter the second string: bo
The joined string: akkad bakkad bambe bo
Length of new string: 21
/*24. Develop a program in python to read an entire text file and display the number of special
characters present in it to the screen.
Name: Mohd Hasnain Khan
Roll No.: 43
Section: C1
Course: B.Tech
Branch: CSE

*/

# Open the file in read mode


with open(C:\\Users\\Hasnain\\OneDrive\\C Projects\\college\\Sem 2\\Lab Journal\\file2.txt,
"r") as file:
# Read the entire file
content = file.read()

special_characters = "!@#$%^&*()_+[]{}|;:,.<>?`~"
special_char_count = 0

for char in content:


# Check if the character is a special character
if char in special_characters:
special_char_count += 1

print("Number of special characters: ", special_char_count)


SAMPLE INPUT: ThreeParas.txt
Content of the file:
Amidst the bustling cityscape, a symphony of honking horns and hurried footsteps echoed
through the narrow alleys. Towering skyscrapers reached for the heavens, their glass facades
reflecting the frenetic energy below. !@#$

OUTPUT:
Number of special characters: 8

SAMPLE INPUT: file2.txt


Content of the file:
In the tranquil embrace of dawn, the sleepy town stirred to life, its cobblestone streets glistening
with the morning dew. Fragrant tendrils of steam rose from the local bakery, enticing passersby
with promises of freshly baked delights.

OUTPUT:
Number of special characters: 5
/*25. Write a python program to find all the occurrence of words starting with the letters a, c,
d, g, p and s in a file read from the user to the screen. Assume articles or single letters also as
words. Ignore the case.

Name: Mohd Hasnain Khan


Roll No.: 43
Section: C1
Course: B.Tech
Branch: CSE

*/

def find_words_with_starting_letters(file_path):
# Define the starting letters
starting_letters = {'a', 'c', 'd', 'g', 'p', 's'}

try:
# Open the file in read mode
with open(file_path, 'r') as file:
# Read the file and split its content into words
words = file.read().split()

# Filter out the words starting with specified letters


filtered_words = [word.strip(",.?!") for word in words if
word.lower().startswith(tuple(starting_letters))]

# Print the filtered words


print(" ".join(filtered_words))

except FileNotFoundError:
print("File not found. Please check the file path.")
# Example usage:
file_path = input("Enter the path of the text file: ")
find_words_with_starting_letters(file_path)

SAMPLE INPUT: ThreeParas.txt


Content of the file:
Amidst the bustling cityscape, a symphony of honking horns and hurried footsteps echoed
through the narrow alleys. Towering skyscrapers reached for the heavens, their glass facades
reflecting the frenetic energy below. !@#$

OUTPUT:
Amidst cityscape a symphony and alleys skyscrapers glass

SAMPLE INPUT: file2.txt


Content of the file:
In the tranquil embrace of dawn, the sleepy town stirred to life, its cobblestone streets glistening
with the morning dew. Fragrant tendrils of steam rose from the local bakery, enticing passersby
with promises of freshly baked delights.

OUTPUT:
dawn sleepy stirred cobblestone streets glistening dew steam passersby promises delights

You might also like