CSE 1209 Assignment - Categorized Exact Questions
Course: CSE 1209 - Computer Fundamentals and Programming
tags: cse-1209 c fundumental
Basic Arithmetic, Formulas & Sequential Logic
1. Area of a Rectangle
Compute area of a rectangle. Make two integers- length and width. Take the values of them from the user. Show the area of the rectangle.
Steps: 1. Input
lengthandwidth. 2. Calculatearea = length * width. 3. Printarea.
2. Volume of a Cylinder
Compute the volume of a cylinder. Ask the user to input the radius (r). Assign the value 3.14 to a variable called pie. (hint: volume of a cylinder . Take all the variables as float.
Steps: 1. Input
r. 2. Setpie = 3.14. 3. Calculatevolume = (4.0/3.0) * pie * r * r * r. 4. Printvolume.
3. Basic Arithmetic Operations
Modify the above program. a and b will be input by the user. Declare four more variables and take a-b, a*b, and a%b into them. Show all of the four variables as well.
Steps: 1. Input
a,b. 2. Calculatesub = a - b,mul = a * b,div = a / b,mod = a % b. 3. Print all variables.
4. Jovian Years Calculation
Compute Jovian years. Jupiter rounds the sun in 10 hours. It will be interesting to know if the Earth passes X years, how many years Jupiter will pass. Ask the user to put the years the Earth passes. Make appropriate calculations with arithmetic operators and show how many years Jupiter passes by that time.
Steps: 1. Input
X(Earth years). 2. Convert to hours:earth_hours = X * 365 * 24. 3. Calculatejupiter_years = earth_hours / 10. 4. Printjupiter_years.
5. Volume of a Cube
Write a program that shows the volume of a cube. Ask the user to input three dimensions of the cube- length, width and height.
Steps: 1. Input
length,width,height. 2. Calculatevolume = length * width * height. 3. Printvolume.
6. Swap Without Temporary Variable
Swap the values of two numbers without using temporary variable.
Steps: 1. Input
a,b. 2.a = a + b. 3.b = a - b. 4.a = a - b. 5. Printa,b.
15. Calculate Intelligence Level Formula
According to a study, the approximate level of intelligence of a person can be calculated using the following formula: Write a program, which will produce a table of values of i, y and x, where y varies from 1 to 6, and, for each value of y, x varies from 5.5 to 12.5 in steps of 0.5.
Steps: 1. Outer loop
yfrom 1 to 6. 2. Inner loopxfrom 5.5 to 12.5 (step 0.5). 3. Calculatei = 2 + (y + 0.5 * x). 4. Printi,y,xin table format.
30. Quadratic Equation Roots
Write a program to calculate the roots of the quadratic equation where a, b and c are known.
Steps: 1. Input
a,b,c. 2. Calculate discriminantd = (b * b) - (4 * a * c). 3. Calculate roots(-b ± sqrt(d)) / (2 * a). 4. Print roots.
31. Days to Months and Days
Write a program that reads days as input and convert days to months and days.
Steps: 1. Input total
days. 2. Calculatemonths = days / 30(integer division). 3. Calculateremaining_days = days % 30. 4. Print result.
33. Gross and Net Pay Calculation
Write a program that computes an employee’s gross pay and net pay using the formulas
a. Gross = Hours * Rate
b. Net = Gross - Tax
Tax is subtracted from the gross only if an employee earns more than TK.5000. Otherwise deduct no tax. Tax rate is 5% of gross pay.
Steps: 1. Input
hours,rate. 2. Calculategross = hours * rate. 3. Ifgross > 5000,tax = 0.05 * gross, elsetax = 0. 4. Calculatenet = gross - tax. 5. Printgross,net.
46. Sum of Inverses
Find the sum of inverse of 1000 natural numbers.
Steps: 1. Initialize
sum = 0.0. 2. Loopifrom 1 to 1000. 3.sum += 1.0 / i. 4. Printsum.
47. Sum of Factorials
Find the sum of factorials of 10 natural numbers.
Steps: 1. Init
total_sum = 0,fact = 1. 2. Loopifrom 1 to 10. 3.fact *= i. 4.total_sum += fact. 5. Printtotal_sum.
Conditional Logic (If-Else & Switch Cases)
7. Odd or Even
Write a program that takes an integer from the user and can tell if the integer is odd or even. (hint: if you divide an integer with 2 and there is reminder- that integer is odd, else it is even).
Steps: 1. Input
n. 2. Ifn % 2 == 0, print “Even”. 3. Else, print “Odd”.
8. Grade Conversion
The Grade conversion. Ask the user to input his marks. Then convert the marks into letter grades and provide it to the user. The grade conversion chart is given here-
| Marks | Grade | Marks | Grade |
| 160 or more | A+ | 150-159 | A |
| 140-149 | A- | 130-139 | B+ |
| 120-129 | B | 110-119 | B- |
| 100-109 | C+ | 90-99 | C |
| 80-89 | D | Less than 80 | F |
Steps: 1. Input
marks. 2. Use if/else ladder: if>= 160print A+, else if>= 150print A, etc.
9. Driver Insurance Eligibility
A company insures its drivers in the following cases-
-
If the driver is married
-
If the driver is unmarried, male & above 30 years of age
-
If the driver is unmarried, female & above 25 years of age In all other cases, the drivers are not insured. Write a program that takes input marital status, sex and age of the driver. Determine whether the driver is insured or not.
Steps: 1. Input
status,sex,age. 2. Ifstatus == married, insured. 3. Else ifsex == maleANDage > 30, insured. 4. Else ifsex == femaleANDage > 25, insured. 5. Else, not insured.
10. Character Type Identification
Enter a character from the keyboard. Write a program to determine whether the character is a capital letter, a small case letter, a digit or a special symbol. [Hint: you can directly compare a character with an integer value]. Use the following table for the comparison.
| Characters | ASCII Values |
| A-Z | 65-90 |
| a-z | 97-122 |
| 0-9 | 48-57 |
| Special symbols | 0-47, 58-64, 91-96, 123-127 |
Steps: 1. Input
char. 2. Get ASCII code. 3. If65 <= code <= 90, print “Capital”. 4. Else if97 <= code <= 122, print “Small”. 5. Else if48 <= code <= 57, print “Digit”. 6. Else, print “Special”.
16. Menu-Driven: Math Operations
Write a menu driven program which has following options:
- Factorial of a number.
- Prime or not
- Odd or even
Steps: 1. Print menu options. 2. Input
choice. 3. Switch/If onchoice. Case 1: run factorial loop. Case 2: check if divisible by 2..N-1. Case 3: checkN % 2.
18. Menu-Driven: Arithmetic Calculator
Write a menu driven program with switch statement. In this case, user will give two integers and a choice. Choice=1: add, Choice=2: subtract, Choice=3: multiply, Choice=4: divide, Default: show error message
Steps: 1. Input
a,b,choice. 2. Switch(choice): Case 1: printa+b. Case 2: printa-b. Case 3: printa*b. Case 4: printa/b. Default: print “Error”.
32. Leap Year Checker
Write a program that reads a year from the keyboard and determine whether it is leap year or not.
Steps: 1. Input
year. 2. If(year % 4 == 0 AND year % 100 != 0) OR (year % 400 == 0), print “Leap Year”. 3. Else, print “Not Leap Year”.
35. Admission Eligibility Checker
Admission to a professional course is subject to the following conditions: a. Marks in mathematics >= 60, b. Marks in physics >= 50, c. Marks in chemistry >= 40 d. Total in all three subjects >= 200 Or Total in mathematics and physics >= 150
Steps: 1. Input
m,p,c. 2.total = m + p + c,mp_total = m + p. 3. Ifm >= 60 AND p >= 50 AND c >= 40 AND (total >= 200 OR mp_total >= 150), print “Eligible”. Else, “Not Eligible”.
Loops & Iteration
11. Multiplication Table Grid
Write a program that prints the following (Hint: Use nested while repetition)-
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 |
| 3 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 | 30 | 33 | 36 |
| 4 | 8 | 12 | 16 | 20 | 24 | 28 | 32 | 36 | 40 | 44 | 48 |
| 5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 |
| 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | 60 | 66 | 72 |
| 7 | 14 | 21 | 28 | 35 | 42 | 49 | 56 | 63 | 70 | 77 | 84 |
| 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 | 80 | 88 | 96 |
| 9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 | 90 | 99 | 108 |
| 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100 | 110 | 120 |
| 11 | 22 | 33 | 44 | 55 | 66 | 77 | 88 | 99 | 110 | 121 | 132 |
| 12 | 24 | 36 | 48 | 60 | 72 | 84 | 96 | 108 | 120 | 132 | 144 |
Steps: 1. Init
row = 1. 2. Whilerow <= 12: Initcol = 1. 3. Whilecol <= 12: Printrow * col(tabbed).col++. 4. Print newline.row++.
13. Factorial Calculation
Take a number from the user. Calculate its factorial.
Steps: 1. Input
n. 2. Initfact = 1. 3. Loopifrom 1 ton:fact *= i. 4. Printfact.
14. Prime Numbers from 1 to 300
Write a program to print all prime numbers from 1 to 300.
Steps: 1. Loop
ifrom 2 to 300. 2. Initis_prime = true. 3. Inner loopjfrom 2 toi/2: ifi % j == 0,is_prime = false, break. 4. Ifis_prime, printi.
17. Triangular Number
Ask the user to put an integer. Using a do-while loop, find its triangular number.
Steps: 1. Input
n. 2. Initsum = 0,i = n. 3. Do:sum += i,i--. 4. Whilei > 0. 5. Printsum.
Functions
19. Power Function
Write a function power (a, b), to calculate the value of a raised to b.
Steps: 1.
def power(a, b):2. Initres = 1. 3. Loopbtimes:res *= a. 4. Returnres.
20. Greatest Common Divisor (GCD)
Write a function gcd (a,b) that returns the Greatest Common Divisor of a and b.
Steps: 1.
def gcd(a, b):2. Ifb > a, swapaandb. 3. Whileb != 0:rem = a % b,a = b,b = rem. 4. Returna.
21. Least Common Multiple (LCM)
Write a program using appropriate user defined functions that returns the Least Common Multiple of a and b.
Steps: 1.
def lcm(a, b):2. Calculategcd(a, b). 3. Return(a * b) / gcd.
22. Prime Factors
A positive integer is entered through key-board. Write a function to obtain the prime factors of this number.
Steps: 1.
def prime_factors(n):2. Loopi = 2upwards. 3. Whilen % i == 0: printi,n = n / i. 4. Ifn > 1, continue loop.
Arrays & Matrices
23. Histogram from Array
Write a program that will ask 10 values from the user. And for those 10 values, draw the histogram.
Steps: 1. Input array of 10 ints. 2. Loop through array: for each
val, printval, then print*charactervaltimes in a line.
26. Number, Square, Cube Array
Initialize a multidimensional array (10X3) so that the first element of each row contains a number, the second element contains its square and the third element gets its cube.
Steps: 1. Init 10x3 array. 2. Loop
i1 to 10:arr[i-1] = [i, i^2, i^3]. 3. Inputcube. 4. Loop array to findcubein index 2. Print corresponding index 0 and 1.
27. Matrix Multiplication
Make two arrays of dimension 3X3. Think of these two arrays as two matrices. Multiply the matrices.
Steps: 1. Initialize matrices
A,B, and resultC(all 0). 2. Nested loops:i(0-2),j(0-2),k(0-2). 3.C[i][j] += A[i][k] * B[k][j]. 4. PrintC.
28. Element Frequency in Array
Create an integer array of 20 elements. Ask the user to put 20 numbers. Determine their frequency.
Steps: 1. Input 20 ints. 2. Loop to find unique numbers. 3. For each unique number, loop array and count occurrences. 4. Print “number: count times”.
29. Bubble Sort
Write a program that will take an array of values and sort them according to bubble sort procedure.
Steps: 1. Input array. 2. Loop
ifrom 0 toN-1. 3. Loopjfrom 0 toN-i-1. 4. Ifarr[j] > arr[j+1], swap them.
42. Row and Column Sum
Write a program to calculate row sum and column sum.
Steps: 1. Input 2D matrix. 2. For row sum: loop rows, inside loop cols,
sum += cell. 3. For col sum: loop cols, inside loop rows,sum += cell.
48. Find Middle Value
Given 11 values in a list, find the middle value when arranged in order of magnitude.
Steps: 1. Input 11 values. 2. Sort array (ascending). 3. Middle value is at index 5 (the 6th element).
49. Array Subranges
Given 11 values, find the subranges from the lowest value to the middle one and from the middle value to the highest value.
Steps: 1. Input and Sort array. 2.
subrange1 = arr[0] to arr[5]. 3.subrange2 = arr[5] to arr[10].
50. Least Difference in Array
Given 10 values, find and print the two values with the least difference.
Steps: 1. Input and Sort array. 2. Init
min_diff = infinity. 3. Loopi0 to 8: checkdiff = arr[i+1] - arr[i]. Ifdiff < min_diff, save pair. 4. Print saved pair.
51. Biggest Difference in Array
Given 10 values, find and print the two values with the biggest difference.
Steps: 1. Input array. 2. Find maximum value and minimum value in the array. 3. Print min and max.
52. Values Less Than Mean
Given 10 values, print the values, which are less than the mean of all ten values.
Steps: 1. Input 10 values. 2. Calculate sum and
mean = sum/10. 3. Loop array: ifval < mean, printval.
53. Sort Persons Ascending by Age
Input name and age of each of 10 different persons and sort the data into and ascending order of age.
Steps: 1. Create array of structures/tuples
(name, age). 2. Use bubble sort to sort byageascending. 3. Print names and ages.
54. Sort Persons Descending by Age
Input name and age of each of each of 20 different persons and sort the data in order of descending age.
Steps: 1. Same as 53, but sort 20 persons by
agedescending (ifarr[j].age < arr[j+1].age→ swap).
55. Search for Specific Person’s Age
From the list of 10 persons as in problem-51, find the age of a person NANNU and print it.
Steps: 1. Loop through the array of person structures. 2. If
name == "NANNU", printageand break.
56. Find Minimum Age Difference
Find the names of persons with the minimum age difference in Problem-51
Steps: 1. Sort persons by age. 2. Loop pairs (index
iandi+1) to find minimumage[i+1] - age[i]. 3. Print names of that pair.
57. Mean of Values in Range
Given 10 values X, find the mean of the values, which lie between 27 and 53 both inclusive.
Steps: 1. Init
sum = 0,count = 0. 2. Loop 10 inputs: if27 <= val <= 53,sum += val,count++. 3. Calculate and printsum/count.
58. Mean of Bounded Non-integers
Given, 10 values X, find the mean of the values, which non-integers, not exceeding 95.
Steps: 1. Init
sum = 0,count = 0. 2. Loop inputs: ifval <= 95ANDval != int(val)(has decimals),sum += val,count++. 3. Printsum/count.
59. Frequency Distribution Intervals
Given 100 values lying between 0 and 25, sort the values into a frequency distribution with five equal class intervals, the right-hand limit being always inclusive.
Steps: 1. Init counters for 5 bins: 1-5, 6-10, 11-15, 16-20, 21-25. 2. Loop 100 values. 3. Use if/else to increment correct bin based on value. 4. Print bins.
60. Multiply Any Matrices
Write a program which will multiply two matrices (2 dimensional or 3 dimensional).
Steps: 1. Input rows/cols of M1 and M2. 2. If
M1_cols != M2_rows, print Error. 3. Apply standard nested 3-loop matrix multiplication (like Q27) dynamically.
Strings & Character Data Processing
24. Caesar Cipher Encryption
In wars, there are certain codes… Simply turning a letter to its following letter: i→j, a→b, m→n and so on.
Steps: (See 25 below for logic).
25. Implement the Encryption
Write a program that will take such string from the user and will convert that string into such encrypted message.
Steps: 1. Input string. 2. Loop each
char. 3. If alphabet,char = char + 1(handle ‘z'→'a’ wrap). 4. Print encrypted string.
34. Reverse Character Case
Write a program that reads a character from keyboard and then prints it in reverse case is given in.
Steps: 1. Input
char. 2. Ifcharis uppercase, add 32 to ASCII (or usetoLowerCase). 3. Else if lowercase, subtract 32 (or usetoUpperCase).
36. Reverse Digits of Integer
Write a program that reverses the digits of a given positive integer.
Steps: 1. Input
n. 2. Initrev = 0. 3. Whilen > 0:rem = n % 10,rev = (rev * 10) + rem,n = n / 10. 4. Printrev.
37. Toggle String Case
Write a program that read a string from the keyboard and convert all the characters in it into opposite case.
Steps: 1. Input string. 2. Loop string: apply single-character toggle logic (from Q34) to each character.
38. Custom Character Encoding
Write a program that will read in a sequence of characters and write out a sequence of encoded characters in its place. [Hint: If a character is a letter or digit, you have to replace it with the next character in the characters set except that Z will be replaced by A, z by a and 9 by 0. Any character other than a letter or digit will be re placed by a*.]
Steps: 1. Input string. 2. Loop chars. 3. If ‘Z'→'A’, ‘z'→'a’, ‘9'→'0’. 4. Else if letter/digit,
char++. 5. Else,char = '*'.
40. String Concatenation (Manual)
Write a program to concatenate two strings without using built in function of concatenation.
Steps: 1. Input
str1,str2. 2. Find length ofstr1. 3. Loopifrom 0 to len(str2):str1[length + i] = str2[i]. 4. Add null terminator\0.
41. Format Date String
Write a program that reads a data form the keyboard in the form “dd-mm-yy” and displays the date in following form- January 2nd, 1999
Steps: 1. Input string. 2. Extract
dd,mm,yyusing string slicing/splitting. 3. Mapmm(01-12) to month names array. 4. Append ‘st/nd/rd/th’ toddbased on rules. 5. Prepend “19” or “20” toyy.
43. Count Characters in String
Write a program which will read a line of text (with space) from the terminal and display the text with number of characters.
Steps: 1. Input string. 2. Init
count = 0. 3. Loop until character is\0(end of string):count++. 4. Print string andcount.
44. Palindrome Checker
Write a program to determine whether the input string is a palindrome or not.
Steps: 1. Input string. 2. Find
length. 3. Loopifrom 0 tolength/2. 4. Ifstr[i] != str[length-1-i], it’s not a palindrome.
45. Alphabet vs Consonant
Write a program that reads a character as an input and print the character is alphabet or consonant.
Steps: 1. Input
char. 2. If not a letter, error. 3. Ifcharin[A, E, I, O, U, a, e, i, o, u], print “Vowel” (assumed intent of question). 4. Else, print “Consonant”.