AnshYadavCP
AnshYadavCP
uNiversity of techNology
Q2. Write a C program to enter the radius of circle/sphere and compute its (i)Perimeter (ii) Area and (iii)
Volume
Q6. Given a n integer number write a program that displays the number as
follows
First line : All digits
Second Line :All except first digit
Third line : All except first two digits
.
.Last line : The last digit
Output:
Q7. Write a program to enter an integer and print the sum of the digits in the Integer.
Source code And Output:
Q8. Write a C program to input an investment amount and compute its fixed
deposit commulative return after 10 years at the rate of interest of 7.75%
Source code and Output:
Q9. Write A C program to compute the roots of a quadratic equation.
#include <stdio.h>
int main()
{
char name[20];
int level;
float basic_salary, hra, perks, gross_salary, tax, net_salary;
switch (level) {
case 1:
perks = 7000 + 3000;
if (basic_salary >= 40000 && basic_salary <= 60000) {
tax = 0.10 * basic_salary;
} else {
printf("Invalid basic salary for level 1\n");
return 0;
}
break;
case 2:
perks = 6000 + 2000;
if (basic_salary >= 30000 && basic_salary <= 40000) {
tax = 0.08 * basic_salary;
} else {
printf("Invalid basic salary for level 2\n");
return 0;
}
break;
case 3:
perks = 5000 + 1500;
if (basic_salary >= 20000 && basic_salary <= 30000) {
tax = 0.05 * basic_salary;
} else {
printf("Invalid basic salary for level 3\n");
return 0;
}
break;
case 4:
perks = 5000 + 1500;
if (basic_salary >= 15000 && basic_salary <= 20000) {
tax = 0.0;
} else {
printf("Invalid basic salary for level 4\n");
return 0;
}
break;
default:
printf("Invalid level\n");
return 0;
}
return 0;
}
Output:
11 :Given a text file, which contains few integers and few strings.
WAP in python to select all the prime numbers from the file and
store them in another file.
Solution :
def is_prime(n):
"""Check if a number is prime."""
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
def extract_primes(input_file, output_file):
"""Extract prime numbers from input_file and save to output_file."""
with open(input_file, 'r') as infile:
data = infile.read().split() # Read file and split into words
primes = []
for item in data:
if item.isdigit(): # Check if item is a number
num = int(item)
if is_prime(num): # Check if it's a prime number
primes.append(num)
with open(output_file, 'w') as outfile:
outfile.write('\n'.join(map(str, primes))) # Write primes to output file
# Specify input and output file paths
input_file = 'input.txt'
output_file = 'primes.txt'
# Call the function
extract_primes(input_file, output_file)
print(f"Prime numbers have been written to {output_file}.")
Input :
23 apple 45 67 banana 89 12 10
Output :
23
67
89
12.Given a text file, which contains few lines.WAP in python to
remove all the articles (a,an,the)and store in another file.
Solution :
def remove_articles(input_file, output_file):
"""Remove articles ('a', 'an', 'the') from input_file and save to output_file."""
articles = {"a", "an", "the"}
cleaned_lines = []
for line in lines:
words = line.split() # Split line into words
# Remove articles
filtered_words = [word for word in words if word.lower() not in articles]
cleaned_lines.append(' '.join(filtered_words)) # Join filtered words back
into a line
return line_count
# Specify input and output file paths
input_file = 'input.txt'
output_file = 'output.txt'
# Call the function and get the line count
line_count = process_file(input_file, output_file)
print(f"Processed text has been written to {output_file}.")
print(f"Number of lines in the output file: {line_count}")
Input :
Python is a programming language. C++ is object oriented programming
language.C is procedural programming language
Output :
1. Python is a programming language
2. C++ is object oriented programming language
3. C is procedural programming language
14. Given a text file, which contains few lines.WAP in python to
count number of characters, words and lines.
Solution :
def count_file_contents(input_file):
"""Count characters, words, and lines in a text file."""
with open(input_file, 'r') as infile:
lines = infile.readlines() # Read all lines from the file
# Count lines
line_count = len(lines)
# Count words and characters
word_count = 0
char_count = 0
for line in lines:
words = line.split() # Split line into words
word_count += len(words)
char_count += len(line) # Include spaces and punctuation
return char_count, word_count, line_count
# Specify input file path
input_file = 'input.txt'
# Call the function and get counts
char_count, word_count, line_count = count_file_contents(input_file)
# Display the results
print(f"Number of characters: {char_count}")
print(f"Number of words: {word_count}")
print(f"Number of lines: {line_count}")
Input : This is the first line.
Here is the second line.
And the third one.
Output : Number of characters: 69
Number of words: 14
Number of lines: 3
15 : (File Encryption and Decryption )Write a program that uses a
dictionary to assign “codes” to each letter of the alphabet. For example:
codes = { 'A' : '%', 'a' : '9', 'B' : '@', 'b' : '#', etc...} Using this example, the
letter A would be assigned the symbol %, the letter a would be assigned
the number 9, the letter B would be assigned the symbol @, and so
forth. The program should open a specified text file, read its contents,
and then use the dictionary to write an encrypted version of the file’s
contents to a second file. Each character in the second file should
contain the code for the corresponding character in the first file. Write a
second program that opens an encrypted file and displays its decrypted
contents on the screen.
Solution :
def encrypt_file(input_file, output_file, codes):
"""Encrypts a file using a dictionary of codes."""
with open(input_file, 'r') as infile:
content = infile.read() # Read the file's contents
encrypted_content = ''.join([codes.get(char, char) for char in content])
# Encrypt content
if capital.lower() == 'exit':
break
if capital.lower() == states_and_capitals[state].lower():
print("Correct!")
correct += 1
else:
print(f"Incorrect! The capital of {state} is
{states_and_capitals[state]}.")
incorrect += 1
print("\nQuiz Finished!")
print(f"Correct Answers: {correct}")
print(f"Incorrect Answers: {incorrect}")
# Run the quiz
quiz_user(states_and_capitals)