What is a CLASS & OBJECT?
How do you differentiate these two ? Explain with examples.
A class is a template, blueprint, plan,design or protype from wichObjects can be derived
If you create an object from a class , it means you created an instance of a class.
A SMALL EXAMPLE IN MY VIEW.....
Suppose if you are planning to construct a house first step what you will do ? you will go for design or plan of it, Once plan is ready you will start actual construction. By keeping this plan with you, construct one more house ,like wise you can construct any number of houses. Now here this plan is a class ,and actual construction is object. this class doesn't occupy any memory only object occupies memory. From one class we can create ‘N’ number of objects.
Few points on Structured programming and Object oriented programming?
DESIGNER OF PASCAL LANGUAGE NIKLAUS WIRTH ONCE SAID
"ALGORITHMS+DATASTRUCTURES=PROGRAMS".
HERE ALGORITHM COMES FIRST AND DATA STRUCTURES COMES SECOND.THIS IS HOW PROGRAMMERS WERE THINKING AT THAT TIME .FIRST THEY DECIDED THE PROCEDURES FOR MANIPULATIONS OF DATA, THEN THEY DECIDED WHAT STRUCTURE TO IMPOSE ON DATA TO MAKE MANIPULATIONS EASIER.OOP REVERSES THE ORDER AND PUTS DATA FIRST, THEN LOOKS AT THE ALGORITHM THAT OPERATE ON THE DATA. FOR SMALL PROGRAMS PROCEDURES WORKS WELL, BUT OBJECTS ARE BETTER FOR LARGER PROGRAMS. DEBUGGING ALSO EASIER WITH OOP.
Goals of JAVA!
What is JAVA ?
JAVA IS A PROGRAMMING LANGUAGE.
AS A WHOLE JAVA IS A PLATFORM, WITH LARGE LIBRARY, CONTAINING LOTS OF REUSABLE CODE, AND AN EXECUTION ENVIRONMENT THAT PROVIDE SERVICES SUCH AS SECURITY, PORTABILITY ACROSS SYSTEMS AND AUTOMATIC GARBAGE COLLECTION.
JAVA IS A HIGH QUALITY ENVIRONMENT AND LIBRARY.
History of JAVA
They divided it in to 3 parts
and now it is called
1.JSE 2.JEE 3.JME
What is Programming?
NOW A DAY COMPUTERS BECAME PART OF HUMAN LIFE .MANY PEOPLE USES IT FOR DIFFERENT PURPOSES LIKE PLAYING GAMES, BANKING TRANSACTIONS, AND FOR ONLINE PURCHASES. THESE COMPUTERS HAS NO INTELLIGENCE BY ITSELF, IT SIMPLY EXECUTES INSTRUCTIONS THAT HAVE BEEN PREPARED IN ADVANCE. IN ORDER TO ACHIEVE THIS FLEXIBILITY THE COMPUTERS MUST BE PROGRAMMED TO PERFORM EACH TASK, DIFFERENT TASKS REQUIRE DIFFERENT PROGRAMS. THESE PROGRAMS ARE SEQUENCE OF INSTRUCTIONS AND DECISION THAT COMPUTERS CARRYOUT TO ACHIEVE A TASK. THE ART OF WRITING THESE KINDS OF PROGRAMS IS NOTHING BUT PROGRAMMING.
Simple Way to learn Java Programing in two Days
STRINGS BASIC PROGRAM SERIES-1
package com.javainchennai;import java.util.Scanner;public class StringReverse {public static void main(String[] args) {Scanner sc=new Scanner(System.in);System.out.println("Enter a String");String str=sc.nextLine();int strLen=str.length();char c[]=new char[20];for(int i=0,j=strLen-1;i<strLen;i++,j--){c[i]=str.charAt(j);System.out.print(c[i]);}}}OUTPUT:-Enter a Stringmadam123321madampackage com.javainchennai;import java.util.Scanner;public class Stringpolindrome {public static void main(String[] args) {int i,j=0;Scanner sc=new Scanner(System.in);System.out.println("Enter your String:-");String str=sc.next();char c[]=new char[20];c=str.toCharArray();i=str.length();--i;while(j<=i){if(c[i]!=c[j]){System.out.println("The given string "+ str +" is not polindrome");System.exit(0);}++j;--i;}System.out.println("The given string" + str +"is polindrome");}}OUTPUT-1 :Enter your String:-javainchennaiThe given string javainchennai is not polindromeOUTPUT-2 :Enter your String:-malayalamThe given string malayalam is polindrome
FINDING THE LENGTH OF A STRINGpackage com.javainchennai;import java.util.Scanner;public class StringLength {public static void main(String[] args) {Scanner sc=new Scanner(System.in);System.out.println("Please Enter your String:-");String str=sc.nextLine();char c[]=new char[10];c= str.toCharArray();int count=0;try{for( count=0;c[count] !='\0'; count++);}catch (ArrayIndexOutOfBoundsException e) {System.out.println(count);}}}Please Enter your String:-questionabanksThe Length of the Given String is: 13
FINDING THE NUMBER OF WORDS IN A STRINGpackage com.javainchennai;import java.util.Scanner;public class NoOfWordsInAString {public static void main(String[] args) {Scanner sc=new Scanner(System.in);System.out.println("Enter your string:-");String str=sc.nextLine();int strLen=str.length();int word=1;for(int i=0;i<strLen-1;i++){if((str.charAt(i))==' '){word++;}}System.out.print("The Number of words in the Given String are :" +word);}}OUTPUT:-Enter your string:-question banksThe Number of words in the Given String are :2
--------------------------------------------------------------------------------------------------------------
Before going into programs a small introduction on StringsIf there is any mistakes in this article please write me blog4helping@gmail.com======================-:STRINGS:-======================1.String is a class in Java2.String means collection of characters3.String is a sequence of characters enclosed in double Quotes.4.Java Strings are sequence of Unicode characters5.String class produces Immutable objects.6.String class does not have a methods that let you change a characters in an existing String.7.It is like number 5 , the String "HELLO" we cannot change it.Sting Objects can be created in 2 ways1. without using new operatorString str1="javainchennai";2. with using new operatorString str2=new String("javainchennai");The memory space is divided in to 2 parts1.constant area2.non-constant areaIf you create object without using new operator this will be saved in constant areaString str1="javainchennai";If you create object with using new operator this will be saved in non-constant areaString str2=new String("javainchennai");Here Constant area will not allow duplicateswhere as non-constant area will allow duplicates.Do Remember inside a String class the toString() method is overidedHere if you try to print reference variableString s1="javainchennai";System.out.println(s1);The output is javainchennai instead of String@ff2201(classname @ hexadecimal value)equals() method also overrided to check 2 objects based on the value(content) it containsobserve the program carefully and read the explanation:-1.package com.qb;2.public class StringTest {3. public static void main(String[] args) {4. String str1="javainchennai";5. String str2="javainchennai";6. String str3=new String("javainchennai");7. String str4=new String("javainchennai");8. System.out.println(str1==str2);9. System.out.println(str1==str3);10. System.out.println(str2==str3);11. System.out.println(str3==str4);12. System.out.println(str1.equals(str2));13. System.out.println(str1.equals(str3));14. System.out.println(str2.equals(str3));15. System.out.println(str3.equals(str4));16. }17. }output:-truefalsefalsefalsetruetruetruetrueexplanation:-If you say 5==5 is true, here two are primitive literals so == operator(checks content) prints true.If you use == operator to compare two object references it will see whether both references are pointing to same object or different.If both references are pointing to same object it(== operator) returns true otherwise false.observe line4 and line5 ,here actually I created String object without using new operator so, str1 and str2 both references are pointing to same object (javainchennai). (constant area will not allow duplicates)so, str1==str2 returns true.observe line4 and line5 ,here actually I created String object by using new operator so, str3 and str4 both references are pointing to different objects (javainchennai).(non constant area allows duplicates).So, str3==str4 returns false.Observe the same effect with equals() method it returns true all time .(because it is overrided to compare based on content inside object)PROGRAMS:-program:-1.public class StringTest {2. public static void main(String[] args) {3. System.out.println(3+5);4. System.out.println("3"+5);5. System.out.println(3+"555");6. System.out.println(3+2+5+"4444");7. System.out.println("35"+111+"99");8. System.out.println("42"+11+32+"77");9. System.out.println("100"+"222");}}OUTPUT:-8353555104444351119942113277100222Explanation:--------------In Java there is no operator overloading concept.+ is the only one operator overloaded to add 2 integers as well as 2 stringsIn the above example we added 2 primitives that is common ,when we added String with any primitives only String object is produced.observe the output of lines 6,7,8,9.
DIFFERENCES BETWEEN STRING , STRINGBUFFER AND STRINGBUILDER
String:-
------------------1.String class introduced in jdk1.02.String class is a final class and implements Serializable,Comparable and CharSequence interfaces3.String Objects can be created in two ways a.Using new operatorconstructors to create String obb.Without using new operator 4.----- 5. String class has several jects(both using new or without using new operator) a.String s1="";that it represents an empty charb.String s2=new String(""); Initializes a String object so acter sequence. c.String s1="javainchenna";Initializes a String object so that it re p is no delete method to delete a characters from . 7.StringBuilder objects are Non-Synchronized. ------------------5.String class has no append method.
resents the same sequence of characters as the argument
6.ThereStringBuffer:-------------------StringBuffer and StringBuilder has the same methods:-1.String class introduced in jdk1.02.String class is a final class and im pated without using new operator StringBuffer s2="javainchennai";(wrong way)
lements Serializable and CharSequence interfaces
3
.StringBuffer Objects cannot be cre4.use new operator to create objects of StringBuffer StringBuffer sb1=new StringB u characters in it and an initial capacity of 16 characters.
ffer("");
Constructs a StringBuffer with no5.StringBuffer class has append() method , It appends the stri nobject. 7.StringBuffer objects are Synchronized.StringBuffer was being used by a single thread. ------------------
g representation of the any type(primitive or derived) argument to the sequence.
6
.There is a delete method to delete a characters from thisStringBuilder:-------------------.String class introduced in jdk1.51,2. S.StringBuffer Objects cannot be created without using new operator
tring class is a final class and implements Serializable and CharSequence interfaces
3StringBuilder s3="javainchennai";(wrong way) 4.use new opera tgBuilder sb1=new StringBuilder();
or to create objects of StringBuilder
StrinConstructs a StringBuilder w id() method,It appends the string representation of the any type(primitive or derived) argument to the sequence.
th no characters in it and an initial capacity of 16 characters.
5.Str i
ngBuilder class has append.6. There is a delete method to delete a characters from this object. 7.StringBuilder objects are Non-Synchronize
--------------------------------------------------------------------------------------------------------------
FIND BIGGEST OF 3 NUMBERS:import java.util.Scanner;public class biggest{public static void main(String[] args){System.out.println("Enter 3 numbers");Scanner sc=new Scanner(System.in);int a=sc.nextInt();int b=sc.nextInt();int c=sc.nextInt();int max=a;if(b>max)max=b;if(c>max)max=c;System.out.println("The biggest of 3 numbers:"+max);}}Output:Enter 3 numbers659843The biggest of 3 numbers:98
FIND GIVEN NUMBER IS ODD OR EVEN:import java.util.Scanner;public class evenodd{public static void main(String[] args){System.out.println("Enter the number");Scanner sc=new Scanner(System.in);int num=sc.nextInt();if(num/2==1)System.out.println("Number is ODD");elseSystem.out.println("Number is EVEN");}}Output:Enter the number88Number is EVEN
FIND GIVEN NUMBER IN PERFECT SQUARE OR NOT:import java.util.Scanner;public class perfectsquare{public static void main(String[] args){System.out.println("Enter a number");Scanner sc=new Scanner(System.in);int num=sc.nextInt();double num1=Math.sqrt(num);double num2=num1;if(num1==num2)System.out.println(num + "is a perfect square");elseSystem.out.println(num + "is not a perfect square");}}Output:Enter a number100100is a perfect square
WRITE A PROGRAM TO FIND GIVEN NUMBER IS PALINDROME OR NOTimport java.util.Scanner;public class PalindromeOfNumber{public static void main (String[] args){int mod=0;int c=0;int d=0;int num1=0;System.out.println("Enter a number:");Scanner sc=new Scanner(System.in);int num=sc.nextInt();num1=num;while(num>0){mod=num%10;c=c+mod;num=num/10;d=mod+d*10;}if(num1==d){ System.out.println("The given number "+num1+" is palindrome");}else{System.out.println("The given number "+num1+" is not palindrome");}}}OUTPUT:RUN 1:Enter a number:1523The given number 1523 is not palindrome
WRITE A PROGRAM TO FIND GIVEN NUMBER IS PRIME OR NOTimport java.util.Scanner;public class PrimeOrNot{public static void main(String[] args){System.out.println("Enter the number:");Scanner sc=new Scanner(System.in);int i;int flag=0;long num=sc.nextLong();if(num<=3)System.out.println("The given number is prime:");elsefor(i=2;i<=num/2;i++)if(num%i==0){flag=1;break;}elseflag=0;if(flag==1)System.out.println("The given number "+num+" is not prime");elseSystem.out.println("The given number "+num+" is prime");}}Output1:Enter the number:5The given number 5 is primeOutput2:Enter the number:55The given number 55 is not prime
WRITE A PROGRAM TO DISPLAY MULTIPLICATION TABLEimport java.util.Scanner;public class multiplicationtable{public static void main(String[] args){System.out.println("Enter the table number and the count value:");Scanner sc=new Scanner(System.in);int tnum=sc.nextInt();int count=sc.nextInt();System.out.println("The table is ...");for(int i=1;i<=count;i++){System.out.println(tnum+"*"+i+"=" + i*tnum);}}}Output:Enter the table number and the count value:810The table is ...8*1=88*2=168*3=248*4=328*5=408*6=488*7=568*8=648*9=728*10=80
WRITE A PROGRAM TO FIND COMMAND LINE NUMBER COUNTpublic class CommandLineNumbersCount{public static void main(String[] args){int l=args.length;System.out.println(l);}}OUT PUT:Java CommandLineNumbersCount 10 5 23 4 25java CommandLineNumbersCount 21 3 36 44
WRITE A PROGRAM TO SORT THE DIGITS OF A NUMBER IN ASCENDING ORDERimport java.util.Scanner;public class SortDigitsInInteger{public static void main(String[] args){Scanner sc=new Scanner(System.in);System.out.println("Enter a number:-");int num=sc.nextInt();int a,i=0;int sort[]=new int[10];while(num>0){a=num%10;sort[i]=a;i++;num=num/10;}for(int j=0;j<i-1;j++)for(int k=j+1;k<i;k++)if(sort[j]>sort[k]){int temp=sort[j];sort[j]=sort[k];sort[k]=temp;}System.out.println("The digits in number ascending order:");for(int j=0;j<i;j++)System.out.print(sort[j]+" ");}}OUTPUT:Enter a number:-4261The digits in number ascending order:1 2 4 6
WRITE A PROGRAM TO SORT A NUMBER IN ASCENDING ORDERimport java.util.Scanner;public class SortNumbersInAscendingOrder{public static void main(String[] args){Scanner sc=new Scanner(System.in);System.out.println("Enter Number of elements:-");int limit=sc.nextInt();System.out.println(" Enter Elements:- ");int a[]=new int[10];for(int i=1;i<=limit;i++){a[i]=sc.nextInt();}for(int i=0;i<limit-1;i++)for(int j=i;j<limit;j++)if(a[i]>a[j]){int temp=a[i];a[i]=a[j];a[j]=temp;}System.out.println("The sorted list is:-");for( int i=0;i<limit;i++)System.out.print(" "+a[i]);}}OUTPUT:Enter Number of elements:-5Enter Elements:-15 25 16 12 4The sorted list is:-0 12 15 16 25
-------------------------------------------------------------------------------------------------
PROGRAMMING WITH RECURSION
Fibonacci seriesimport java.util.Scanner;public class FibonacciSeries{public static void main(String[] args){int x=0,y=1;Scanner sc=new Scanner(System.in);System.out.println("Enter a Number:-");int z=sc.nextInt();System.out.println("The fibo series is:- ");fiboSeries(z,x,y);}private static void fiboSeries(int x,int f,int f1){if(x==0)System.exit(0);System.out.print(" " + (f+f1));fiboSeries(--x,f1,f+f1);}}OUTPUTEnter a Number:-8The fibo series is:-1 2 3 5 8 13 21 34Enter a Number:-10The fibo series is:-1 2 3 5 8 13 21 34 55 89
Tribonacci series
mport java.util.Scanner;i
ublic class TribonacciSeriesp{public static void main(String[] args){int x=-1,y=1,z=0;Scanner sc=new Scanner(System.in);System.out.println("Enter a Number:-");int n=sc.nextInt();System.out.println("The tribo series is:- ");triboSeries(n,z,x,y);}private static void triboSeries(int x,int f,int f1,int f2){if(x==0)System.exit(0);System.out.print(" " + (f+f1+f2));triboSeries(--x,f1,f2,f+f1+f2);}}OUTPUTEnter a Number:-10The tribo series is:-0 0 1 1 2 4 7 13 24 44Enter a Number:-8The tribo series is:-0 0 1 1 2 4 7 13WRITE A PROGRAM TO FIND THE POWER OF THE NUMBERimport java.util.Scanner;public class XpowN{public static void main(String[] args){Scanner sc=new Scanner(System.in);System.out.println("Enter the values of X and N:-");double x=sc.nextDouble();int n=sc.nextInt();System.out.println(power(x,n));}private static double power(double x,int n){double pow=0;pow= ((n==0)?1:x*power(x,n-1));return pow;}}OUTPUTEnter the values of X and N:-105100000.0Enter the values of X and N:-5 225.0
--------------------------------------------------------------------------------------------------------------
PROGRAM TO READ AN INTEGER AND REDUCE IT TO A SINGLE DIGITimport java.util.Scanner;public class DigitReduce{public static void main(String[] args){Scanner sc=new Scanner(System.in);System.out.println("Enter a Number:-");long num=sc.nextLong();System.out.println(" " + calculateDigit(num));}private static long calculateDigit(long num){return (num/10 == 0 ? num : calculateDigit(num/10 + num % 10));}}OUTPUTEnter a Number:-47891Enter a Number:-33489
WRITE A PROGRAM TO DISPLAY AMOUNT INTO 500’S,100’S,50’S,20’S,10’S,5’S,2’S,1’S IN A GIVEN NUMBERimport java.util.Scanner;public class AmountCount{public static void main(String[] args){System.out.println("Enter the amount in rupees:-");Scanner sc=new Scanner(System.in);int num=sc.nextInt();while(num>=500){int a=num/500;System.out.print("\n The no .of five hundreds are:- "+ a);break;}while(num>=100){int a=num/100;System.out.print("\n The no .of hundreds are :-"+ a);break;}while(num>=50){int a=num/50;System.out.print("\n The no .of fiftie's are "+ a);break;}while(num>=20){int a=num/20;System.out.print("\n The no .of twenty's are "+ a);break;}while(num>=10){int a=num/10;System.out.print("\n The no .of ten's are "+ a);break;}while(num>=5){int a=num/5;System.out.print("\n The no .of five's are "+ a);break;}while(num>=2){int a=num/2;System.out.print("\n The no .of two's are "+ a);break;}while(num>=1){int a=num/1;System.out.print("\n The no.of one's are"+ a);break;}}}OUTPUT:Enter the amount in rupees:-1854The no .of five hundreds are 3The no .of hundreds are 18The no .of fiftie's are 37The no .of twenty's are 92The no .of ten's are 185The no .of five's are 370The no .of two's are 927The no.of one's are1854
WRITE A PROGRAM TO SORT A NUMBER IN ASCENDING ORDERimport java.util.Scanner;public class SortNumbersInAscendingOrder{public static void main(String[] args){Scanner sc=new Scanner(System.in);System.out.println("Enter Number of elements:-");int limit=sc.nextInt();System.out.println(" Enter Elements:- ");int a[]=new int[10];for(int i=1;i<=limit;i++){a[i]=sc.nextInt();}for(int i=0;i<limit-1;i++)for(int j=i;j<limit;j++)if(a[i]>a[j]){int temp=a[i];a[i]=a[j];a[j]=temp;}System.out.println("The sorted list is:-");for( int i=0;i<limit;i++)System.out.print(" "+a[i]);}}OUTPUT:Enter Number of elements:-5Enter Elements:-15 25 16 12 4The sorted list is:-0 12 15 16 25
--------------------------------------------------------------------------------------------------------------
WRITE A PROGRAM TO CONVERT INTEGER INTO IT’S EQUIVALENT NAMEimport java.util.Scanner;public class IntegerToItsEqualentName{public static void main(String[] args){Scanner sc=new Scanner(System.in);System.out.println("Enter Number");int num=sc.nextInt();int m=0,c=0;int a[]=new int[10];while(num>0){m=num%10;a[c]=m;num=num/10;c++;}for(int k=c-1;k>=0;k--){switch (a[k]){case 0:System.out.print("ZERO ");break;case 1:System.out.print("ONE ");break;case 2:System.out.print("TWO ");break;case 3:System.out.print("THREE ");break;case 4:System.out.print("FOUR ");break;case 5:System.out.print("FIVE ");break;case 6:System.out.print("SIX ");break;case 7:System.out.print("SEVEN ");break;case 8:System.out.print("EIGHT ");break;case 9:System.out.print("NINE ");break;default:break;}}}}OUTPUT:Enter Number12069ONE TWO ZERO SIX NINE
PROGRAM FOR SWAPPING TWO NUMBERS USING THREE VARIABLESimport java.util.Scanner;public class Output{public static void main(String[] args){System.out.println ("enter 2 numbers");Scanner sc=new Scanner(System. in);int num1 =sc.nextInt();int num2 =sc.nextInt();System.out.println("Before swapping");System.out.println("num1:"+num1+"\n"+"num2:"+num2);int temp=num1;num1=num2;num2=temp;System.out.println("After swapping");System.out.println("num1:"+num1+"\n"+"num2:"+num2);}}Output:enter 2 numbers5565Before swappingnum1:55num2:65After swappingnum1:65num2:55
PROGRAM FOR SWAPPING TWO NUMBERS WITHOUT USING THIRD VARIABLEimport java.util.Scanner;public class swapping2{public static void main(String args[]){System.out.println("Enter 2 numbers");Scanner sc=new Scanner(System.in);int num1=sc.nextInt();int num2=sc.nextInt();System.out.println("Before swapping");System.out.println("num1:"+num1+"\n"+"num2:"+num2);num1=num1+num2;num2=num1-num2;num1=num1-num2;System.out.println("After swapping");System.out.println("num1:"+num1+"\n"+"num2:"+num2);}}Output:Enter 2 numbers2332Before swappingnum1:23num2:32After swappingnum1:32num2:23
SWAPPING 2 NUMBERS WITHOUT USING ARITHMETIC OPERATORSimport java.util.Scanner;public class arithmatic{public static void main(String[] args){System.out.println("Enter 2 numbers");Scanner sc=new Scanner(System.in);int num1=sc.nextInt();int num2=sc.nextInt();System.out.println("Before swapping");System.out.println("num1:"+num1+"\n"+"num2:"+num2);num1=num1^num2;num2=num1^num2;num1=num1^num2;System.out.println("After swapping");System.out.println("num1:"+num1+"\n"+"num2:"+num2);}}Output:Enter 2 numbers1991Before swappingnum1:19num2:91After swappingnum1:91num2:19
--------------------------------------------------------------------------------------------------------------
WHETHER IT IS C , C++, C# OR JAVA , WORKING WITH THESE KIND OF PROGRAMS GIVES YOU LOT OF FUN :-
Write a program to display the following output12 23 3 34 4 4 45 5 5 5 5import java.util.Scanner;public class ForLoop1{public static void main(String[] args){System.out.println("Enter the limit:-");Scanner sc=new Scanner(System.in);int limit=sc.nextInt();for(int i=1;i<=limit;i++){for(int j=1;j<=i;j++){System.out.print( i +" ");}System.out.println();}}}Enter the limit:-512 23 3 34 4 4 45 5 5 5 5
Write a program to display the following output11 21 2 31 2 3 41 2 3 4 5package com.vel.nestedforloop;import java.util.Scanner;public class ForLoop2{public static void main(String[] args){System.out.println("Enter the limit:-");Scanner sc=new Scanner(System.in);int limit=sc.nextInt();for(int i=1;i<=limit;i++){for(int j=1;j<=i;j++){System.out.print( j +" ");}System.out.println();}}}Enter the limit:-511 21 2 31 2 3 41 2 3 4 5
Write a program to display the following output12 34 5 67 8 9 1011 12 13 14 1516 17 18 19 20 21package com.vel.nestedforloop;import java.util.Scanner;public class ForLoop3{public static void main(String[] args){System.out.println("Enter the limit:-");Scanner sc=new Scanner(System.in);int limit=sc.nextInt();int k=1;for(int i=1;i<=limit+1;i++){for(int j=1;j<=i;j++){System.out.print( k++ +" ");}System.out.println();}}}Enter the limit:-512 34 5 67 8 9 1011 12 13 14 1516 17 18 19 20 21
WRITE A PROGRAM TO DISPLAY THE FOLLOWING OUTPUT3 2 1 0 1 2 3import java.util.Scanner;public class LineFormat{public static void main(String[] args){int i,j,k;System.out.println("Enter Limit:-");Scanner sc= new Scanner(System.in);int n=sc.nextInt();k=n;for(i=0;n>=i;i++,k--)System.out.print(" "+k);k=1;for(j=0;j<n;j++,k++)System.out.print(" "+k);}}OUTPUTEnter Limit:-33 2 1 0 1 2 3
Write a program to display the following output11 11 1 1import java.util.Scanner;public class NumberGenerator{public static void main(String[] args){int i,j;System.out.println("ENTER THE NUMBER TO BE DISPLAYED :-");Scanner sc=new Scanner(System.in);int a=sc.nextInt();System.out.println("ENTER THE SIZE :-");int n=sc.nextInt();for(i=0;i<n;i++){for(j=0;j<=i;j++)System.out.print(" "+a);System.out.println();}}}ENTER THE NUMBER TO BE DISPLAYED :-1ENTER THE SIZE :-311 11 1 1
WRITE A PROGRAM TO DISPLAY THE FOLLOWING OUTPUTENTER THE LIMIT:-612 13 2 14 3 2 15 4 3 2 1import java.util.Scanner;public class GenerateNumber1{public static void main(String[] args){int i,j;System.out.println( "ENTER THE LIMIT:-");Scanner sc=new Scanner(System.in);int n=sc.nextInt();for(i=1;i<n;i++){for(j=i;j>0;j--){System.out.print(j+" ");}System.out.println();}}}OUTPUTENTER THE LIMIT:-612 13 2 14 3 2 15 4 3 2 1
Write a program to print the following output1 0 0 00 2 0 00 0 3 00 0 0 4import java.util.Scanner;public class GenerateNumber1{public static void main(String[] args){int i,j,x=0;System.out.println( "ENTER THE LIMIT:-");Scanner sc=new Scanner(System.in);int n=sc.nextInt();for(i=1;i<=n;i++){for(j=1;j<=n;j++){if(i==j)System.out.print(i+" ");elseSystem.out.print(x +" ");}System.out.println();}}}
Write a program to print the following output with the given characterENTER THE CHARACTER TO BE DISPLAYED:-7ENTER THE LIMIT:-47 7 7 77 7 7 77 7 7 77 7 7 7import java.util.Scanner;public class GenerateNumber1{public static void main(String[] args){int i,j,x=0;Scanner sc=new Scanner(System.in);System.out.println("ENTER THE CHARACTER TO BE DISPLAYED:-");int c=sc.nextInt();System.out.println( "ENTER THE LIMIT:-");int n=sc.nextInt();for(i=1;i<=n;i++){for(j=1;j<=n;j++){System.out.print(c+" ");}System.out.println();}}}
.Write a program to print the following output with the given characterENTER THE CHARACTER TO BE DISPLAYED:-8ENTER THE LIMIT:-48 8 8 88 88 88 8 8 8import java.util.Scanner;public class GenerateNumber1{public static void main(String[] args){int i,j,x=0;Scanner sc=new Scanner(System.in);System.out.println("ENTER THE CHARACTER TO BE DISPLAYED:-");int c=sc.nextInt();System.out.println( "ENTER THE LIMIT:-");int n=sc.nextInt();for(i=1;i<=n;i++){for(j=1;j<=n;j++){if(i==1||j==1||j==n||i==n)System.out.print(c+" ");elseSystem.out.print(" ");}System.out.println();}}}
Program to read an integer and reduce it to a single digitimport java.util.Scanner;public class DigitReduce{public static void main(String[] args){Scanner sc=new Scanner(System.in);System.out.println("Enter a Number:-");long num=sc.nextLong();System.out.println(" " + calculateDigit(num));}private static long calculateDigit(long num){return (num/10 == 0 ? num : calculateDigit(num/10 + num % 10));}}OUTPUTEnter a Number:-47891Enter a Number:-33489
1 comments:
Dear All
If there is any error in above program or wanna download full article please request us via sms or contact form :)
Wish you All the best friends and professors
Post a Comment