salamo3alikom everyone :)
Last session we take the conditional statement if and the logical operators and /or
in this session we will practice on how to use if condition and logical operators.
Session Agenda:
- Practice on if condition and Logical operators.
write a program that takes two numbers and always print the bigger one :).
solution :
#include<stdio.h>
int main()
{
int N1, N2;
printf("Please Enter Two Numbers\n");
scanf("%d%d",&N1,&N2);
if(N1>N2)
printf("%d\n",N1);
if(N2>N1)
printf("%d\n",N2);
return 0;
}
let's take a look about this solution :
if(N1>N2)
printf("%d\n",N1);
why we didn't put { } after the if condition as we said before ?
if you have only one operation there is no need to put {} as if implements automatically the first line below it.
but if you have multiple operations you need to put {} like this :
if(N1>N2)
{
printf("%d\n",N1);
printf("another statements\n");
//some code
}
ELSE Statement
what is the possible comparisons between two numbers?
Number 1 bigger than Number 2
Number 1 smaller than Number 2
Number 1 equal to Number 2
okay so if N1 is bigger than N2 we will print N1 but if N1 is smaller or even equal we need to print N2 right ?
well we will modify the previous program to be much simpler to be like this:
#include<stdio.h>
int main()
{
int N1, N2;
printf("Please Enter Two Numbers\n");
scanf("%d%d",&N1,&N2);
if(N1>N2)
printf("%d\n",N1);
else //else statement used if you don't care about other conditions
printf("%d\n",N2);
return 0;
}
this means print N1 if it was bigger and print N2 if the first condition failed.
Practice 2:
write a program that takes a number from user and check if it was even or odd number.
Even & Odd:
- Even Number a number who is divisible by 2.
- Odd Number a number who isn't divisible by 2.
2
output:
Even
sample input :
5
Output:
Odd
#include<stdio.h>
int main()
{
int x;
printf("Please Enter a number\n");
scanf("%d",&x);
if(x%2==0)
printf("Even\n");
else
printf("Odd\n");
return 0;
}
Practice 3:
Write a program that takes a number and check if it was a multiple of three or not.
- multiple of three ==> is divisible by 3.
9
output :
Yes
sample input :
20
output:
No
#include<stdio.h>
int main()
{
int x;
printf("Please Enter a number\n");
scanf("%d",&x);
if(x%3==0)
printf("Yes\n");
else
printf("No\n");
return 0;
}
Practice 4:
Write a Program that takes Three numbers and print them sorted.
sample input:
5 1 2
output :
1 2 5
sample input:
6 1 8
output:
1 6 8
#include<stdio.h>
int main()
{
int x,y,z;
printf("Please Enter Three number\n");
scanf("%d%d%d",&x,&y,&z);
if(x<=y && y<=z)
printf("%d %d %d\n",x,y,z);
else if(x<=z && z<=y) // this means if first one didn't occur check me
printf("%d %d %d\n",x,z,y);
else if(y<=x && x<=z)
printf("%d %d %d\n",y,x,z);
else if(y<=z && z<=x)
printf("%d %d %d\n",y,z,x);
else if(z<=x && x<=y)
printf("%d %d %d\n",z,x,y);
else if(z<=y && y<=x)
printf("%d %d %d\n",z,y,x);
return 0;
}
if(condition 1 )
do task 1;
if (condition 2)
do task 2;
.........
.......
this means each time you are going to check all conditions.
but if you made it like that :
if(condition 1 )
do task 1;
else if (condition 2)
do task 2;
you will check condition1 one if succeeded you won't check condition 2.
so you don't need to pass through all conditions since you got your condition.
i hope you enjoyed this session :) thanks so much salamo3alikom.
No comments:
Post a Comment