Program to print numbers from 1 to N using for loop PROGRAM: /*Syntax for(initial expression;condition;update expression) { } */ //PROGRAM TO PRINT THE NUMBERS FROM 1 TO N: import java . util .*; public class forloop { public static void main ( String args []) { Scanner sc = new Scanner ( System . in ); int n = sc . nextInt (); for ( int i = 1 ; i <= n ; i ++) { System . out . println ( i ); } } } //I/p: // 5 //O/P: // 1 // 2 // 3 // 4 // 5
Program to Copy One Array to Another using Assignment operator Program: public class CopyArrayAssignment { public static void main ( String args []) { int arr []={ 1 , 2 , 3 , 4 , 5 } ; int copyarr []= arr ; for ( int i : copyarr ) { System . out . println ( i ) ; } } } // 1 // 2 // 3 // 4 // 5
PROGRAM TO FIND FACTORIAL OF A NUMBER USING RECURSION I/P: 3 O/P: 6 SOLUTION: package N umbers ; import java . util .*; public class Factorial { static int factorial ( int n ) { return ( n > 1 ) ? n * factorial ( n - 1 ) : 1 ; } public static void main ( String [] args ) { Scanner sc = new Scanner ( System . in ); int n = sc . nextInt (); int result ...
Comments
Post a Comment