salamo3alikom everyone :) hope you are fine :)
last session Practice
Practice 1:
write a program that take two numbers from user and print their sum and multiplication and subtraction each in a separated line.
#include<stdio.h>
int main()
{
int N1, N2;
printf("Please Enter Two Numbers\n");
scanf("%d%d",&N1,&N2);
printf("The Sum = %d\n",N1+N2);
printf("The Multiplication = %d\n",N1*N2);
printf("The Subtraction= %d\n",N1-N2);
return 0;
}
Session Agenda:
- If Condition.
- And , OR operators.
A company has a security number for accessing its database so they made security numbers so that they can give specific people the access to this database.
they called the security database number SDN ,which should be 4 digits that is bigger than 1000 and divisible by 2.
write a program that asks for a password before logging into company's database and if the password match the SDN requirements grant access to the database by typing "successful login".
IF Condition
a conditional statement that perform specific tasks under conditions.
if(something happened)
{
do this
}
let's try to solve the previous program ..
we need a variable for holding our password let's call it Pass then we will ask the user to enter the password and store it in Pass variable so our code should look like this
#include<stdio.h>
int main()
{
int Pass;
printf("Enter your Password\n") ;
scanf("%d",&Pass);
return 0;
}
And Operator
now we need to check if the given Password is a right SDN or not. so we going to add this after scanf statement:
- check if it was bigger than 1000 .--> if(Pass> 1000)
- check if it is divisible by 2 -->
- a number is divisible by 2 if the result of division has no reminder
- we use the % for modulus operator so that we can check if there is no reminder.--> if(Pass%2==0)
if(Pass>1000 && Pass%2==0) //which means both conditions should be true
{
printf("Successful Login\n");
}
so the code should look like this:
#include<stdio.h>
int main()
{
int Pass;
printf("Enter your Password\n") ;
scanf("%d",&Pass);
if(Pass>1000 && Pass%2==0) //which means both conditions should be true
{
printf("Successful Login\n");
}
return 0;
}
OR Operator
what if our SDN shouldn't have both features ?
we need to tell the computer to check if the password is >1000 or even divisible by 2 in both cases it will be a successful login what we should do??
here we need to replace our And operator by Or operator
if(Pass>1000 || Pass%2==0) //if at least one condition happened
{
printf("Successful Login\n");
}
Session Practice:
write a program that takes two numbers and always print the bigger one :).
thanks so much salamo3alikom :)
No comments:
Post a Comment