350 likes | 361 Views
This program calculates zakat based on the user-entered amount, where zakat is equal to 2.5% of the amount. If the amount is less than 1000, zakat is not calculated.
E N D
Revision Nouf almunyif
Pseudocode & Flowcharts Nouf almunyif
Example 1 • Draw a flowchart for a program that calculates the Zakat, where the user enter the amount of money then the program show the zakat. • Zakat =(2.5/100) * amount. • Zakat is not calculated if the amount is less than 1000 S.R Nouf almunyif
Solution • Input • amount. • Processing • Check if amount is below 1000 Zakat =0. • Check if amount is above 1000 Zakat =(2.5/100) * amount • Output • Zakat Nouf almunyif
Solution Start Read amount yes Amount > 1000 no Zakat=0. Zakat=(2.5/100)*amount Print Zakat End Nouf almunyif
Example 2 Draw a flowchart to find the sum of first 50 natural numbers. 1+2+3+ ….. +50 Nouf almunyif
Structure of a .c file Nouf almunyif
input/output Nouf almunyif
Output: If you need to display a string on the screen Nouf almunyif
Input: if you want to read values from the user Nouf almunyif
Data types Nouf almunyif
Declaring different types Nouf almunyif
output Nouf almunyif
Constants Nouf almunyif
Operator precedence Nouf almunyif
Branchingif syntax Nouf almunyif
Example • Write a code that read x and change its value as follows: • If x is even, divide x by 2. • If x is odd, multiply x by 3 and subtract 1. Nouf almunyif
Branchingif syntax if (testExpression1) { // statements to be executed if testExpression1 is true } else if(testExpression2) { // statements to be executed if testExpression1 is false // and testExpression2 is true } else if (testExpression 3) { // statements to be executed if testExpression1 //and testExpression2 is false and testExpression3 is true } . . else { // statements to be executed if all test expressions are false } Nouf almunyif
example Nouf almunyif
what will be printed if x=5 y=8 Nouf almunyif
Modify the previous code to produce the output shown. Nouf almunyif
Conditional operator ?: if condition is true ? then X return value : otherwise Y value; Exercise: Write a code that test whether an integer variable score contains a valid test score. Valid test scores are in the range from 0 to 100. Nouf almunyif
Conditional operator ?:if condition is true ? then X return value : otherwise Y value; Nouf almunyif
switch...case StatementSyntax • switch (n) • { • case constant1: • // code to be executed if n is equal to constant1; • break; • case constant2: • // code to be executed if n is equal to constant2; • break; • . . . • default: • // code to be executed if n doesn't match any constant • } Nouf almunyif