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 length and width. 2. Calculate area = length * width. 3. Print area.

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. Set pie = 3.14. 3. Calculate volume = (4.0/3.0) * pie * r * r * r. 4. Print volume.

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. Calculate sub = 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. Calculate jupiter_years = earth_hours / 10. 4. Print jupiter_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. Calculate volume = length * width * height. 3. Print volume.

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. Print a, 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 y from 1 to 6. 2. Inner loop x from 5.5 to 12.5 (step 0.5). 3. Calculate i = 2 + (y + 0.5 * x). 4. Print i, y, x in 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 discriminant d = (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. Calculate months = days / 30 (integer division). 3. Calculate remaining_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. Calculate gross = hours * rate. 3. If gross > 5000, tax = 0.05 * gross, else tax = 0. 4. Calculate net = gross - tax. 5. Print gross, net.

46. Sum of Inverses

Find the sum of inverse of 1000 natural numbers.

Steps: 1. Initialize sum = 0.0. 2. Loop i from 1 to 1000. 3. sum += 1.0 / i. 4. Print sum.

47. Sum of Factorials

Find the sum of factorials of 10 natural numbers.

Steps: 1. Init total_sum = 0, fact = 1. 2. Loop i from 1 to 10. 3. fact *= i. 4. total_sum += fact. 5. Print total_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. If n % 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 >= 160 print A+, else if >= 150 print A, etc.

9. Driver Insurance Eligibility

A company insures its drivers in the following cases-

  1. If the driver is married

  2. If the driver is unmarried, male & above 30 years of age

  3. 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. If status == married, insured. 3. Else if sex == male AND age > 30, insured. 4. Else if sex == female AND age > 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. If 65 <= code <= 90, print “Capital”. 4. Else if 97 <= code <= 122, print “Small”. 5. Else if 48 <= code <= 57, print “Digit”. 6. Else, print “Special”.

16. Menu-Driven: Math Operations

Write a menu driven program which has following options:

  1. Factorial of a number.
  2. Prime or not
  3. Odd or even

Steps: 1. Print menu options. 2. Input choice. 3. Switch/If on choice. Case 1: run factorial loop. Case 2: check if divisible by 2..N-1. Case 3: check N % 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: print a+b. Case 2: print a-b. Case 3: print a*b. Case 4: print a/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. If m >= 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. While row <= 12: Init col = 1. 3. While col <= 12: Print row * col (tabbed). col++. 4. Print newline. row++.

13. Factorial Calculation

Take a number from the user. Calculate its factorial.

Steps: 1. Input n. 2. Init fact = 1. 3. Loop i from 1 to n: fact *= i. 4. Print fact.

14. Prime Numbers from 1 to 300

Write a program to print all prime numbers from 1 to 300.

Steps: 1. Loop i from 2 to 300. 2. Init is_prime = true. 3. Inner loop j from 2 to i/2: if i % j == 0, is_prime = false, break. 4. If is_prime, print i.

17. Triangular Number

Ask the user to put an integer. Using a do-while loop, find its triangular number.

Steps: 1. Input n. 2. Init sum = 0, i = n. 3. Do: sum += i, i--. 4. While i > 0. 5. Print sum.

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. Init res = 1. 3. Loop b times: res *= a. 4. Return res.

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. If b > a, swap a and b. 3. While b != 0: rem = a % b, a = b, b = rem. 4. Return a.

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. Calculate gcd(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. Loop i = 2 upwards. 3. While n % i == 0: print i, n = n / i. 4. If n > 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, print val, then print * character val times 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 i 1 to 10: arr[i-1] = [i, i^2, i^3]. 3. Input cube. 4. Loop array to find cube in 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 result C (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. Print C.

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 i from 0 to N-1. 3. Loop j from 0 to N-i-1. 4. If arr[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. Loop i 0 to 8: check diff = arr[i+1] - arr[i]. If diff < 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: if val < mean, print val.

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 by age ascending. 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 age descending (if arr[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", print age and 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 i and i+1) to find minimum age[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: if 27 <= val <= 53, sum += val, count++. 3. Calculate and print sum/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: if val <= 95 AND val != int(val) (has decimals), sum += val, count++. 3. Print sum/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. If char is uppercase, add 32 to ASCII (or use toLowerCase). 3. Else if lowercase, subtract 32 (or use toUpperCase).

36. Reverse Digits of Integer

Write a program that reverses the digits of a given positive integer.

Steps: 1. Input n. 2. Init rev = 0. 3. While n > 0: rem = n % 10, rev = (rev * 10) + rem, n = n / 10. 4. Print rev.

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 of str1. 3. Loop i from 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, yy using string slicing/splitting. 3. Map mm (01-12) to month names array. 4. Append ‘st/nd/rd/th’ to dd based on rules. 5. Prepend “19” or “20” to yy.

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 and count.

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. Loop i from 0 to length/2. 4. If str[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. If char in [A, E, I, O, U, a, e, i, o, u], print “Vowel” (assumed intent of question). 4. Else, print “Consonant”.