Professional Documents
Culture Documents
Computing Fundamentals: Dr. Muhammad Yousaf Hamza
Computing Fundamentals: Dr. Muhammad Yousaf Hamza
if(num%2==0)
printf("\nThe number %d is an even number",num);
if(num%2!=0)
printf("\nThe number %d is an odd number",num);
getchar(); getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
To decide even/odd
#include<stdio.h>
int main()
{
int num;
printf("Please enter an integer number:\n");
scanf("%d",&num);
else
printf("\nThe number %d is an odd number",num);
getchar(); getchar();
return 0;
}
Dr. Muhammad Yousaf Hamza
Control Statements
if (age<=12)
printf("Please go to Child Specialist in Room 10\n\n");
else
printf("Please go to Medical Specialist in Room 15\n");
printf(“Allah Hafiz");
getchar();
return 0;
}
Dr. Yousaf, PIEAS
The if Statement
• Form 1:
if (expression)
statement1;
next statement;
• Form 2:
if (expression)
statement1;
else
statement2;
next statement;
false true
age < = 12
print “15” print “10”
• && AND
• || OR
• ! NOT
.
.
.
true case z break
case z
false action(s)
default
action(s)
Dr. Yousaf, PIEAS
Switch Statement
• The expression used in a switch statement must have an integral or
character type.
• You can have any number of case statements within a switch. Each case
is followed by the value to be compared and a colon.
• The constant-expression for a case must be the same data type as the
variable in the switch, and it must be a constant or a literal.
• When the variable being switched on is equal to a case, the statements
following that case will execute until a break statement is reached.
• When a break statement is reached, the switch terminates, and the flow
of control jumps to the next line following the switch statement.
• Not every case needs to contain a break. If no break appears, the flow of
control will fall through to subsequent cases until a break is reached.
• A switch statement can have an optional default case, which appears at
the end of the switch. The default case can be used for performing a task
when none of the cases is true. No break is needed in the default case.
printf("%f", y);
getchar();
return 0;
}
Output: 1.000000
Dr. Yousaf, PIEAS
// If given theta is in degree, then convert it first into radians.
#include<stdio.h>
#include<math.h> // use of math.h
#define PI 3.14
int main()
{
float y;
float theta_deg, theta_rad;
theta_deg = 90.0;
theta_rad = (PI/180)*theta_deg;
y = sin(theta_rad); // argument is in radian
printf("%f", y);
getchar(); return 0; }
Output: 1.000000
Do it Yourself
int main()
{
printf("*");
getchar();
return 0;
}
int main()
{
printf("*\n*");
getchar();
return 0;
}