0% found this document useful (0 votes)
2 views

algorithm and practice test

The document explains algorithms and flowcharts, defining an algorithm as a step-by-step instruction set for problem-solving, while a flowchart visually represents the logic of an algorithm. It outlines the characteristics, symbols, and differences between algorithms and flowcharts, emphasizing clarity, logical flow, and flexibility. Additionally, it provides practice test questions and programming exercises in C to reinforce the concepts discussed.

Uploaded by

agamerz341
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
2 views

algorithm and practice test

The document explains algorithms and flowcharts, defining an algorithm as a step-by-step instruction set for problem-solving, while a flowchart visually represents the logic of an algorithm. It outlines the characteristics, symbols, and differences between algorithms and flowcharts, emphasizing clarity, logical flow, and flexibility. Additionally, it provides practice test questions and programming exercises in C to reinforce the concepts discussed.

Uploaded by

agamerz341
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 6
gorithm and Flowchart What is Algorithm? '* Aset of step-by-step instructions to solve a problem. 1es the logic of the solu 1n before writing the actual € program. ple language or pseudocode of an Algorithm 1. Finiteness: An algorithm must always terminate after a finite number of steps. It cannot run indefinitely. 2. Definiteness: Each step of the algorithm must be clear and unambiguous. There should be no confusion in the instructions, 3. Input: An algorithm must take zero, one, or more inputs (data) to work with. 4. Output: An algorithm must produce at least one output or result that solves the given problem. 5. Effectiveness: Each step of the algorithm should be simple and practical enough to be performed within a finite amount of time and effort. 6. Generality: An algorithm should solve not just one problem but a class of problems with similar inputs. 7. Logical Sequence: The steps must follow a logical order, ensuring the process flows from start to finish correctly. Example: Write an algorithm that takes two numbers as input from the user, calculates their sum, and displays the result. Step 1: Start Step 2: Declare variables: num, num2, and sum as integers. Step 3; Print a message: "Enter the first number:" and take input for numd. Step 4: Print a message: “Enter the second number:” and take input for num2. Step 5: Calculate the sum: sum = num1 + num2, Step 6: Print the result: "The sum is:" followed by the walue of sum. Step 7: End What is Flowchart? + Adiagrammatic representation of an algorithm using symbols, '* Itshows the flow of logic for solving a problem visually. ‘+ Helps to understand the logic before writing the C program. Characteristics of Flowchart '* Clarity: A flowchart should be easy to understand, with symbols and steps clearly representing the process. ‘+ Logical Flow: The steps in a flowchart should follow a logical sequence from start to finish. ‘+ Simplification: A flowchart simplifies complex processes by breaking them into smaller, manageable steps, + Direction of Flow: Arrows indicate the flow of the process, showing the sequence of steps. '* Flexibility: A good flowchart can easily be updated or modified as the process evolves. ‘+ Specific Purpose: Each flowchart is created to sofve ar represent a specific problem or process. © Completeness: A flowchart should represent the entire process, from start to finish, without missing any critical steps. Symbols of Flowchart symbol Name Function > Start/End ‘An oval represents a start or end point. Aline is a connector that shows —_ Arrows relationships between the representative shapes y | epee | Apteoteceran apceare meer a onect [| on pene erence toraee <> Decision A diamond indicated a decision. Example: Draw The Flowchart of that takes two numbers as input from the user, calculates their sum, and displays the result, eae Pere) Difference b/w Algorithm and Flowchart Aspect ithm Flowchart Definition ‘Astep-by-step set of instructions | A diagrammatic representation of to solve a problem. an algorithm, Representation ‘Written in simple language or Represented using symbols ike pseudocode. ovals (Start/End), rectangles (Process), and diamonds {Decision} Focus Focuses on the logic of the Focuses on the visual Process, representation of the process. | Case of Understanding May be harder for beginners to understand. Easier to understand because of its | visual nature: Symbols No specific symbols; uses plain Oval: Start/End text. Rectangle: Process Diamond: Decision Arrow: Flow Direction Usage Best for documenting and writing Bast for explaining and presenting detailed instructions. the process visually, Flexibility Easier to modify or update steps. | Updating a flowchart may require redrawing. Application Used for planning and Used for explaining the logic and implementing solutions in code. flow of 2 process. Practice Test 1) Write the algorithm and flowchart that check the number is even or odd. 2) Write the algorithm and flowchart find the greater of two numbers. 3) Write the algorithm and flowchart that find the number is positive or negative. 4) Write the algorithm and flowchart that find the factorial of the number. Practice Set C Programming Test Questions 1, Program to check if a Number is Prime Write a € program that checks whether a given number is a prime number or not. ‘The program should take an integer input from the user and determine if itis a prime number. Example Input: 5 Example Output: 5 is a prime number. 2, Program to Reverse a Number Write aC program to reverse the digits of a given number: The program should take an integer input and display the number in reverse order. Example Input: 1234 Example Output: Reversed number: 4321. 3, Program to Find the Sum of Digits of a Number Write a C program to calculate the sum of the digits of a given number. The program should take an integer input and display the sum of its digits. Example Input: 1234 Example Output: Sum of digits: 10 4, Program to Swap Two Numbers without Using a Temporary Variable Write a C program to swap two numbers without using a temporary variable. The program should take two integer inputs and swap thelr values. Example put:a=5, b=10 Example Output: After swapping: a= 10, b= 5 5. Program to check if a Number is a Palindrome Write a C program to check if given number is 8 palindrome, Anumber is a palindrome if it reads the same backward as forward, The program should take an integer input and determine if it is a palindrome. Example Input: 121 Example Output: 121 is a palindrome. 6. Program to Print Fibonacci Series Up ton Terms Write aC program to print the Fibonacci series up to n terms. The program should take an imeger input for the number of terms and display the series. Example Input: 5 Example Output: Fibonacci Series 01123 aged (a -il a 4 C Programming Test Questions 1. Write a C program that inputs two numbers, num1 and num2. Calculate how many even numbers exist between num and num2, and display the result. 2. Write a program te convert temperature from Celsius to Fahrenheit. Hint: Use the formula F = (C «9 / 5) + 32. Instruction: Input the temperature in Celsius. 3. Write a program to calculate the area of a circle given its radius. Hint: Use the formula Area =m rx r, Instruction: Input the radius of the circle. 4, Write a C program to check whether a character is an alphabet, digit or special character 5, Write a program to calculate simple interest given principal, rate, and time. Hint: Use the formula Simple Interest = (Principal x Rate x Time) / 100. Instruction: Input the principal amount, annual interest rate, ard time in years, 6. Write a C program tn firal whether a given year ic a leap year or not

You might also like