Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Tuesday, June 21, 2016

Write a code in JAVA to find a prime number

Before going forward you must know what is meaning of prime number.
Prime number is a number which is divisible completely by one and itself only . For example , 3 is a prime number because it can only be completely divided by 1 and 3.
So , using "For" loop to find a prime number , the code will become:

Wednesday, April 20, 2016

Write a code in which keep taking marks from user until user enter a negative number and calculate grade and at end give total number of A,B,C,D,E and F grades.

In this question we are required to  create a program which we will keep taking marks from user until user enter a negative number and then calculate grade after every input .At end give total number of A,B,C,D,E and F grades.
So here is the JAVA code for solving that problem.

Tuesday, April 5, 2016

Example of While loop & Nesting of loops

Nesting of loops:
Nesting of loops mean using loop inside a loop.It could be "For" loop inside a "For" loop or a "For" loop inside a "While" loop or the other way round. 

Our Objective ?
Our objective is to write a JAVA program which keeps on taking values from user unless user give a prime number as input value.
We can easily solve this problem using nested loops.
We will use a "While" loop because we don't know when user will give a prime number as input value.

Sunday, April 3, 2016

Print out table of any number given by the user

As we have already studied about "For Loop" , now we will see some example codes for that.
if you haven't read that before then check out : Introduction to loop and its types .
For example if you want to print out table of any number , given by the user then you can use "For" loop for that process and the code will become:


package table;
import java.util.Scanner;

public class Table
{
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int a=input.nextInt();
        for(int i=1;i<=10;i++)
        {
            int b=a*i;
            System.out.println(a+" * "+i+"= "+b);
        }
        
    }
}



Introduction to loop and its types

Actually loop is a process of repeating the same process again and again without writing it multiple times.
For example if you want to take 50 inputs from user then you can use a loop to take those inputs by using the input statement only once.
The process of repeating the same thing again and again ( Loop ) is known as Iteration.
Types of Loop:

  • "For" Loop
  • "While" Loop
  • "Do While" Loop
"For" Loop:
"For" Loop is used when we know how many time iteration will be performed.For example if we know that we will take 50 inputs from user then we will use "For" Loop for that work .
Syntax for a "For" loops is:
for(int i=starting value;condition;increment or decrement)
{
    This is the body of loop.
}

"While" and "Do While" Loop:
"While" and "Do While" Loop is used when don't know how many times the iteration will be performed.For example if we know that we want to do the given process but we have no idea that how many times we will perform this thing then we use "While" or "Do While" loop.
Syntax for "While" loop is :
while(true/condition)
{
    this is the body of "while" loop.
    use a break statement to end the loop . It is recommended to use a break statement in a      condition.
}

Syntax for "Do-While" loop is:
do
{
   This is body of "Do-While" loop.
   
}while(condition);


Sunday, March 27, 2016

Check if given date is valid or not , using java coding



In order to check if the given date is valid or not you need to know the logic behind it.
  • During Leap year, February have 29 days.While generally February have 28 days in it.
  • April , June , September and November consists of 30 days.
  • While remaining months have 31 days in them.
We will be using only "if & else" conditions throughout the program coding.There is nothing else included in it.So let's get started.Here is the code for checking if given date is valid or not:


package labsessional;

import java.util.Scanner;
public class LabSessional
{
    public static void main( String [] args)
    {
        int day,month,year,leepYear ;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter day : ");
        day=input.nextInt();
        System.out.print("Enter month: ");
        month=input.nextInt();
        System.out.print("Enter year: ");
        year=input.nextInt();
        leepYear=year%4;
       
    if(day>=1 && day<=31 && month>=1 && month<=12 && year>=1950 && year<=2099)
           
        {
            if (leepYear==0 && month==2 && day>=0 && day<=29)
                System.out.print("Your date is valid. ");
         else
         {
       
               
            if(month==4 || month==6 || month==9 || month==11 )
           
             {
                 if(day>=0 && day<=30)
                System.out.print("Your date is valid. ");
             else
                System.out.print("Your date is invalid. ");   
             }
   
            else if (month==2)
             {
                if(day>=0 && day<=28)
                    System.out.print("Your date is valid. ");
                else
                    System.out.print("Your date is invalid. ");
             }
             else if ( month==1|| month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
              {
                  if(day>=0 && day<=31)
                      System.out.print("Your date is valid. ");
                  else
                      System.out.print("Your date is invalid. ");
              }
           
        }
    }
        else
            System.out.print("Your given date is invalid.");
 }
}


If you have any confusion or you found any error in our code , then please feel free to contact us . You can email us.Our email address is : k.ahmed2k19@gmail.com
If you liked it , then please share it with your friends. Keep visiting & Keep learning.

Saturday, March 26, 2016

Java code for finding Area Of a Circle



Here is the java code for finding the area of a circle.
You need to import "Scanner class", so that you can take input from user.That input will be radius of the circle.

package javaapplication2;
import java.util.Scanner;
public class JavaApplication2 
{
    public static void main(String[] args) 
    {
    double radius;
    double area;
    Scanner input=new Scanner(System.in);
    System.out.print("Enter Radius: ");
    radius=input.nextDouble();
    area = radius *radius * 3.14;
    System.out.println("The area of the circle of radius " + radius +" is " +area);
    
    }
    
}


If you found any error in this code or if you have a better and short java code for this work then please feel free to send it on our email so that we can upload it too.
Our contact email is :  K.ahmed2k19@gmail.com
If you like it then share it with your friends.Keep visiting and keep learning.

Thursday, March 24, 2016

Rules and Compilation of first java program

Rules:
Here are some of the rules and conventions that you need to follow while writing a code.

  • You need to import a package before starting anything else.
  • Package must contain at least one "Class".
  • Each "Class" must contain one "Method (function) ".
  • If you use more than one word jointly then start first word after the dot with a small letter while first letter of each word next to it will be capital.For example: " ItIsAnExample ".
  • If you are creating an app then you must have to use "main" method.
    If you haven't read our article on "Basics of Java" then i would recommend you read it first before reading it further.
  • A data type for a variable must be defined before using that variable.
    For example " int a ; " ,this will define that "a" is a variable and it can be only an integer.
    if we directly write "a" without writing any data type with that then it will display us an error.
  • You need to terminate each and every syntax if it is terminating . For example:
                            System.out.print("Hello world!");
           Here , ";" is used to terminate the syntax.
           Example of non-terminating syntax is :
            if( a>b)
            We can't terminate if and else conditions , because if we do so then it will not read its                            remaining portion .

  •  "Else" cannot be used without "if " but "if " can be used without "else ".
  • Don't change the "casing" of words during coding .For example if you used "a" as a variable then you cannot use "A" in place of that because they both are different words in Java's point of view.
  • While using "if " and "else" syntax , if you need more than one function or syntax, you will need to use curly brackets for that.
  • Pre-defined words (keywords) cannot be used as identifies ( variables ).


Writing your first java program:

import java.lang.*;
public class Test
{
   public static void main (String[] args)
     {
            System.out.println("Hello world!");
            System.out.print("This is our first Java program.");
      }
}



The output of Code will be:

"Hello word
This is our first Java program. "

Explanation of code:
  • "import java.lang.*;" .This command is importing the package.
  • "public class Test". This command will create a new class with name "test".
  • "public static void main(String[] args) ".This command is used to differentiate between application and applet.
  • "System.out,println("Hello world!");". This command will generate an output showing "Hello world!" and then cursor will move to next line because "ln" after "print" means that after generating the display of the given string , cursor will move to new line.
  • "System.out.print("This is our first Java Code.");" . This command is also used to display the output to user  ,but the difference is that it doesn't move cursor to next line  after displaying the string if anything is written after that syntax,it will execute that in same line.

So here was our first and simplest java program.If you like this then please share it with your friends .
Keep Visiting and Keep learning.

Sunday, March 20, 2016

Basics of Java programming language

As we are already aware of the Java's benefits and introduction , now we can start implementing it ..
If you have not read the introduction and benefits of Java then click here to read it first:
"Introduction to Java".
So lets get started.In this tutorial we will learn about some basics of Java programming language.
Before proceeding further , keep in mind that Java is a case sensitive language . It treats "a" and "A" as two different variables which means we cannot use "A" in place of "a".
Here are the few important and commonly used terms , commands (Syntax) , keywords and their role.


A brief Introduction to Java language

Java is an object oriented language.It is platform independent language and it is a case sensitive language.For example it treats "Test" and "test" as two different variables. It is considered to be fourth generation language.
Java can be used for two main purposes:
  • For Creating Application
  • For Creating Applets

What is difference between an Application and an Applet ?

Application is the program which can be executed (run ) on windows.While an Applet is a program which can't be executed on windows , it can only be used on websites.

introduction to java