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
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...
"C language" Take this code and make the minor modification necessary to create a circular linked...
"C language" Take this code and make the minor modification necessary to create a circular linked list (Hint: Store a pointer to the first node in the next pointer of the last node.) Demonstrate that this is working by traversing the list until the first pointer is encountered 3 times. Next redefine the node structure to include a back pointer. This will enable your program to move from front to back and then from back to front. It is not...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct. The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models. Please explain the logic errors present in in the definition of is_reverse_sorted...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Please answer the following C question: There is a documented prototype for a function called get_a_line...
Please answer the following C question: There is a documented prototype for a function called get_a_line in the code below. Write a definition for get_a_line—the only function called from that definition should be fgetc. #include <stdio.h> #include <string.h> #define BUFFER_ARRAY_SIZE 10 int get_a_line(char *s, int size, FILE *stream); // Does what fgets does, using repeated calls to fgetc, but // provides a more useful return value than fgets does. // // REQUIRES // size > 1. // s points to...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...