Showing posts with label Basic Java Programs. Show all posts
Showing posts with label Basic Java Programs. Show all posts

java program to find fibonacci series

In this post you will learn what is Fibonacci series and how to write code for that using java programming.

Fibonacci:

Fibonacci series is the series of numbers where each number is obtained by adding two previous numbers. The first two numbers in the Fibonacci series are either 1 and 1 or 0 and 1.

Example: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89……..

Program1:

class Fibonacci{
public static void main(String args[])
{
int prev, next, sum, n;
prev=next=1;
for(n=1;n<=10;n++){
System.out.println(prev);
sum=prev+next;
prev=next;
next=sum;
}
}
}
OUTPUT:
javac Fibonacci.java
java Fibonacci
1 1 2 3 5 8  13 21 34 55

Using Recursion:

import java.util.Scanner;

class calc{
int fibo(int n){
if(n==0)
return 0;
if(n==1)
return 1;
else
return fibo(n-1)+fibo(n-2);
}
}
public class Fibonacci{
public static void main(String[] args){ 
Scanner sc=new Scanner(System.in);
System.out.println("Enter fibonacci Number :");
int n=sc.nextInt();
System.out.println("Fibonacci Series is :\n"); 
calc c=new calc(); 
for(int i=0;i<n;i++){
System.out.print("   "+c.fibo(i));
}
}
}

OUTPUT:
javac Fibonacci.java
java Fibonacci
Enter fibonacci Number:
10
0 1 1 2 3 5 8 13 21 34

Java Program to Multiply 2 matrices

In this program we will take three level nested loop is used to perform the multiplication.First, you have to ask the user to enter the number of rows and columns of the first matrix and then ask to enter the first matrix elements.Again ask the same for the second matrix. Then multiply two matrices and store the multiplication result inside the any variable say res and then print all the matrices as a result


Example:


import java.util.Scanner;
class MatrixExample
{
int m,n,p,q;
int[][] a;
int[][]b;
int[][]c;
public void readData()
{
int i,j;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the order of the matrix A");
m=sc.nextInt();
n=sc.nextInt();
System.out.println("Enter the order of the matrix B");
p=sc.nextInt();
q=sc.nextInt();
if(n!=p)
{
System.out.println("matrix order mismatch for multiplication");
System.exit(0);
}
a=new int[m][n];
b=new int[p][q];
c=new int[m][q];
System.out.println("enter the elements of Matrix A");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
System.out.printf("a[%d][%d]=",i,j);
a[i][j]=sc.nextInt();
System.out.println("\n");
}
System.out.println("enter the elements of Matrix B");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
{
System.out.printf("b[%d][%d]=",i,j);
b[i][j]=sc.nextInt();
System.out.println("\n");
}
}
public void multiply()
{
for(int i=0;i<m;i++)
for(int j=0;j<q;j++)
{
c[i][j]=0;
for(int k=0;k<n;k++)
c[i][j]+=a[i][k]*b[k][j];
}
}
public void printMatrix()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<q;j++)
System.out.printf("%d\t",c[i][j]);
System.out.println("\n");
}
}
public static void main(String []args)
{
MatrixExample obj=new MatrixExample();
obj.readData();
obj.multiply();
System.out.println("The result of multiplication is ");
obj.printMatrix();
}
}
Output:









Java Program to Print Prime Numbers

A Prime Number is a natural number  or positive integer  greater than 1 that has no positive divisors other than 1 and itself.

Examples: 2,3,5,7,11,13,15,17...

Program:


class primeNumber
{
public static void main(String args[]){
int i,s,j;
for(i=2;i<100;i++)
{
s=0;
for(j=2;j<i;j++)
{
if(i%j==0)
{
s=1;
break;
}
}
if(s==0)
{
System.out.println(i);
}
}
}
}                         

Output:
















Explanation:

The logic behind to the above program is all about to divide the number which you want to check whether it is prime or not by the number less than itself continuously and if the reminder of any of these division is zero then that entered number is not a prime.












How to create a table by using loop in java

In this post you will learn how to print a table using while loop. As we know looping statements are used to repeat a statement until the specified the condition becomes false.

Now we will see the the example of this program;

class  Table{
public static void main(String[] args) {
int a=10, b=1;
System.out.println("the table of "+a+"= ");
while(b<=10){
int c = a*b;
System.out.println(c);
b = b+1;
}
}
}

Output:
javac Table.java
java Table
the table of 10=
10*1=10
10*2=20
10*3=30
10*4=40
10*5=50
10*6=60
10*7=70
10*8=80
10*9=90
10*10=100

Basic Java Programs

In this Post we will learn fundamental Java Programs for Beginners and final year students,these programs will improve your programming skills. Let's start now

1. Java program to display a message

// Hello World example

class FirstProgram
{
public static void main(String args[])
{
System.out.print("Hello coding4world");

}
}

Output:

Hello coding4world

2. Java Program to find area of circle

class circle
{
public static void main("String args[])
{
double rad;
final double PI=3.1415;
rad=10.0;
double area=PI*rad*rad;
System.out.print("Area of circle is="+area);
}
}

Output:
Area of circle is= 314.15

3. Java Program to find area of triangle

class Triangle
{
public static void main(String args[])
{
double area,p,q,r,s;

p=3;
q=4;
r=5;
s=(p+q+r)/2;
area=Math.sqrt(s*(s-p)*(s-q)*(s-r));
System.out.print("Area of triangle is=+area);
}
}

output:

Area of trianlge is=6.0

4. Java Program to find Area of  Rectangle

import java.util.Scanner;
class AreaOfRectangle
{
public static void main(String args[])
{
int length;
int breadth;
int area;
scanner sc=new Scanner(System.in);
System.out.print("Enter the length of Rectangle");
length=sc.nextInt();
System.out.print("\nEnter the Breadth of Rectangle");
breadth=sc.nextInt();
 area=length*breadth;
System.out.print("\n Area of Rectangle:"+area);
}
}

Output:
Enter the length of Rectangle: 5
Enter the breadth of Rectangle: 6
Area of Rectangle : 30

5. Java program to find the volume of Spehere

import java.util.Scanner;
class Spehere
{
public static void main(String args[]);

{
float radius;

float volume;
Scanner sc= new Scanner(System.in);
System.out.print("enter radius of the sphere");
radius=sc.nextFloat();
volume=(float)((4*3.14*radius*radius* radius)/3;
System.out.print(\n valume of sphere is: +volume);
}
}

Output:

Enter radius of the sphere 2
Volume of sphere is: 33.493

6. Java program to covert Celsius To Fahrenheit

import java.util.Scanner;
class CelsiusToFahrenheit
{
public static void main(String args[])
{

float c;
float f;
Scanner sc=new Scanner(System.in);
System.out.print("Enter temp in centigrade:");
c=sc.nextFloat();
f=(float)((1.8*c)+32.0);
System.out.print("Temperature in Fahrenheit="+f);
}
}


Output:
Enter temp in centigrade: 100
Temperature in Fahrenheit=212

7. Java Program to find LCM

import java.util.Scanner;
class LCM
{
public static void main(String args[])

{
int a,b;
int big,small;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the value of a and b");
a=sc.nextInt();
b=sc.nextInt();
if(a>b)
{
big=a;

small=b;
}
else

{
big=b;
small=a;
}
for(int i=1;i<=big;i++)
{
if(((big*i)%small)==0)
{
int lcm=big*i;

System.out.print("The LCM is"+(lcm));
break;
}
}

}
}

Output:
Enter the value of a and b:
10
15
The LCM is 30

8. Java Program to find Hexagon

import java.util.Scanner;
class Hexagon
{
public static void main(String args[])
{
int side;

float area;
Scanner sc=new Scanner(System.in);
System.out.print("enter the Length of side:");
side=sc.nextInt();
area=(float)(3*Math.squre(3)*side*side)/2;
System.out.print("\n Area of Regular Hexagon:"+area);
}
}
Output:
Enter the length of side: 3
Area of Regular Hexagon: 23.38

9.Java Program to find Volume of Hemisphere

import java.util.Scanner;
class Hemisphere
{
public static void main(String args[]);

{
float radius;

float volume;
System.out.print("\n enter radius of the hemisphere");
radius=sc.nextFloat();
volume=(float)((2*3.14*radius)/3);
System.out.print("\n volume of hemisphere is:"+volume);
}
}
Output:
Enter radius of the hemisphere: 6

Volume of hemispere is: 12.64

10. Java program to find Volume of cone

import java.util.Scanner;
class Cone
{
public static void main(String args[])

{
float r;

float h;
float volume;
Scanner sc=new Scanner(System.in);
System.out.print("Enter radius of the cone:");
r=sc.nextFloat();
System.out.print("enter height of the Cone:");
h=sc.nextFloat();
volume=(float)((3.14*r*r*h)/3);
System.out.print("\n volume of cone is:"+volume);
}
}

Output:
Enter radius of the cone: 5

Enter height of the cone: 8
Volume of the cone is: 209.33






Basics of CPP Programs

In this post you will learn fundamental C++ programs. These programs for freshers or beginners. 1 .write a cpp program to display hello w...