Chapter 1: Problem Solving Aspects : C Programming : FYBCS SEM 1 - BCS Guruji

Ad

Saturday, July 29, 2023

Chapter 1: Problem Solving Aspects : C Programming : FYBCS SEM 1

 Q. Define Algorithm.

- An algorithm is a set of instructions to be executed to perform a specific task. 


Q. Define Flowchart.

- A flowchart is a graphical representation of the algorithm. It uses standard symbols to show sequence of steps involved in completing a specific task.


Q. Differentiate between algorithm and flowchart. [5 mark]

Algorithm vs. Flowchart
Feature Algorithm Flowchart
Definition A stepwise sequence to be followed for implementing a task. A diagrammatic representation of the sequence to be followed.
Time Consumption Comparatively less time consuming. Time-consuming task due to drawing and designing.
Flow Representation Cannot properly demonstrate the flow, especially for looping and branching tasks. Shows proper flow of tasks, even for looping and branching statements.
Representation Presented as a set of logical steps or instructions in plain language or pseudo-code. Displayed as a visual diagram using symbols and arrows to illustrate the sequence of steps.
Application Widely used in programming and problem-solving for designing computational solutions. Applied in various fields beyond programming, such as business processes, system design, troubleshooting, and decision-making.


Q. Differentiate between compiler and interpreter.

Compiler vs. Interpreter

Feature Compiler Interpreter
Definition Translates entire code at once Translates and executes code line-by-line or statement-by-statement
Execution Creates a separate executable file Executes code directly during interpretation process
Process Checks entire code for errors before execution Reports errors as soon as they are encountered
Speed Faster execution as code is pre-translated Execution might be slower due to simultaneous translation and execution
Error Reporting Reports errors after the entire code is checked Reports errors immediately as they are encountered
Examples GCC (C/C++ compiler), Java compiler (javac) Python interpreter, JavaScript interpreter in web browsers



Q. What is a compiler ?

- A compiler is a type of computer program that translates the entire source code written in a high-level programming language into machine code or an intermediate code  that can be executed directly by the computer's hardware. 


Q. FLOWCHART SYMBOLS.


Flowchart Symbols
Name Symbol Use in Flowchart
Oval Used for start or stop of an algorithm
Flow Line Used to denote the direction of flow
Parallelogram Used to perform input or output i.e. take input from the keyboard or give output to the monitor
Rectangle Used to indicate an operation to be performed, e.g., addition
Diamond Used to take a decision; it normally has one input and two outputs based on whether the condition checked is true or false

----

Q. What is global variable?

- The global variables can be accessed by all the functions.


Q. What is local variable?

- The local variable are only local to the corresponding functions.


Q. What are properties of algorithm?

The algorithm should have following properties associated with it:

1) Finiteness: The algorithm should be finite.

2) Range of Input: The range of input for which algorithm works should be compulsorily mentioned.

3) Non-ambiguity: Each statement in algorithm must be clear and precise. 

4) Speed: The algorithm should produce result at a fast speed.

5) Multiplicity: The algorithm can be represented in multiple ways i.e. (English language,flowchart,pseudo-code,etc.)


Q. What are advantages of an algorithm?

1) It follows a clear and specific set of instructions or steps.

2) It can be used independently without relying on a specific programming language.

3) Each step in the algorithm is logically ordered, making it easy to find and fix mistakes.

4) It divides a complex problem into smaller, manageable parts, helping the programmer to easily convert it into a real program.


Q. What are disadvantages of the algorithm?

1) Writing an algorithm can be time-consuming.

2) An algorithm is not an actual computer program; it's more like a plan or idea of how a program should work..

3) It's difficult to show the flow of the program, especially for branching (decision-making) and loops, in an algorithm..


Write an algorithm and draw a flowchart for ‘finding of area of traingle’

-

Q. Algorithm to Find the Area of a Triangle:


Step 1: START

Step 2: PRINT "Enter the length of the base of the triangle (b):"

Step 3: INPUT b

Step 4: PRINT "Enter the height of the triangle (h):"

Step 5: INPUT h

Step 6: area = (1/2) * b * h

Step 7: PRINT "The area of the triangle is:", area

Step 8: STOP



Q. Write an algorithm and flowchart for swap of two numbers

-

Algorithm:


Step 1: START

Step 2: PRINT "Enter the two numbers:"

Step 3: INPUT num1 & num2

Step 4: PRINT "Before swapping: num1 =", num1, "num2 =", num2

Step 5: SET temp = num1

Step 6: SET num1 = num2

Step 7: SET num2 = temp

Step 8: PRINT "After swapping: num1 =", num1, "num2 =", num2

Step 9: STOP


Q. Write algorithm and draw flowchart to find out factorial of a number.

-

Step 1: START

Step 2: PRINT "Enter a number (num):"

Step 3: INPUT num

Step 4: SET factorial = 1

Step 5: SET i = 1

Step 6: WHILE i <= num, DO Steps 7 to 9

Step 7: SET factorial = factorial * i

Step 8: SET i = i + 1

Step 9: END WHILE

Step 10: PRINT "The factorial of", num, "is:", factorial

Step 11: STOP


Q. Write an algorithm and draw a flowcahrt to find the maximum

of 3 no's.

-

Step 1: START

Step 2: PRINT "Enter the three numbers:"

Step 3: INPUT num1, num2 & num3

Step 4: IF num1 >= num2 AND num1 >= num3 THEN

Step 5: SET max = num1

Step 6: ELSE IF num2 >= num1 AND num2 >= num3 THEN

Step 7: SET max = num2

Step 8: ELSE

Step 9: SET max = num3

Step 10: END IF

Step 11: PRINT "The maximum of", num1, ",", num2, "and", num3, "is:", max

Step 12: STOP


Q. Write an algorithm  and draw a flowchart for sum of digit until one digit.  (w.g: 932= 9+3+2=14 1+4=5 = output)

-


Step 1: START

Step 2: PRINT "Enter a number (num):"

Step 3: INPUT num

Step 4: WHILE num >= 10, DO Steps 5 to 7

Step 5: SET sum = 0

Step 6: WHILE num > 0, DO Steps 7 and 8

Step 7: SET sum = sum + (num % 10)

Step 8: SET num = num / 10 (integer division)

Step 9: END WHILE

Step 10: SET num = sum

Step 11: END WHILE

Step 12: PRINT "The sum of digits until one digit for the given number is:", num

Step 13: STOP


Q. Write an Algorithm to display a statement "Hello Friend".
Step 1: START
Step 2: PRINT "Hello Friend".
Step 3: STOP 

Q. Write an algorithm to check if the year entered is leap year or not.
-
Step 1: START
Step 2: PRINT "Enter a year number"
Step 3: INPUT y
Step 4: IF (ymod4=0 AND ymod≠100 ) OR (ymod400=0) THEN
PRINT "It is a leap year"
ELSE PRINT "Not a leap year".
Step 5: STOP.

Q. Write an Algorithm to add two numbers.
-
Step 1: START
Step 2: PRINT "Enter the first number"
Step 3: INPUT num1
Step 4: PRINT "Enter the second number"
Step 5: INPUT num2
Step 6: sum <- num1 + num2
Step 7: PRINT "The sum of", num1, "and", num2, "is:", sum
Step 8: STOP

Q. Write an Algorithm to find the larger of two numbers.
-
Step 1: START
Step 2: PRINT "Enter the first number"
Step 3: INPUT num1
Step 4: PRINT "Enter the second number"
Step 5: INPUT num2
Step 6: IF num1 > num2 THEN
PRINT num1, "is larger"
ELSE
PRINT num2, "is larger"
Step 7: STOP

Q. Write an Algorithm to check if a given number is positive, negative, or zero.
-
Step 1: START
Step 2: PRINT "Enter a number"
Step 3: INPUT num
Step 4: IF num > 0 THEN
PRINT "The number is positive"
ELSE IF num < 0 THEN
PRINT "The number is negative"
ELSE
PRINT "The number is zero"
Step 5: STOP

Q. Write an Algorithm to display your name on the screen.
-
Step 1: START
Step 2: PRINT "Enter your name"
Step 3: INPUT name
Step 4: PRINT "Hello", name
Step 5: STOP

Q. Write an Algorithm to find the area of a rectangle.
-
Step 1: START
Step 2: PRINT "Enter the length of the rectangle"
Step 3: INPUT length
Step 4: PRINT "Enter the width of the rectangle"
Step 5: INPUT width
Step 6: area <- length * width
Step 7: PRINT "The area of the rectangle is:", area
Step 8: STOP


Apologies for any inconvenience. Here are seven easier algorithms with different questions suitable for beginners:

Q. Write an Algorithm to display a greeting message.
-
Step 1: START
Step 2: PRINT "Hello! Welcome to our program."
Step 3: STOP

Q. Write an Algorithm to find the area of a rectangle.
-
Step 1: START
Step 2: PRINT "Enter the length of the rectangle"
Step 3: INPUT length
Step 4: PRINT "Enter the width of the rectangle"
Step 5: INPUT width
Step 6: area <- length * width
Step 7: PRINT "The area of the rectangle is:", area
Step 8: STOP

Q. Write an Algorithm to convert Celsius to Fahrenheit.
-
Step 1: START
Step 2: PRINT "Enter the temperature in Celsius"
Step 3: INPUT celsius
Step 4: fahrenheit <- (celsius * 9/5) + 32
Step 5: PRINT "The temperature in Fahrenheit is:", fahrenheit
Step 6: STOP

Q. Write an Algorithm to check if a number is even or odd.
-
Step 1: START
Step 2: PRINT "Enter a number"
Step 3: INPUT num
Step 4: IF num mod 2 = 0 THEN
Step 5: PRINT num, "is an even number"
Step 6: ELSE
Step 7: PRINT num, "is an odd number"
Step 8: STOP


Q. Write an Algorithm to display a greeting message.
-
Step 1: START
Step 2: PRINT "Hello! Welcome to our program."
Step 3: STOP

Q. Write an Algorithm to find the area of a rectangle.
-
Step 1: START
Step 2: PRINT "Enter the length of the rectangle"
Step 3: INPUT length
Step 4: PRINT "Enter the width of the rectangle"
Step 5: INPUT width
Step 6: area <- length * width
Step 7: PRINT "The area of the rectangle is:", area
Step 8: STOP

Q. Write an Algorithm to convert Celsius to Fahrenheit.
Step 1: START
Step 2: PRINT "Enter the temperature in Celsius"
Step 3: INPUT celsius
Step 4: fahrenheit <- (celsius * 9/5) + 32
Step 5: PRINT "The temperature in Fahrenheit is:", fahrenheit
Step 6: STOP

Q. Write an Algorithm to check if a number is even or odd.
-
Step 1: START
Step 2: PRINT "Enter a number"
Step 3: INPUT num
Step 4: IF num mod 2 = 0 THEN
Step 5: PRINT num, "is an even number"
Step 6: ELSE
Step 7: PRINT num, "is an odd number"
Step 8: STOP

Q5. Write an Algorithm to find the maximum of three numbers.
-
Step 1: START
Step 2: PRINT "Enter the first number"
Step 3: INPUT num1
Step 4: PRINT "Enter the second number"
Step 5: INPUT num2
Step 6: PRINT "Enter the third number"
Step 7: INPUT num3
Step 8: max_num <- num1
Step 9: IF num2 > max_num THEN
Step 10: max_num <- num2
Step 11: END IF
Step 12: IF num3 > max_num THEN
Step 13: max_num <- num3
Step 14: END IF
Step 15: PRINT "The maximum number is:", max_num
Step 16: STOP

Q. Write an Algorithm to calculate the sum of the first "n" natural numbers.
-
Step 1: START
Step 2: PRINT "Enter a positive number 'n'"
Step 3: INPUT n
Step 4: sum <- n * (n + 1) / 2
Step 5: PRINT "The sum of first", n, "natural numbers is:", sum
Step 6: STOP

No comments:

Post a Comment