Question

would someone kindly convert the following C code to java? thanks so much # include <stdio.h>...

would someone kindly convert the following C code to java? thanks so much

# include <stdio.h>


int main(void)
{
    int changeOwed;
    int check;
    char invisibleChar;
    int count = 0;
        int numQ=0, numD=0, numN=0, numP=0;
    
    
    do{
          printf("How much change is owed (in cents)?\n");
        check = scanf("%d", &changeOwed); // returns 1 if the input is a number and returns 0 otherwise
        
        //Get's rid of any extra invisible characters if the input is a char/String
        do{
            scanf("%c",&invisibleChar);
        }while(invisibleChar != '\n');
            
    }while(check == 0 || !(changeOwed >=0 ));
    
    
        while(changeOwed > 0){
        
            //Use as many quarters needed
                while(changeOwed >= 25){
                        count ++;
                        numQ++;
                        changeOwed = changeOwed - 25;
                }
                //Use as many dimes needed
                while(changeOwed >= 10){
                        count ++;
                        numD++;
                        changeOwed = changeOwed - 10;
                }
                //Use as many nickels needed
                while(changeOwed >= 5){
                        count ++;
                        numN++;
                        changeOwed = changeOwed - 5;
                }
                //Use as many pennies needed
                while(changeOwed >= 1){
                        count ++;
                        numP++;
                changeOwed = changeOwed - 1;
                }

        }
    
        printf("Quarters: %d, Dimes: %d, Nickels: %d, Pennies: %d\nNumber of coins used= %d\n\n", numQ, numD, numN, numP, count);
}

Homework Answers

Answer #1

Ans:- Java code for the following c code.

import java.util.*;
public class Cent
{
        //this function is used to check whether the input is integer or not 
        private static boolean checkInt(String x){
        int y;
        try{
            y = Integer.parseInt(x);
            return false;
            }catch(NumberFormatException ex){
                return true;
            }
        }
        public static void main (String[] args) 
        {
        int changeOwed=0;
        String c;        
        boolean check;
        int count = 0;
        Scanner obj = new Scanner(System.in); //Scanner class is used to take input in java
        int numQ=0, numD=0, numN=0, numP=0; 
        // it will run until user enter an integer       
        do{

            System.out.print("How much change is owed (in cents)?");
            c=obj.next(); //this statement will take input from user
            check=checkInt(c); //check whether the input is integer or not
          }while(check);
        changeOwed=Integer.parseInt(c);       
        while(changeOwed > 0){ //this while loop remain same in java
        
            //Use as many quarters needed
            while(changeOwed >= 25){
                count ++;
                numQ++; 
                changeOwed = changeOwed - 25;
            }
            //Use as many dimes needed
            while(changeOwed >= 10){
                count ++;
                numD++;
                changeOwed = changeOwed - 10;
            }
            //Use as many nickels needed
            while(changeOwed >= 5){
                count ++;
                numN++;
                changeOwed = changeOwed - 5;
            }
            //Use as many pennies needed
            while(changeOwed >= 1){
                count ++;
                numP++;
                changeOwed = changeOwed - 1;
            }

        }
        // the below statements are used to print the output
        System.out.println("Quarters: "+numQ+", Dimes: "+numD+", Pennies: "+numP);
        System.out.println("Number of coins used= "+count);
        }
}

Output:-

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
The code is in C programming language pls convert it into python. Thanks. Program --> #include...
The code is in C programming language pls convert it into python. Thanks. Program --> #include <stdio.h> #include <stdlib.h> void main() { //declare variables FILE *fileptr; char filename[15]; char charRead; char filedata[200],searchString[50]; int i=0,j=0,countNoOfWord=0,count=0; //enter the filename to be opened printf("Enter the filename to be opened \n"); scanf("%s", filename); /* open the file for reading */ fileptr = fopen(filename, "r"); //check file exit if (fileptr == NULL) { printf("Cannot open file \n"); exit(0); } charRead = fgetc(fileptr); //read the string...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char **argv) { const int BUF_LEN = 128; char str[BUF_LEN]; int i; char c; int is_binary; int d, n; /* Get the user to enter a string */ printf("Please enter a string made of 0s and 1s, finishing the entry by pressing Enter.\n"); for (i=0; i<BUF_LEN-1; i++) { scanf("%c", &c); if (c == '\n') { break; } str[i] = c; } str[i] = '\0'; /*...
Construct a flowchart based on this code and write its equivalent algorithms. #include <stdio.h> int main()...
Construct a flowchart based on this code and write its equivalent algorithms. #include <stdio.h> int main() { int x,y; float result; char ch; //to store operator choice printf("Enter first number: "); scanf("%d",&x); printf("Enter second number: "); scanf("%d",&y); printf("Choose operation to perform (+,-,*,/): "); scanf(" %c",&ch); result=0; switch(ch) { case '+': result=x+y; break; case '-': result=x-y; break; case '*': result=x*y; break; case '/': result=(float)x/(float)y; break; case '%': result=x%y; break; default: printf("Invalid operation.\n"); } printf("Result: %d %c %d = %.2f\n",x,ch,y,result); // Directly...
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input....
/*C PROGRAMMING: HOW TO INSERT ERROR BELOW CODE? QUESTION: This program reads integers from standard input. The first integer indicates the number of values that will follow. Read that many values, and return their sum, ignoring any additional values that may follow. However, if there are fewer integers than specified in the input, print "Error" and terminate. Hint: int n, success; ... success = scanf("%d", &n); // FIRST INTEGER INPUT reads an integer from stdin, returning 1 if the integer...
don't understand why this code outputs 3. Why doesn't it output 32?? #include <stdio.h> #include <ctype.h>...
don't understand why this code outputs 3. Why doesn't it output 32?? #include <stdio.h> #include <ctype.h> int main() { printf("%d\n", parseint("32")); return 0; } int parseint(char *str) { int i=0; while(str[i] != '\0'){ return (str[i] - '0'); i++; } return 1;    }
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3...
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3 functions input, gcd and lcm in such a way that the main function below compiles correctly and has the correct behavior. The input function prompts the user to enter a non-negative integer. If the user enters a negative integer, the function prints a "sorry" statement and prompts the user again. It keeps on prompting until the user enters a non-negative number. The input function...
Convert the following C program to C++. More instructions follow the code. #include <stdio.h> #include <stdlib.h>...
Convert the following C program to C++. More instructions follow the code. #include <stdio.h> #include <stdlib.h> #define SIZE 5 int main(int argc, char *argv[]) {    int numerator = 25;    int denominator = 10;    int i = 0;    /*    You can assume the files opened correctly and the    correct number of of command-line arguments were    entered.    */    FILE * inPut = fopen(argv[1], "r");    FILE * outPut = fopen(argv[2], "w");    float result =...
#include<stdio.h> int main() {       /* For binary search, the array should be arranged in ascending...
#include<stdio.h> int main() {       /* For binary search, the array should be arranged in ascending or descending order */       int data[] = {1,2,3,4,5,6,7,8,9,10}; ****You might change the numbers for your program also       /* 'min' use for starting location of the array, 'max' use for end location of the array and 'mid' use for middle location of the array */       int min = 0, max = 10, search, mid = (max + min) / 2;       printf("Enter...
Can you translate this C code into MIPS assembly with comment? #include <stdio.h> #include <math.h> #include...
Can you translate this C code into MIPS assembly with comment? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1);...
Translate the following C program to Pep/9 assembly language. #include <stdio.h.> int main() { int numitms,j,data,sum;...
Translate the following C program to Pep/9 assembly language. #include <stdio.h.> int main() { int numitms,j,data,sum; scanf("%d", &numitms); sum=0; for (j=1;j<=numitms;j++) { scanf("%d", &data); sum+=data; } printf("sum: %d\n",sum); return0; } Please test the answer using Pep/9. Thank you so much!
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT