Responsive Ads Here

Saturday, August 6, 2016

How to Learn Conditional Logic – Lesson#2

Today I’m gonna to show you such a program that can detect the inputed number is odd or even. We know if a number is divided by 2 and it’s remainder is 0, this number will be an odd number and the remainder is 1, this number would be an even number. So we’ve to divide the number by 2 and it’s remainder will show us if it is an odd number or even number. Let’s write the codes.

Code:
#include <stdio.h>   
 int main()   
 {   
     int number;
     scanf(“ %d”,&number);
     if(number % 2 == 0)
                 {   
                       printf("The number is odd\n");   
                  }   
     else
                  {   
                        printf("The number is even\n");   
                   }   
     return 0;   
}
Run the program and check the output. Here we first input a number by scanf function. We used integer type variable as we want to input integer type variable.
Then we used conditional logic to solve the problem. We gave the condition after “if”. “number%2==0” means the input number is divided by 2 and it’s remainder is 0. If this condition is true, the program will show the first printf area. If the condition is not true, the program will show the else printf area.

Output:






Now see another conditional logic related program. The program is to detect the inputed letter is small letter or capital letter. The one way to solve the problem may be we can keep all 26 letters in a variable and then we can check it if it is capital letter or small letter. Which condition will be true, the program will print this area. So the program codes will be…

Code:

 #include<stdio.h>
int main()
{
char ch = 'p';  

if (ch == 'a')  

{  

printf("%c is lower case\n", ch);  

}  

else if (ch == 'A')  

{  

printf("%c is upper case\n", ch);  

}  

else if (ch == 'b')  

{  

printf("%c is lower case\n", ch);  

}  

else if (ch == 'B')  

{  

 printf("%c is upper case\n", ch);  

 }    

 else if (ch == 'c')  

{  

printf("%c is lower case\n", ch);  

}  
else if (ch == 'C')  
{  

 printf("%c is upper case\n", ch);  

}
…………..continue
return 0;

}

But this is not the easy way to solve the problem. We need to apply a different way. We need to use AND Operator to solve the problem easily. In C language, AND Operator is expressed by “&&”. Ok, let’s write the code,

Code:
#include<stdio.h>
int main()
{  
     char ch;
     scanf(%c,&ch);
     if(ch >= 'a' && ch <= 'z')
                           {  
                                    printf("%c is lower case\n", ch);  
                           }  
     if(ch >= 'A' && ch <= 'Z')
                           {  
                                     printf("%c is upper case\n", ch);  
                            }  
     return 0;  
 }

Output:



See the output. Enter a letter and check it. Here we first, input a character type variable by scanf function. Then we check a to z in lower case by (ch >= 'a' && ch <= 'z'). If the condition is true, the program will show the first printf area. Otherwise, the program will print the other area.

No comments:

Post a Comment