Responsive Ads Here
Showing posts with label c programming language. Show all posts
Showing posts with label c programming language. Show all posts

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.

Friday, August 5, 2016

How to Print Fractional Numbers in C


If we want show a fractional no. like 54.839373 this. The codes will be,

#How to Print Fractional Numbers in C


Code:
#include <stdio.h>  
 int main()  
 {  
     double a =  54.839373; 
     printf("%lf", a);  
     return 0;  
 } 
Output:


Run the program and see the output. The program prints the numbers correctly. Now, write the following codes again replacing the fractional number by 46.664765767.


Code:
#include <stdio.h>  
 int main()  
 {  
     double a =  54.83559373; 
     printf("%lf", a);  
     return 0;
 }
Output:


Run the program again and see the result. Look, the program can not make the correct result. Now write the codes in bellow,
#include <stdio.h>  
 int main()  
 {  
     double a =  54.83559373; 
     printf("%.8lf", a);  
     return 0;  
 }
Output:


Run the program and see the result again. This time, program are presenting the correct number. Now think why it’s happened? Look the printf quotation line. We wrote .9 to print the 9 digits after the point. As the number contains 9 digits after the point so we’ve to write the total number of digits or more after the point.


Now write a program to print 76.8686765977.

To know more about this topic,

How to Learn C - Conditional Logic

If we need to use any condition to solve a problem, then we’ve to apply if-else method. This is called conditional logic.
c programming

General structure of this program:
#include<stdio.h>
int main()

{
            if ( condition)

                     {
                                    statement
                      }
            else
                    {
                                   statement
                     }
    return 0;
}
We’ve to write the condition in ( ) after “if”. If the given condition is true then, the the program will show the output of “if” area. If the given condition is not true, then the program will the result of “else” area.

We’ll think a problem now and make it solved. Ok, think with me. Suppose, We want to create a program which will be able to take a number input from the user and inform the user that the number is positive or negative. After writing the codes of this program we will try to understand. Now write the program.

How to Learn to Code C

Code:
#include <stdio.h>   
 int main()
 {   
     int n;   
     scanf (“%d”, &n);   
     if(n >= 0)
{   
                        printf("The number is positive\n");  
}   
     else
 {   
                         printf("The number is negative\n");   
            }  
      return 0;   
}
Output:


Run the program and input a decimal number then press on enter to see the output. If the program shows the correct result that means if it can detect the number is negative or positive, the written codes are all right. In the program we used scanf function to input a number from the user. Then we check the inputed number as it is greater than 0 or equal to 0. If it is true then the program can show “The number is positive” for that we keep the line in printf function. If the inputed number is less than 0, the program will make the output of the else area and will show “The number is negative”. ( \n ) is used to see the next result in new line.


If your mind ask you that 0 is not a positive number and you want to show 0 as a nutral number, then you write the following program.

Code:
#include <stdio.h>   
 int main()   
{   
     int n;
     scanf(“%d”,&n);
     if(n < 0) {   
         printf("The number is negative\n");   
     }   
     else if (n > 0) {   
         printf("The number is positive\n");   
     }   
     else if (n == 0) {  
         printf("The number is zero!\n");   
     }   
     return 0;  
}

Output:


In this program, 0 is assigned in a new line and new condition. As there is more than 2 conditions, we used else if.

If (n<0) : Here we examined as n is less than 0 or not. If less, then the program will show “ The number is negative”.

If (n>0) : We check the number as it is greater than 0 or not. If the condition is true, then the program will show the printf area.

If (n==0) : Here we  checked the number as it is equal to 0 or not. We used here ( = =)  to make the compiler understand that n is equal to 0.

How to Input Character Type Data

Today we’ll write a program using Character type data. Character type data is used to assign letters in English language and special symbols like summation (+), substriction (-), multiplication (*) and division (/) and etc. A character type symbol needs a byte to keep in memory.
c programming

How to Learn to Code C

The following program is to input and print the first letter of your name. Write the codes and run the program.

Code:

#include <stdio.h>  
 int main()  
{  
     char ch;  
     printf("Enter the first letter of your name: ");  
     scanf("%c", &ch);  
     printf("The first letter of your name is: %c\n", ch);  
     return 0;  
}
Output:



We wrote char as it wanted to input a character type data. For this reason, we wrote %c in the scanf and printf quotation. ( \n ) is for new line.


We can write the program in another way,

Code:

#include <stdio.h>  
 int main()  
{ 
     char ch;  
     printf("Enter the first letter of your name: ");  
     ch = getchar();  
     printf("The first letter of your name is: %c\n", ch);  
     return 0;
 }
Output:


Getchar is also a function that can read character type data. In this program , getchar function assigned a character after reading. If we want to keep a character directly in a variable, we’ve to use quotation like char c = 'A';

Now, see the program like this…

Code:

#include<stdio.h>
int main()
{
int num1, num2, value;
char sign;
printf("Please enter a number: ");
scanf("%d", &num1);
printf("Please enter another number: ");
scanf("%d", &num2);
value = num1 + num2;
sign = '+';
printf("%d %c %d = %d\n", num1, sign, num2, value);
value = num1 - num2;
sign = '-';
printf("%d %c %d = %d\n", num1, sign, num2, value);
value = num1 * num2;
sign = '*';
printf("%d %c %d = %d\n", num1, sign, num2, value);
value = num1 / num2;
sign = '/';
printf("%d %c %d = %d\n", num1, sign, num2, value);
return 0;
}
Output:


Write the program and run it. See the output and try to understand. I wish you’ll be able to understand.

How to Comment in a Program

Experianced programmars always leave a comment after writing important code lines so that they or anybody can understand easily later. This is a good method for understanding to the new less exprianced programmars.


To comment in a program, you’ve to write in a definited area. You can write comment in a single line or more in the middle of  /* your comments here */ . Compiler don’t compile these area. Now write the following program and run it to understand well.

Code:
#include <stdio.h>
int main()

{
printf("Hello World!");

/* We have printed Hello World.
Note: this is a multi-line comment */

return 0;
}
See the result. The result is only “Hello World!” because the compiler didn’t compile the /*-------- */ area.


Output: