C to Java (binary numbers programm) [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I made this program on C,and I have no idea of Java (new-yοung developer).Can you help me to "translate" it on Java?
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
void printbin(int num)
{
int binnum;
binnum=num%2;
num=num/2;
if (num!=0) printbin(num);
printf("%d",binnum);
return;
system("pause");
}
int main(void)
{
int posnumber,binnum;
printf("Give a number : ");
scanf("%d",&posnumber);
printbin(posnumber);
printf("\n");
return 0;
}

You can learn from this code, hope this can help you:
//here is comments in java
package help;
import java.util.Scanner;
//this is the way how you can all the libraries in java so
/*
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
*/
//here name of your class
public class Help {
//here name of your methode
public static void printbin(int num) {
int binnum;
binnum = num % 2;
num = num / 2;
if (num != 0) {
printbin(num);
}
System.out.println(binnum);
}
//here is the main methode in java
public static void main(String args[]) {
int posnumber, binnum;
Scanner scan = new Scanner(System.in);
System.out.println("Give a number : ");
posnumber = scan.nextInt();
System.out.println(posnumber);
}
}
Good luck, and good start with java :)

Related

local variable initialization were wrong java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am new to java, why a, b,c initialization are wrong in the following code.
public static void main(String[] args) {
if (args.length < 2)
throw new IllegalArgumentException ("we need 2 argumeents");
else {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = a+b;
}
System.out.println(a + " + " + b + " = " + c);
}
Java works differently compared to JavaScript. Every {} block has an own variable scope. Variables defined inside a block are not visible outside.
public static void main(String[] args) {
{
int x=1;
System.out.println(x); // prints 1
}
{
int x=2;
System.out.println(x); // prints 2
}
// System.out.println(x); // error: cannot find symbol
}

How do I retrieve the data input from a scanner to use in a method? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Basically I have to write a program that uses data that was inputted by the user and put it in a mathematical formula so it spits out an answer. The formula has to be created as a method. This is what I have so far. When I run the code it lets me input a number, but when I do that the program does not output anything and it just finishes running.
import java.util.Scanner;
public class Namek {
static int myMethod(int radius) {
return ((radius * radius) * (22/7));
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter Radius");
String radius = keyboard.nextLine();
}
private static void myMethod() {
}
You need to call the method passing the radius as parameter of the method. You can check https://www.tutorialspoint.com/java/java_methods.htm
,this link to see how to create and call methods with parameters.
I think what you wanted to do is the following:
import java.util.*;
public class Namek {
static int myMethod(int radius) {
return ((radius * radius) * (22/7));
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter Radius");
int radius = Integer.valueOf(keyboard.nextLine());
int area = myMethod(radius);
System.out.println(area);
}
}
Below is what you were trying to do I think
public class Formula {
static int myMethod(int radius) {
return ((radius * radius) * (22/7));
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter Radius");
int radius = Integer.valueOf(keyboard.nextLine());
float output = myMethod(radius);
System.out.println(output);
}
}

What do I need to fix to get the code to compile? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Write a Java program with a recursive method that accepts a value i as input and computes the sum:
m(i) = 1/3 + 2/5 + 3/7 + 4/9 + 5/11 + ... + i/(2i + 1)
So far I have:
import java.util.Scanner;
public class Recursion {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter an i: ");
int i = sc.nextInt();
if (i == 0) {
System.out.println(0);
}
else {
System.out.println(i / (2 * i + 1) + (main(i-1)));
}
}
}
The code isn't compiling. Please help!!
Ok I think I finally understand what you're trying to do! The problem is you're actually trying to build a string! The method is doing what it should, your return just isn't really doing what it should.
import java.util.Scanner;
public class Recursion
{
private static String result = null;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter an i: ");
int i = sc.nextInt();
System.out.println(recursion(i));
}
public static String recursion(int i)
{
if(i != 0)
{
if(result == null)
{
result = (i + "/" + (2 * i + 1));
recursion(i - 1);
}
else
{
result = result.concat(" + ").concat(i + "/" + (2 * i + 1));
recursion(i - 1);
}
}
return result;
}
}
Please let me know if this does what you want it to!
You shouldn't use recursion on the main function. Just create a second function that isn't the main and it should work:
import java.util.Scanner;
public class Recursion {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.println("Enter an i: ");
int i = sc.nextInt();
System.out.println(recursion(i));
}
public int recursion(int i){
if (i == 0) {
return 0;
}
else {
return (i / (2 * i + 1) + (recursion(i-1)));
}
}
}

What does" .class expected error " mean in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Here is my code. I got an error as ".class expected".
What should I do to rectify it.
import java.util.*;
class Atm2
{
public static void main (String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the amount to be withdrawed");
int withdraw = in.nextInt();
System.out.println("Enter the amount in the account");
double acnt_balc = in.nextDouble();
if ((withdraw % 5 == 0) && (acnt_balc >= withdraw + 0.50))
double cur = acnt_balc - (withdraw + 0.50);
System.out.println(cur);
else
System.out.println(acnt_balc);
}
}
Try curly braces in the context of your if-else.
It should look like this:
if (isMoving) {
currentSpeed--;
} else {
System.err.println("The bicycle has already stopped!");
}
So you can see, that there is a if-block and a else-block. You have to say which code belongs to if and which belongs to else. You can do that with the braces.
Scanner requires you to import java.util.*;
I tried it works and also format properly , see below , it runs
import java.util.*;
class Atm2
{
public static void main (String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the amount to be withdrawed");
int withdraw = in.nextInt();
System.out.println("Enter the amount in the account");
double acnt_balc = in.nextDouble();
if ((withdraw % 5 == 0) && (acnt_balc >=(withdraw + 0.50)))
{
double cur=(acnt_balc-(withdraw + 0.50));
System.out.println(cur);
}
else
{
System.out.println(acnt_balc);
}
}
}
Also make sure your Filename is the same as class name , in this case Atm2.java
The Scanner Class in present in the 'java.util' package , since you forgot to import it and used Scanner sc=new Scanner(); the compliler is complaining, and remember to compile use javac filename.java , to run use java filename

How does not use the loop output 99 multiplication table? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to output 99 multiplication table not use the loop. Do not say only use 'system.out.print'.
public class NextDiGui {
public static void main(String[] args) {
row();
}
static int num1 =0, num2 =1;
public static void row(){
num1++;
if(num1<10){
col();
}
}
public static void col(){
if(num2<=num1){
System.out.print(num1+"*"+num2+"="+num1*num2+"\t");
num2++;
col();
}
else{
num2=1;
System.out.println();
row();
}
}
}

Categories

Resources