0% found this document useful (0 votes)
37 views

Lab Assign.

The document contains 10 Java programs that demonstrate various programming concepts: 1) Printing "Hello World" 2) Calculating the sum of digits in a number 3) Finding the reverse of a number 4) Printing a number pattern 5) Calculating the factorial of a number 6) Printing the Fibonacci series 7) Displaying a matrix 8) Calculating the sum of a matrix 9) Finding the row and column sums of a matrix 10) Explaining the output of each program

Uploaded by

Rohit Pawar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Lab Assign.

The document contains 10 Java programs that demonstrate various programming concepts: 1) Printing "Hello World" 2) Calculating the sum of digits in a number 3) Finding the reverse of a number 4) Printing a number pattern 5) Calculating the factorial of a number 6) Printing the Fibonacci series 7) Displaying a matrix 8) Calculating the sum of a matrix 9) Finding the row and column sums of a matrix 10) Explaining the output of each program

Uploaded by

Rohit Pawar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

WAP TO PRINT HELLO WORLD IN JAVA.

PROGRAM
public class Nikhil{ public static void main(String args[]) { System.out.println("Hello class"); } }

OUTPUT
C:\j2sdk1.4.2_19\bin>javac Nikhil.java C:\j2sdk1.4.2_19\bin>java Nikhil Hello World

WAP TO DISPLAY THE SUM OF DIGITS OF A N UMBER.


PROGRAM
public class Sum { public static void main(String args[]) {int n=1234,i,s=0; for(i=n;i>0;i=i/10) { int y=i%10; s+=y; } System.out.println("Sum is;"+s); }}

OUTPUT
C:\j2sdk1.4.2_19\bin>javac sum.java C:\j2sdk1.4.2_19\bin>java sum
Sum=10

WAP TO DISPLAY REVERSE OF A NUMBER


PROGRAM
public class reverse { public static void main(String arg[]) { int n=1234 ,temp=0, reverse=0; while(n>0) { temp=n%10; reverse=reverse*10 +temp; n=n/10; } System.out.println("reverseis"+reverse); } }

OUTPUT
C:\j2sdk1.4.2_19\bin>javac reverse.java C:\j2sdk1.4.2_19\bin>java reverse Reverse is: 4321

WAP TO DISPLAY THE PATTERN:


1 12 123 1234

PROGRAM
public class pattern{ public static void main(String args[]){ int i,j; for(i=1;i<=4;i++) { for(j=1;j<=i;j++) { System.out.print(j); } System.out.println(); } } }

Output:
C:\j2sdk1.4.2_19\bin>javac pattern.java C:\j2sdk1.4.2_19\bin>java pattern 1 12 123 1234

WAP TO PRINT FACTORIAL OF A NUMBER


PROGRAM
import java.io.*; public class Fact{ public static void main(String arg[]) { int n=6, f=1,i; for(i=n; i>=1;i--) { f=i*f; } System.out.println("The factorial of the no is:"+f); } }

OUTPUT
C:\j2sdk1.4.2_19\bin>javac Fact.java C:\j2sdk1.4.2_19\bin>java Fact The factorial of 6 is: 720

WAP TO PRINT FIBONACCI SERIES


PROGRAM
public class fibonacci{ public static void main(String args[]) { int a=0,b=1,c,i; System.out.println("The series is:"); System.out.print(a); System.out.print(" "+b); for(i=0;i<5;i++) { c=a+b; System.out.print(" "+c); a=b; b=c; } } }

Output
C:\j2sdk1.4.2_19\bin>javac finonacci.java C:\j2sdk1.4.2_19\bin>java finonacci The series is: 0112358

WAP FOR MATRIX DISPLAY:

PROGRAM
class matrixdisplay { public static void main(String args[]) { int a[][]={{1,2,3},{4,5,6},{7,8,9}} System.out.println("the matrix is"); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { System.out.print(a[i][j]+ " "); } System.out.println(); } } }

OUTPUT
C:\j2sdk1.4.2_19\bin>javac matrixdisplay.java C:\j2sdk1.4.2_19\bin>java matrixdisplay The matrix is: 1 2 3 4 5 6 7 8 9

WAP FOR MATRIX SUM:

PROGRAM
class matrixadd { public static void main(String args[]) { int a[][]={{1,2,3},{4,5,6},{7,8,9}}; int sum=0; System.out.println("the matrix is"); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { System.out.print(a[i][j]+ " "); sum=sum+a[i][j]; } //System.out.println(); }System.out.println("sum is" +sum); } }

OUTPUT
C:\j2sdk1.4.2_19\bin>javac matrixadd.java C:\j2sdk1.4.2_19\bin>java matrixadd

The matrix is: 1 2 3 4 5 6 7 8 9 Sum is: 45

WAP TO DISPLAY ROW AND COLUMN SUM OF A MATRIX:

PROGRAM
public class rowcolumnadd{ public static void main(String args[]){ int i,j,n=1; int y[][]=new int[3][3]; int srow[]={0,0,0}; int scol[]={0,0,0}; System.out.println("The 3*3 matrix is:"); for(i=0;i<3;i++) { System.out.println(); for(j=0;j<3;j++) { y[i][j]=n++; System.out.print(y[i][j]+" "); //row sum srow[i]+=y[i][j]; } } System.out.println(); for(i=0;i<3;i++) System.out.println("Sum of row " + i + "is :"+srow[i]);

// column sum for(j=0;j<3;j++) { for(i=0;i<3;i++) scol[i]+=y[i][j]; } for(i=0;i<3;i++) System.out.println("Sum of column " + i + "is :"+scol[i]); } }

OUTPUT
C:\j2sdk1.4.2_19\bin>javac rowcolumnadd.java C:\j2sdk1.4.2_19\bin>java rowcolumnadd The 3*3 matrix is: 1 2 3 4 5 6 7 8 9 Sum of row 0 is: 6 Sum of row 1 is: 15 Sum of row 2 is: 24 Sum of column 0 is: 12 Sum of column 1 is: 15 Sum of column 2 is: 18

You might also like