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

Comp

The program accepts the number of lines for an upper and lower triangle from the user. It prints the upper triangle with asterisks, then a line of dashes, and the lower triangle with the specified character. It demonstrates printing triangle patterns by iterating through loops.

Uploaded by

Nabaneet Roy
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)
35 views

Comp

The program accepts the number of lines for an upper and lower triangle from the user. It prints the upper triangle with asterisks, then a line of dashes, and the lower triangle with the specified character. It demonstrates printing triangle patterns by iterating through loops.

Uploaded by

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

PROGRAM LISTING

//To find the hcf and lcm of n numbers import java.util.*; class HcfLcm { public void acceptData() { Scanner sc = new Scanner(System.in); System.out.println("Enter no. of numbers"); int n=sc.nextInt(); int arr[] = new int[n]; for(int i=0;i<n;i++) { System.out.println("Enter a number"); arr[i] = sc.nextInt(); } int h = arr[0]; int l = arr[0]; for(int j=1;j<n;j++) { h=hcf(h,arr[j]); l=lcm(l,arr[j]); } print(h,l); }//end of acceptData() public int hcf(int a,int b) { int r=0; int c=(a<b)?a:b;

PAGE NO:

PAGE NO:

for (int i=1;i<=c;i++) { if(a%i==0&&b%i==0) r=i; } return r; }//end of hcf() public int lcm(int a,int b) { int h = hcf(a,b); int r = (a*b)/h; return r; }//end of lcm() public void print(int h,int l) { System.out.println("The H.C.F. of the given numbers is "+h); System.out.println("The L.C.M. of the given numbers is "+l); }//end of point() }//end of class HcfLcm public class HcfLcmMain { public static void main(String args[]) { HcfLcm obj = new HcfLcm(); obj.acceptData(); }//end of main() }//end of class HcfLcmMain

SAMPLE OUTPUT
Enter no. of numbers

PAGE NO:

4 Enter a number 32 Enter a number 24 Enter a number 96 Enter a number 128 The H.C.F. of the given numbers is 8 The L.C.M. of the given numbers is 384

PROGRAM LISTING
//To find all the twin prime numbers in a given range import java.util.*;

PAGE NO:

class TwinPrimeRange { public void get() { Scanner sc = new Scanner(System.in); System.out.println("Enter lower and upper limit of range"); int l = sc.nextInt(); int u = sc.nextInt(); show(l,u); }//end of get() public void show(int l,int u) { int cn=0; for(int i=l;i<=u-2;i++) { if(checkTwinPrime(i)==true) { System.out.println(i+" and "+(i+2)+" are twin prime"); cn++; } } if (cn==0) System.out.println("There are no twin prime numbers in the given range"); }//end of show()

public boolean checkTwinPrime(int a) { if(checkPrime(a)==true) {

PAGE NO:

if(checkPrime(a+2)==true) return true; } return false; }//end of checkTwinPrime() public boolean checkPrime(int n) { int c=0; for(int i=1;i<=(n/2);i++) { if (n%i==0) { c=c+1; } } if (c==1) return true; else return false; }//end of checkPrime() }//end of class TwinPrimeRange

P.T.O. public class TwinPrimeRangeMain { public static void main(String args[]) { TwinPrimeRange obj = new TwinPrimeRange(); PAGE NO:

obj.get(); }//end of main() }//end of class TwinPrimeRangeMain

SAMPLE OUTPUT
Enter lower and upper limit of range 1 60 3 and 5 are twin prime 5 and 7 are twin prime

PAGE NO:

11 and 13 are twin prime 17 and 19 are twin prime 29 and 31 are twin prime 41 and 43 are twin prime

PROGRAM LISTING
//To perform linear search of an element in an array import java.util.*; class LinearSearch { private int arr[]; private int n;

PAGE NO:

public void get() { Scanner sc = new Scanner(System.in); System.out.println("Enter size"); int s = sc.nextInt(); arr = new int [s]; for(int i=0;i<s;i++) { System.out.println("Enter element"); arr[i]=sc.nextInt(); } System.out.println("Enter the number two be searched number"); n = sc.nextInt(); search(); }//end of get() public void search() { int c=0; for(int i=0;i<arr.length;i++) {

if(n==arr[i]) { c++; showPos(i+1); } } showOcc(c); }//end of search()

PAGE NO:

public void showPos(int p) { System.out.println("The number "+n+" is present at the "+p+"th position"); }//end of showPos() public void showOcc(int o) { if(o>0) System.out.println("The number "+n+" has a occurence of "+o+" times in the array"); else System.out.println("The number "+n+" is not present in the array"); }//end showOcc() }//end of class LinearSearch public class LinearSearchMain { public static void main(String args[]) { LinearSearch obj = new LinearSearch(); obj.get(); }//end of main() }//end of class LinearSearchMain

SAMPLE OUTPUT
Enter size 9 Enter element 5 Enter element 3 Enter element

PAGE NO:

7 Enter element 6 Enter element 4 Enter element 3 Enter element 8 Enter element 7 Enter element 7 Enter the number two be searched number 3 The number 3 is present at the 2th position The number 3 is present at the 6th position The number 3 has an occurrence of 2 times in the array

PROGRAM LISTING
//To calculate the grade of a student import java.util.*; class GradeCalc { private double avg; private double sp; private char g; public void get()

PAGE NO:

{ Scanner sc = new Scanner(System.in); System.out.println("Enter marks in three subjects"); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); System.out.println("Enter point in sports"); sp = sc.nextDouble(); avg = (double)(a+b+c)/3; calculateGrade(); show(); }//end of get() public void calculateGrade() { if(avg>=90 && sp>=9) g='A'; else if(avg>=75 && sp>=7.5) g='B';

else if(avg>=60 && sp>=6) g='C'; else g='F'; }//end of calculateGrade() public void show() { System.out.println("Average marks of three subjects = "+avg); System.out.println("Achieved Grade : "+g); }//end of show()

PAGE NO:

}//end of class GradeCalc public class GradeCalcMain { public static void main(String args[]) { GradeCalc obj = new GradeCalc(); obj.get(); }//end of main() }//end of class GradeCalcMain

SAMPLE OUTPUT
Enter marks in three subjects 85 87 81 Enter point in sports 9 Average marks of three subjects = 84.33333333333333 Achieved Grade: B

PAGE NO:

PROGRAM LISTING
//To print a design import java.util.*; class Design { public void get() { Scanner sc=new Scanner(System.in); System.out.println("Enter line number of upper and lower triangle"); int l1 = sc.nextInt(); int l2 = sc.nextInt(); System.out.println("Enter character of lower triangle");

PAGE NO:

char ch = sc.next().charAt(0); show(l1); show(); show(l2,ch); }//end of get() public void show(int l) { for(int i=1;i<=l;i++) { for(int j=1;j<=i;j++) { System.out.print("*"); } System.out.println(); } }//end of show(int) public void show() { for(int i=1;i<=20;i++) { System.out.print("-"); } System.out.println(); }//end of show() public void show(int l,char c) { for(int i=l;i>=1;i--) { for(int j=i;j>=1;j--) PAGE NO:

{ System.out.print(c); } System.out.println(); } }//end of show(int,char) }//end of class Design public class DesignMain { public static void main(String args[]) { Design obj = new Design(); obj.get(); }//end of main() }//end of class DesignMain

SAMPLE OUTPUT
Enter line number of upper and lower triangle 7 12 Enter character of lower triangle $ * ** *** **** ***** ****** ******* --------------------

PAGE NO:

$$$$$$$$$$$$ $$$$$$$$$$$ $$$$$$$$$$ $$$$$$$$$ $$$$$$$$ $$$$$$$ $$$$$$ $$$$$ $$$$ $$$ $$ $

You might also like