0% found this document useful (0 votes)
74 views9 pages

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)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views9 pages

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)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

WAP TO PRINT HELLO WORLD IN JAVA.

PROGRAM
public class Nikhil{ public static void main(String args[]) { [Link]("Hello class"); } }

OUTPUT
C:\j2sdk1.4.2_19\bin>javac [Link] 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; } [Link]("Sum is;"+s); }}

OUTPUT
C:\j2sdk1.4.2_19\bin>javac [Link] 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; } [Link]("reverseis"+reverse); } }

OUTPUT
C:\j2sdk1.4.2_19\bin>javac [Link] 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++) { [Link](j); } [Link](); } } }

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

WAP TO PRINT FACTORIAL OF A NUMBER


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

OUTPUT
C:\j2sdk1.4.2_19\bin>javac [Link] 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; [Link]("The series is:"); [Link](a); [Link](" "+b); for(i=0;i<5;i++) { c=a+b; [Link](" "+c); a=b; b=c; } } }

Output
C:\j2sdk1.4.2_19\bin>javac [Link] 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}} [Link]("the matrix is"); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { [Link](a[i][j]+ " "); } [Link](); } } }

OUTPUT
C:\j2sdk1.4.2_19\bin>javac [Link] 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; [Link]("the matrix is"); for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { [Link](a[i][j]+ " "); sum=sum+a[i][j]; } //[Link](); }[Link]("sum is" +sum); } }

OUTPUT
C:\j2sdk1.4.2_19\bin>javac [Link] 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}; [Link]("The 3*3 matrix is:"); for(i=0;i<3;i++) { [Link](); for(j=0;j<3;j++) { y[i][j]=n++; [Link](y[i][j]+" "); //row sum srow[i]+=y[i][j]; } } [Link](); for(i=0;i<3;i++) [Link]("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++) [Link]("Sum of column " + i + "is :"+scol[i]); } }

OUTPUT
C:\j2sdk1.4.2_19\bin>javac [Link] 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