Checking whether a year is leap or not
This tutorial is going to teach you the coding for checking whether a year is a leap year or not. Here, we have taken the year 2000. So define an integer n=2000 in the class "Leapyear" and now apply "if else" condition. As we know leap year is divided by the integer 4 and so applying if condition as n/4=0, then "n" is a leap year. Now in the System.out.println write the message that the year is a leap year. Again applying "else" condition the output will be that the year is not a leap year.
Here is the code of program:
class Leapyear{ public static void main(String[] args) { int n=2000; if (n%4==0){ System.out.println("The given year is a leap year"); } else{ System.out.println("This is not a leap year"); }}} |
- Listing out leap years between certain period
The programming lesson will teach you the coding for finding and listing out the leap years between two years. In the following example we have to find out the leap years between 1990 and 2006.
Listing out leap years between certain period
The programming lesson will teach you the coding for finding and listing out the leap years between two years. In the following example we have to find out the leap years between 1990 and 2006. First define the two years under a class "leapyears". Let i = 2006 and n=1990. Now with the help of for loop method initialize the year as n=1990 and n<=i. Also apply the increment statement in the loop as we have to check one by one.
As we know a leap year is divisible by 4, define an integer l=n%4. So if 'n' is divisible by 4 or l=0, then the particular year can be a leap year. For checking this, apply the if statement and if this satisfies then, the year will be a leap year. For listing out each year write "+n" in the System.out.println.
Now compile and run the program in the command window and see the result. If you find any error, check the whole program and find out the
Here is the code of the program:
Here is the code of the program:
class leapyears { public static void main(String[] args) { int i=2006; int n; for (n=1990; n<=i ; n++){ int l=n%4; if (l==0){ System.out.println("leap year: "+n); } } }} |
- Preparing table of a number by using loop
This tutorial will teach you the methods of preparing the table of a given number by using loop condition. As we know the loop statements are used to repeat a statement or process multiple times according to a specified condition.
Preparing table of a number by using loop
This tutorial will teach you the methods of preparing the table of a given number by using loop condition. As we know the loop statements are used to repeat a statement or process multiple times according to a specified condition. Loop checks certain condition and if it finds the condition is valuable then all the statements written under loop are executed.
Here we will take a number a=25 of which we have to prepare a table. Define the integer a=25 and b=1 as the initial point. Now apply "while" condition of loop and confine b<=10 as we have to make a table of 25. Again define another integer as c=a*b, this will be the result when we multiply 'a' with 'b'. Here we have to multiply 'a' with 'b' up to 10 times like a*1, a*2....................a*9, a*10. So make define b=b+1 as increment operator.
Now compile and run the program on the command window.
Here is the code of the prorgram:
class PreparingTable{ public static void main(String[] args) { int a=25, b=1; System.out.println("the table of "+a+"= "); while(b<=10){ int c = a*b; System.out.println(c); b = b+1; } }} |
- Finding out the prime number
This lesson of Java programming language will teach you the coding to find out whether a given number is prime or not. Here we have used the 'for loop' statement and given the required condition for a prime number.
Find out the prime number
This lesson of Java programming language will teach you the coding to find out whether a given number is prime or not. Here we have used the 'for loop' statement and given the required condition for a prime number. As we know, a prime number is only divided by 1 and itself, in other words it has no other factorial other than 1 and the number itself.
Here, first make a class and named as "
Primenumber" and take an integer as num=11, and define an integer 'i' as the integer other than 1 and the given number. That means, i>2 and i<num. Now apply this in the "for loop" statement and define an integer n=num/i as given below in the example. Now apply the "if" condition and if the reminder of the earlier equation comes "0", then the result will be not prime. Again the loop system will check the above condition until it has not satisfied from the starting point(2) to the end(10). Here under this loop we have to use the "break" statement for unnecessary checking further one point where the reminder comes zero(0). Now after checking the whole condition, if the reminders does not come "zero", then we have to again apply the "if" condition and check whether i=num or not. If it is true then number (num) is prime. As we have taken here as num=11, then after compiling and running the program, the result will show that num is prime number.
Here is the code program:
class Prime_number { public static void main(String[] args) { int num = 11; int i; for (i=2; i < num ;i++ ){ int n = num%i; if (n==0){ System.out.println("not Prime!"); break; } } if(i == num){ System.out.println("Prime number!"); } }} |
- Prime Number in Java
This Java programming tutorial, we will be read how to get prime number between 1 to given number. First of all we have to define a class "PrimeNumber".
Prime Number in Java
This Java programming tutorial, we will be read how to get prime number between 1 to given number. First of all we have to define a class "PrimeNumber". Java I/O package has a input stream and a output stream in which input stream is used for reading the stream and memory allocating and the output stream used for writing bytes. As in this program we are going to insert certain instruction by creating buffer reader class. Here we have to create a buffer for the string class that can be used to instantiate a changeable object for storing and processing a string of character. Now use the ParseInt method for converting the parses the string argument and define 'num' as an integer.
Now applying in this program we use two 'for' loop. For loop will start from 1 to entered number. And another loop will start and divide it from 2 to less than those number. If number is divided by any number that means it is not prime otherwise prime number.
Here is the code of the Program
import java.io.*;class PrimeNumber { public static void main(String[] args) throws Exception{ int i; BufferedReader bf = new BufferedReader( new InputStreamReader(System.in)); System.out.println("Enter number:"); int num = Integer.parseInt(bf.readLine()); System.out.println("Prime number: "); for (i=1; i < num; i++ ){ int j; for (j=2; j<i; j++){ int n = i%j; if (n==0){ break; } } if(i == j){ System.out.print(" "+i); } } }} |
Java OOP Concept
- OOP In Java
Object Oriented Programming or OOP is the technique to create programs based on the real world. Unlike procedural programming, here in the OOP programming model programs are organized around objects and data rather than actions and logic.
No comments:
Post a Comment