Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Department of Computer Science and Engineering

Assignment 3
Subject : Programming and Data Structure (CS19001)
——————————————————————————————————————

Instructions:

• Give the name of the programs files as <Your roll> <assignment number> <question number>.c.
For example, 21XX12345 A1 Q1.c for question 1 of assignment 1 of a student with roll 21XX12345.
Follow the file naming convention strictly.

• Apart from the main .c file for each program, you should also upload one additional tempo-
rary .c file for each program (such as when you have finished half of the code). The naming
for the temporary file should be in the format <Your roll> <assignment number> <question
number> temp.c. For example, 21XX12345 A1 Q1 temp.c Make sure that your main code
do not deviate much from its temporary code for each program.

• You should upload the main .c file and the temporary .c file individually to the Moodle course
web page once you finish answering each question. No need to zip the files.

• The deadline to upload the programs is 12:00PM. Beyond that, submission will be closed (No
extensions will be granted).

——————————————————————————————————————

Answer all the questions. [30 + 30 + 40 = 100 Marks]

1. Write a C program that takes an integer input from the user and prints its multiplication table
line by line. You have to go up to 10 times for each number that is taken as input. Your display
must be exactly as shown in the Example below. You can use the ‘x’ character to denote the
multiplication sign while writing the outputs. You have to use while loops only. For each
multiplication output, you also have to print whether the quotient is a perfect square or not.
See the example below.

Example:
Input: 2
Output:
2 x 0 = 0 (not perfect square)
2 x 1 = 2 (not perfect square)
2 x 2 = 4 (perfect square)
2 x 3 = 6 (not perfect square)
2 x 4 = 8 (not perfect square)
2 x 5 = 10 (not perfect square)
2 x 6 = 12 (not perfect square)
2 x 7 = 14 (not perfect square)
2 x 8 = 16 (perfect square)
2 x 9 = 18 (not perfect square)
2 x 10 = 20 (not perfect square) [30]

2. Write a C program that prints the following pattern. Carefully observe the number of rows and
the design. You have to use loops and nested loops, you cannot simply printf() the pattern
directly.

1
[30]

3. Read this question very carefully: Two friends are playing a game in the following way.
Friend 1 always starts and takes as input an integer from the keyboard and it is checked whether
that number is a prime number or not. If yes, then Friend 1 wins and Friend 2 loses. Else,
Friend 2 now enters another number and it is checked whether the number entered is a perfect
number or not. If yes, then Friend 2 is declared the winner, else, Friend 1 is prompted to take
the next input (another number) which is checked for primality. Accordingly, Friend 1 is either
declared the winner or the game goes back again to Friend 2. This process repeats unless any
one friend is declared the winner OR any one friend enter’s ‘N’ (meaning ‘No’) before entering
the number. ‘N’ signifies that the friend is unwilling to continue further, and thus the other
friend is declared the winner. Therefore, before entering the number both the friends will also be
prompted whether they would like to continue or not. See the examples below for more clarity.
Write a C program to emulate such a game between two friends, you can use any loops of your
choice. DO NOT write separate functions for prime number and perfect number, include the
logic within the main loop itself. Also, once the game finishes (i.e either a friend wins or the
opponent inputs ‘N’), the output should display the largest number and the smallest number
that has been taken as input from the start. You must think and incorporate all corner cases.
Prime Number: A prime number is one which is divisible only by itself and unity and has no
other positive (or negative) divisors, like 7, 17, 51, 97 etc.
Perfect number: A perfect number is one which is equal to the sum of its all positive divisors,
excluding itself. Like 6 = 1 + 2 + 3 ;; 28 = 1 + 2 + 4 + 7 + 14 and likewise

Example 1 (Friend 1 always starts):


Friend 1: Do you wish to continue (Y/N)?
Y
Friend 1: Enter integer input
654
The entered integer is not prime, going to Friend 2
Friend 2: Do you wish to continue (Y/N)?
Y
Friend 2: Enter integer input
16
The entered integer is not perfect, going to Friend 2
Friend 1: Do you wish to continue (Y/N)?
Y
Friend 1: Enter integer input
17
The number is prime. Congratulations, Friend 1 wins.
2
The largest number : 654
The smallest number: 16

Example 2 (Friend 1 always starts):


Friend 1: Do you wish to continue (Y/N)?
Y
Friend 1: Enter integer input
50
The entered integer is not prime, going to Friend 2
Friend 2: Do you wish to continue (Y/N)?
Y
Friend 2: Enter integer input
28
The number is perfect. Congratulations, Friend 2 wins.
The largest number : 50
The smallest number: 28

Example 3 (Friend 1 always starts):


Friend 1: Do you wish to continue (Y/N)?
Y
Friend 1: Enter integer input
51
The number is prime. Congratulations, Friend 1 wins.
The largest number : 51
The smallest number: 51

Example 4 (Friend 1 always starts):


Friend 1: Do you wish to continue (Y/N)?
Y
Friend 1: Enter integer input
1002
The entered integer is not prime, going to Friend 2
Friend 2: Do you wish to continue (Y/N)?
N
Friend 2 has aborted. Congratulations, Friend 1 wins.
The largest number : 1002
The smallest number: 1002 [40]

——————————————————————————————————————

You might also like