Responsive Ads Here
Showing posts with label learn to code c. Show all posts
Showing posts with label learn to code c. 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:



Monday, August 1, 2016

Basic Structure of C Language

C programming language is very popular to programmers. It’s also powerfull. To learn to code C, you’ve to maintain some basic steps. In this article, I’ll show you these steps what are must to run the program. Otherwise, compiler will unable to make output of the witten codes.

How to learn to code C

How to Learn to Code C

There are some section of a complete C program. First one is the main function. The main function will be written after the following line.
#include<stdio.h>
int main()
The second line is the main function line. Then we’ve to take second bracket to keep all the codes in this function.
{  ( Function elements. Such as declaration, initialization, operation and output part) }

In the bracket division, first part is the declareation part where variables are declared. Variable declaration means what type of data you want to take or input. Declaration part would be like the following integer data type declaration.
int a, b, c;
Some of these types of variables are used largely. These are,
  • int for Integer type.
  • double for large
  • integer number.
  • float for Float type
  • char for Character type
Then the initialization part. Different signs like following these are used here to take the variables value.
  • %d  --->>> for decimal number.
  • %lf  --->>> for float number.
  • %f   --->>> for long float number.
  • %c  --->>> for character type value.
The initialization part looks like,
scanf(“%d %d %d”, &a,&b,&c);

After initializing the variables, then the operation part. Operation part means, presenting the equation to make output of this program.
c=a+b; 
printf(“Result is : %d”,c);

Finally, the following line to execute the program.
return 0;
These are the basic parts of a C program. Now write all the codes together on a blank page and run it to see the result.

/* Coding start */

#include<stdio.h>
 int main()
{
int a, b, c;            
scanf(“%d %d %d”, &a,&b,&c);           
 c=a+b;            
printf(“Result is : %d”,c);      
return 0;
}

/* Coding end */

To know well, read the previous articles,


Sunday, July 31, 2016

How to Learn C Programming Language - Lesson #2

Hey guys, you know what programming is the heart of technology and technology is the most important thing in this modern era. Just think the importance of learning programming! Anybody can learn to code C and can participate in the competetion of invention new things. So, I decided to write a serial writting about coding.

C Programming Language

There are many languages to write a program. Here we'll start in C/C++ language. C and C++ are about to same. For that, we'll start in C and finish in C++ at last. As we'll write a program in C language, we've to use a C language compiler. It can be Code Blocks, Quincy or Turbo C++. Here we'll use Code Blocks. To download and install Code-Blocks click here,
To install the Code-Blocks softwere click here,

>>>How to Intall Code-Blocks.

How to Learn to Code C

We'll write the program first and then will try to understand it. Let's start our first program...

/*  Coding start  */

#include<stdio.h>
int main()
{
      printf("Hello World!");
      return 0;
}

/*  Coding end  */

This is our first program. Just now you've written it. So you're a programmar! :-D . Yeah, you've turned yourself as a part of programming world.

Let's have a look on the output of our first program. Here is this!
To understand well, revise the lesson one here,


Ok, now we'll try to understand the program. There are some divisions in a C language program. And it is definite for all C language used the programs.

  • First we need to type "#include" to start a program in C. What we are writting in the compiler, what are these? Actually, these are instructions. We input these instructions and compiler will make the result of these. "stdio.h" means standard input output.
  • Then we wrote the main function. "int" means integer.
  • Then we need to declare the variables. As it is a simple program and there is no veriable in this program, we'll try to understand it in the next programs.
  • What we want to show in the screen, we'll write these in the printf("") qouatation. Run the program and you'll see "Hello World!" in the screen.
  • Every statements are closed by placing ";" at the end.
  • After all the statements and conditions we've to write "return 0;" to close the program.
  • All the variables, statements and conditions will be in "{}".

                  How To Learn C Programming Language - Lesson #1

                  Hey guys, I’m going to start a tutorial addressing as “How to Learn C Programming Language”. It will be a serial tutorial. Methods of C language coding will be present here with different types of problems or examples. I think you’ll get benefits from these lessons. C is a powerfull programming language among all. So, It contains high impotance in the programming world. C and C++ is quite same. Actually C++ is the latest version of C language. If you've interest on it you should learn to code C language. It will help to learn C++ in future easily. Let’s start…

                  learn to code c

                  How to Learn C Programming Language

                  1. As computer don’t understand our local or international language, we’ve to use a compiler to make computer understood. Different compilers like Quincy, Code-Blocks, turbo C++ and etc are used to write C codes. We’ll use Code-Blocks to write C programming language. To download this softwere, click here.
                                  >>>Download Code-Blocks.
                  1. After completetion the downloading process of Code-Blocks, Run the Softwere. To know more about the running methods of this softwere, read this article.
                                                   >>>How to Install Code-Blocks on Windows PC.
                  1. Then open the softwere from the desktop or start menu.

                  1. After opening the softwere take a new document from File >>> New >>>Empty file or press on Ctrl+Shift+N.
                  1. In the new empty document, here we’ll have to type the codes.
                  1. After writing the codes click on “Build” option from the menu bar. And then “Build and Run” to run the program.

                    1. If the codes are correct and build these correctly a new window will appear like this. Result will be shown here of this program.
                    This is the entire process of being ready for coding. If you complete the lesson, you can start coding now. To write your first program move on the next article.