Anda di halaman 1dari 16

ANSWER-Assignment

#include <stdio.h>
main()
{
float Quiz1, Quiz2, Quiz3, Sum, Average;
printf (Enter first quiz: );
scanf (%f, &Quiz1);
printf (/nEnter second quiz: );
scanf (%f, &Quiz2);
printf (/nEnter third quiz: );
scanf (%f, &Quiz3);
Sum = Quiz1 + Quiz2 + Quiz3;
Average = Sum / 3;
printf (\n\nAverage is: %.2f, Average);
}
IF STATEMENT
The if statement allows the programmer to make
decisions within a program.
The general format of an if statement is:
if (expression)
statement
where expression represents a relational, equality,
or logical expression (conditional expression).

Example:
if (final_grade > = 70)
printf (You passed this course!\n);
printf (Your grade is %d.\n, final_grade);
IF STATEMENT
If there is a need to execute several statements
(compound statement), then the left and right braces
can group these statements.

Example:
if (final_grade >= 70)
{
printf (You passed this course!\n);
printf (Congratulations!\n);
}
printf (Your grade is %d.\n, final_grade);
IF STATEMENT
THE if-else STATEMENT
In the if statement, if expression is true, then the
program executes a statement. However, if expression
is false, no action is taken and the program executes
the statement after the if statement. If a certain
action has to be taken if expression is false, then
programmers can use the if-else statement.
The general format of an if-else statement is:
if (expression)
statement1;
else
statement2;
IF STATEMENT
If expression is true, then the program executes
statement1and skips statement2. If expression is false,
then the program skips statement1 and executes
statement2.

Example:
if (final_grade > = 70)
printf (You passed this course!\n);
else
printf (You failed this course!\n);
printf (Your grade is %d.\n, final_grade);
IF STATEMENT
As in the if statement, compound statements may also be
used in if-else statements (provided they are grouped
together by braces).
Example:
if (final_grade > = 70)
{
printf (You passed this course!\n);
printf (Congratulations!\n);
}
else
{
printf (You failed this course!);
printf (Sorry!\n);
}
IF STATEMENT
Example: Create a program that prints on the screen x is
100 if x has a value of 100, but if not it prints out x is not
100.
#include <stdio.h>
main()
{
int x;
printf(Enter the value for x: );
scanf (%d, &x);
if (x == 100)
printf("x is 100);
else
printf("x is not 100);
}
IF STATEMENT
Example: Create a program that will accept two numbers and
print the number with the higher value.
#include <stdio.h>
main()
{
int Fnum, Snum;
printf (Enter First Number: );
scanf (%d, &Fnum);
printf (Enter Second Number: );
scanf (%d, &Snum);
if (Fnum > Snum)
printf(%d , Fnum);
else
printf(%d , Snum);
}
IF STATEMENT
if (x > 5)
if (x < 10)
printf (x is between 5 and 10.);
The following code is equivalent to the one
previously given:
if (x > 5 && x < 10)
printf (x is between 5 and 10.);

EXERCISE #7
Draw a program to print TRUE if A is greater
than B, and C is less than D or if A is greater than
B and D is less than 1. Print False if otherwise.
(All values are entered by the user)
IF STATEMENT
If - else if - else

Example:
if (number > 0)
printf (The number is positive\n);
elseif (numer < 0)
printf (The number is negative\n);
else
printf (Number is equals to 0);
IF STATEMENT
EXERCISE # 8
Create a program that will accept input for gender
and print MALE if the user entered M. Print FEMALE
if the user entered F. Otherwise print Invalid Gender.
IF STATEMENT
#include <stdio.h>
main()
{
char gen;
printf (Enter gender: );
scanf (%c, &gen);
if (gen == M)
printf(MALE);
else if (gen == F)
printf(FEMALE);
else
printf(Invalid Gender);
}
IF STATEMENT
EXERCISE # 9
Create a program that will let the user enter a
number from 1 3. If 1 is entered, print Hello; if 2 is
entered, print Hi; and if 3 is entered, then print
Bye. Otherwise, print Invalid Number.
IF STATEMENT
#include <stdio.h>
main()
{
int choice;
printf (Enter a number: );
scanf (%d, &choice);
if (choice == 1)
printf(Hello);
else if (choice == 2)
printf(Hi);
else if (choice == 3)
printf(Bye);
else
printf(Invalid Number);
}
IF STATEMENT
QUIZ
Create a program that will would print the
Department under a certain ID Number.

IDNumber Department
1-3 Owner
4-7 Management
8-9 Employee
ASSIGNMENT
Create a program that will print the students
letter grade given the following specifications :
90 100 = A
80 89 = B
70 79 = C
60 69 = D
below 60 = F

Anda mungkin juga menyukai