Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners according to me its ok but is calculating wrong values in
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Ajeet Pratap Singh
according to me its ok but is calculating wrong values in
0 Answer(s)      2 years and 10 months ago
Posted in : Java Beginners


program to calculate roman numbers .....i.e roman number calculator

import java.util.*; import java.io.*; class romancalci { public static void main(String args[])throws IOException { int decimal1 = 0;int decimal2=0; Scanner input=new Scanner(System.in); System.out.print("Enter a Roman Number 1st: "); String roman1 = input.next(); System.out.println("Enter a Roman Number 2nd: "); String roman2=input.next(); String romanNumeral1 = roman1.toUpperCase(); String romanNumeral2=roman2.toUpperCase(); int x = 0;int result=0;int largest=0;int number=0; int l1=romanNumeral1.length(); int l2=romanNumeral2.length(); String numeral1=romanNumeral1; String numeral2=romanNumeral2;
String s="IC"; String s3="IVC";

 String newNumeral = numeral1.replace("XL", "XXXX"); // Replaces XL with XXXX

 String newNumeral2 = newNumeral.replace("XC", "LXXXX"); // Replaces XC with LXXXX

 String newNumeral3 = newNumeral2.replace("CD", "CCCC"); // Replaces CD with CCCC

 String newNumeral4 = newNumeral3.replace("CM", "DCCCC"); // Replaces CM with DCCC

 String newNumeral5 = newNumeral4.replace("IV", "IIII"); // Replaces IV with IIII

 String newNumeral6 = newNumeral5.replace("IX", "VIIII"); 

l1=newNumeral6.length();

String newNumeralz = numeral2.replace("XL", "XXXX"); // Replaces XL with XXXX

 String newNumeralz2 = newNumeralz.replace("XC", "LXXXX"); // Replaces XC with LXXXX

 String newNumeralz3 = newNumeralz2.replace("CD", "CCCC"); // Replaces CD with CCCC

 String newNumeralz4 = newNumeralz3.replace("CM", "DCCCC"); // Replaces CM with DCCC

 String newNumeralz5 = newNumeralz4.replace("IV", "IIII"); // Replaces IV with IIII

 String newNumeralz6 = newNumeralz5.replace("IX", "VIIII");  
      l2=newNumeralz6.length();

if(newNumeral6.equals(s)) { System.out.println("enter the correct roman value 1 in place of IC"); newNumeral6="0"; }

do { char convert1 = newNumeral6.charAt(x); switch (convert1) { case 'M': decimal1 += 1000; break;

                    case 'D':
                            decimal1 += 500;
                            break;

                    case 'C':
                            decimal1 += 100;
                            break;

                    case 'L':
                            decimal1 += 50;
                            break;

                    case 'X':
                            decimal1 += 10;
                            break;

                    case 'V':
                            decimal1 += 5;
                            break;

                    case 'I':
                            decimal1 += 1;
                            break;
                    }
                    x++;
            } while (x < l1);
            System.out.println("Decimal Number1 is: " + decimal1);
             x=0;
             if(newNumeralz6.equals("ic"))

{ System.out.println("enter the correct roman value 2 in place of IC"); newNumeralz6="0"; }

do { char convert2 = newNumeralz6.charAt(x); switch (convert2) { case 'M': decimal2 += 1000; break;

                    case 'D':
                            decimal2 += 500;
                            break;

                    case 'C':
                            decimal2 += 100;
                            break;

                    case 'L':
                            decimal2 += 50;
                            break;

                    case 'X':
                            decimal2 += 10;
                            break;

                    case 'V':
                            decimal2 += 5;
                            break;

                    case 'I':
                            decimal2 += 1;
                            break;
                    }
                    x++;

            } while (x < l2);
            System.out.println("Decimal Number2 is: " + decimal2);


          System.out.println("enter the choice for calculating \n 1 for addition  \n 2 for subtraction \n 3 for multiplication");
          int choice=input.nextInt();
          switch(choice)

{ case 1: result=decimal1+decimal2; System.out.println("the calculated result in integer is= "+ result); break; case 2: result=decimal1-decimal2; System.out.println("the calculated result in integer is= "+ result); break; case 3: result=decimal1*decimal2; System.out.println("the calculated result in integer is= "+ result); break; default: System.out.println("enter the correct choice"); }

      number=result;
      String roman="";



    while(number>=1000){
        roman += "M";
        number-=1000;
    }

    while(number>=900){
        roman += "CM";
        number-=900;
    }

    while(number>=500){
        roman += "D";
        number-=500;
    }

    while(number>=400){
        roman += "CD";
        number-=400;
    }

    while(number>=100){
        roman += "C";
        number-=100;
    }

    while(number>=90){
        roman += "XC";
        number-=90;
    }

    while(number>=50){
        roman += "L";
        number-=50;
    }

    while(number>=40){
        roman += "XL";
        number-=40;
    }

    while(number>=10){
        roman += "X";
        number-=10;
    }

    while(number>=9){
        roman += "IX";
        number-=9;
    }

    while(number>=5){
        roman += "V";
        number-=5;
    }

    while(number>=4){
        roman += "IV";
        number-=4;
    }

    while(number>=1){
        roman += "I";
        number-=1;
    }

    System.out.println("the final result in roman ="+ roman);
}

}


Advertisement
View Answers

Related Tutorials/Questions & Answers:
according to me its ok but is calculating wrong values in
according to me its ok but is calculating wrong values in   program to calculate roman numbers .....i.e roman number calculator import java.util....); System.out.println("enter the choice for calculating \n 1 for addition \n 2
How to get the correct value by calculating double values....
How to get the correct value by calculating double values....  Hello... and values like 59,142 etc here i am getting wrong output for the same duration 59 i am getting wrong cost .. Sir plz help me in this code.Thank you Sir
Advertisements
How to get the correct value by calculating double values....
How to get the correct value by calculating double values....  Hello... is integer type and values like 59,142 etc here i am getting wrong output for the same duration 59 i am getting wrong cost .. Sir plz help me in this code.Thank you Sir
Wrong parameter values while exporting data from jsp to excel
Wrong parameter values while exporting data from jsp to excel   This is a jsp report. When i export the report data to an excel, the parameter values on the html report do not match those in the downloaded spreadsheet
reply me its urgent - Java Beginners
reply me its urgent  Hi friends I am facing problem in my application in this type please help me i am using database mysql5.0 version...'@'localhost' (using password: YES)" please tell me what is the error....its
its urgent for me - Development process
its urgent for me  Hi All, this is kalavathi,i am new to alfresco.present i am using alfresco3.9b version.my problem is i was writing code in java... is the problem .can u tell me i will send my total code and error exception also. import
Please help me... its very urgent
Please help me... its very urgent  Please send me a java code to check whether INNODB is installed in mysql... If it is there, then we need to calculate the number of disks used by mysql
Ple help me its very urgent
Ple help me its very urgent  Hi.. I have one string 1)'2,3,4' i want do like this '2','3','4' ple help me very urgent
pls help me sir its urgent
pls help me sir its urgent  thanks for reply, but i am getting this error pls help me its urgent type Exception report message description The server encountered an internal error () that prevented it from
wrong year
= [NSDate date]; NSLog(@"%@", [df stringFromDate:date]); return; Its giving me wrong year..   I think u are using other calendar Check Settings... wrong year  hello I m using the following code: NSDateFormatter
its very urgent please help me
its very urgent please help me  how can i retrieve all images from ms access database and display in jsp pages
GUI Interface .Reply me with in hour..Its Urgent - Java Beginners
GUI Interface .Reply me with in hour..Its Urgent  Hi ... Now i am doing project for Voting finger print Authentication .. Can any pls assit me.. I... Existing project... Help me
Plz reply me sir Its urgent - JSP-Servlet
Plz reply me sir Its urgent  Respected Sir/madam, I am R.Ragavendran..Actuaaly,I request you to remember the code which you have provided for me using radio buttons.. You have used the following syntax for checking whether
Calculating current Experience
Calculating current Experience  I have 2 dates one is the current date and another is join date f an employee i need to calculate the current experience as in years months and days can any one help me pls
do the combinations in java, pls help me its urhent - Development process
do the combinations in java, pls help me its urhent  import java.util.*; interface Expression { public void interpret(ArrayList exp); } class... one help me: action when condition1 and condition3 action when condition1
servlet not working properly ...pls help me out....its really urgent
servlet not working properly ...pls help me out....its really urgent  Hi, Below is the front page of my project 1)enty.jsp </form> </body> </html> </form> </body> </html>> <
Java Program for Calculating Marks
Java Program for Calculating Marks  Hi Everyone, I have a assignment that requires me to a write simple java program that will calculate marks... of how to do this. Please someone help me with this assignment. Thank you very
Java Program for Calculating Marks
Java Program for Calculating Marks  Hi Everyone, I have a assignment that requires me to a write simple java program that will calculate marks... of how to do this. Please someone help me with this assignment. Thank you very
Java Program for Calculating Marks
Java Program for Calculating Marks  Hi Everyone, I have a assignment that requires me to a write simple java program that will calculate marks... of how to do this. Please someone help me with this assignment. Thank you very
Java Program for Calculating Marks
Java Program for Calculating Marks  Hi Everyone, I have a assignment that requires me to a write simple java program that will calculate marks... of how to do this. Please someone help me with this assignment. Thank you very
What is wrong with this line
What is wrong with this line  What is wrong with this line JOptionPane.showMessageDialog(null? for Using Lewandowski's Savings pgrm!"); from if (input.equals("exit") || input.equals("Exit")) { JOptionPane.showMessageDialog
what is wrong with this program
what is wrong with this program  import java.io.*; class Distance { int feet,inch; void read() { System.out.println("Enter distance in feet and inches "); feet=Integer.parseInt(ob.readLine()); inch
I am trying to develop a Image Sliding application but unfortunately its not working... Can any one help me in this
I am trying to develop a Image Sliding application but unfortunately its not working... Can any one help me in this  Can Anyone Help Me In This import java.awt.*; import javax.swing.*; import java.awt.event.*; class SlideShow
What is wrong with my servlet?
What is wrong with my servlet?  I'd like to know which part of the code is wrong. My code is: print("code sample"); import java.util.*; import java.io.IOException; import java.io.PrintWriter; import
what is wrong in this program
what is wrong in this program  import java.io.*; class Distance { int feet; int inch; void read(int feet, int inch) { System.out.println("Enter distance in feet and inches "); feet=Integer.parseInt(ob.readLine
what is wrong in this program......
what is wrong in this program......  program to implement given inheritance import java.io.*; class Student { int rno; String name; DataInputStream ob = new DataInputStream(System.in); void getRoll
Text box size varying in IE 7 but ok in firefox - Java Beginners
Text box size varying in IE 7 but ok in firefox  I have problem with the size of text field which varies in IE 7 only but its fixed in Firefox( I have two text boxes one for email id and other for password both with the same text
User LogIn According to Role
User LogIn According to Role  User login According to role but output will be always displayed in a single page.. but content will change according role
change color according to the database value
change color according to the database value  any one know how to get values from database ( 1 or 0 ) and according to that change the color ( red or green) of given list
I want to update the multiple values of database depending on checkbox..? Please help me out..
I want to update the multiple values of database depending on checkbox..? Please help me out..   Hi .. I want to Update the multiple values of database using checkboxes and want to set the session for selected checkboxes
displaying List of records from database in a jsp using ajax, onclick it should display the results ?? its urgent can u help me
displaying List of records from database in a jsp using ajax, onclick it should display the results ?? its urgent can u help me   displaying List... ?? its urgent can u help me
Advertisements
 

 

 

DMCA.com