Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
need some programming help?
i am a beginner programmer and i need a help wit a few programs . i know there arent many java programmers anymore but this has to be done in blueJ only. there are abt 5 programmes (pls). i would really appreciate help in the form of a program.these are the programs i need to execute.
also the nos are the main questions and the letters are the sub bits under that
1.i have to print this pattern
___________1
__________121
_________12321
________ 1234321
_________12321
__________121
___________ 1
this should be done by nested for() loop only and apparently we dont have to use scanner class or anything like that.
ignore the underscores.yahoo keeps ignoring the space i keep instead the pattern should like a diamond with those nos and you dont have to print the underscores
2.i should also write two programs that uses for() loops to find each one of these:
a)we shoulld print the sum of x/2! + x^2/3! + x^3/4!.........n terms (n is a no we accept through a method)
for those who dont know ^ indicates index for example 3^2=9 since 3 squared is 9 and 3^3=27 since 3 cubed is 27. and ! indicates factorial. for example 2!=2*1 and 5!=5*4*3*2*1 and 3!=3*2*1
b)x!/10 + (x+2)!/15 + (x+4)!/20+..........+(x+n)!/m!
n is a no we accept through method and i guess m is the multiple of 5 that we would get by the time we reach (x+n)!
3.we should write a menu driven program using scanner class which displays a menu consisting of 2 options.also when we enter 0 the program should terminate . we should use do while loop for the terminating part .
a)accept a no through method and check whether it is a neon no.
a neon no by the way is a no whose sum of digits of its square is equal to the no itself.
ex: 9
9 squared is 81
81s digits are 8 and 1
8+1=9 so 9 is a neon no
we should do this by using for loop
b)accept a no greater than 0 by scanner object (you know scanner ob=new scanner();int ob=nextInt();)
and print the sum of the prime no digits of the no we accepted
ex:we accept 4326
prime no digits are 3 and 2 so 3+2=5
and we should print 5 this should be done by for loop as well
appparently we have to invoke methods to execute the above to functions
4.accept a string thorugh method i guess and convet the string to lowercase and shift each letter to the left in the alphabet(using string functions)
ex:HELLO
output: gdkkn (g comes before h and d comes before d and so on)
5.inout a string and convert each uppercase letter to lower case and each lower case to upper
ex:JaVa PrOgraMs
output should be :jAvA pRoGRAmS
this should be done by string functions as well
if you read all that then kudos to you and sorry for the amazingly long question i really need help.
1 Answer
- Anonymous8 years agoFavorite Answer
Q1
public class NumberDiamond {
public static void main(String args[]) {
int i,j,l,k;
// Print top half of diamond
for(i=1;i<=4;i++) {
for(l=4-i;l>0;l--) {
System.out.print(" ");
}
for(k=1;k<=i-1;k++) {
System.out.print(k);
}
for(int m=k;m>=1;m--) {
System.out.print(m);
}
System.out.println();
}
// Print lower half of diamond
for(i=3;i>=1;i--) {
for(l=4-i;l>0;l--) {
System.out.print(" ");
}
for(k=1;k<=i-1;k++) {
System.out.print(k);
}
for(int m=k;m>=1;m--) {
System.out.print(m);
}
System.out.println();
}}}
Q2a
import java.util.Scanner;
import java.text.DecimalFormat;
public class SolveEquation {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.00");
double solution = 0;
System.out.println("Solve the series x/2! + x^2/3! + x^3/4! ... x^nth-1/nth!");
System.out.println("Enter the nth value");
int nth = input.nextInt();
System.out.println("Enter a value for x");
int x = input.nextInt();
for (int i = 2; i <= nth; i++) {
solution += ((Math.pow(x, i-1)) / factorial(i));
}
System.out.println(
df.format(solution) + " (2dp)");
}
public static double factorial(int n) {
int factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
return factorial;
}}
Q3
import java.io.*;
import java.util.Scanner;
public class NeonOrSumOfPrimeDigits {
public static void main (String[] args) throws IOException {
Scanner input = new Scanner(System.in);
int choice;
do {
System.out.println(
"[1] - Is your number Neon?\n" +
"[2] - Sum up the Prime Digits in your number\n" +
"[0] - Press 0 to Quit\n"
);
choice = input.nextInt();
} while (choice > 2);
switch (choice) {
case 1 :
neon();
break;
case 2 :
sumOfPrimeDigits();
break;
case 0 :
System.out.println("Program Closing");
System.exit(0);
break;
}}
public static void neon() throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int number, square, length, middle, left_half_square, right_half_square;
System.out.print("Enter a Number\n");
number = Integer.parseInt(br.readLine());
square = number * number;
String str = Integer.toString(square);
if(square <= 9)
str = "0" + str;
length = str.length();
middle = length / 2;
String left = str.substring(0,middle);
String right = str.substring(middle);
left_half_square = Integer.parseInt(left);
right_half_square = Integer.parseInt(right);
if(left_half_square + right_half_square == number)
System.out.println(number + " is a Neon number");
else
System.out.println(number + " is not a Neon number");
}
public static void sumOfPrimeDigits() {
Scanner input = new Scanner(System.in);
int digit, sum = 0;
System.out.println("Enter a Integer number");
String str = input.nextLine();
sum = parseString(str);
System.out.println("The sum of prime digits in " + str + " is: " + sum);
}
public static int parseString(String input) {
char[] charArray = input.toCharArray();
int digit = 0, sum = 0;
for (int index = 0; index < input.length(); index++) {
digit = Character.getNumericValue(charArray[index]);
if (isPrime(digit))
sum += digit;
}
return sum;
}
public static boolean isPrime(int number) {
for(int i = 2; i < number; i++){
if (number % i == 0) {
return false;
}}
return true;
}}
Q4
import java.util.Scanner;
import java.io.*;
public class LetterBefore {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a word");
String str = input.nextLine();
str = str.toLowerCase();
for (int i = 0; i < str.length(); i++) {
int x = str.charAt(i) - 1;
char ch = (char)x;
System.out.print(ch);
}}}
Q5
import java.util.Scanner;
import java.io.*;
public class SwapLowerUpperCase {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a word");
String str = input.nextLine();
for (int i = 0; i < str.length(); i++) {
char letter = str.charAt(i);
if (letter >= 'a' && letter <= 'z') {
letter = Character.toUpperCase(letter);
System.out.print(letter);
}
else {
letter = Character.toLowerCase(letter);
System.out.print(letter);
}}}}
No more room to edit. Updates Q3 for you (complete now). Had to lose ALL indentations just to get what is here in answer. You'll have to do Question 2b yourself; although if you look closely at how I did 2a I am sure you can work out what you need to do.