You are not logged in. Please login at www.codechef.com to post your questions!

×

do help to modify this code. I want my output as multiplication table for even numbers.

import java.util.Scanner;

class Tables { public static void main(String args[]) { int a, b, c, d;

System.out.println("Enter range of numbers to print their multiplication table");
Scanner in = new Scanner(System.in);

a = in.nextInt();
b = in.nextInt();

for (c = a; c <= b; c++) {
  System.out.println("Multiplication table of "+c);

  for (d = 1; d <= 10; d++) {
     System.out.println(c+"*"+d+" = "+(c*d));
  }
}

} }

asked 27 Jul, 18:28

vinita06's gravatar image

0★vinita06
11
accept rate: 0%


I'm not sure if I understand your problem correctly but you are trying to print a multiplication table for even numbers based on the given input 'a' & 'b', right? If it is, maybe the below modification is what you're looking for

    import java.util.Scanner;

    class Tables { 
        public static void main(String args[]) 
        { 
            int a, b, c, d;

            System.out.print("Enter range of numbers to print their multiplication table: ");
            Scanner in = new Scanner(System.in);

            a = in.nextInt();
            b = in.nextInt();

            a = (a % 2 != 0 ? a+1 : a);
            b = (b % 2 != 0 ? b-1 : b);

            System.out.print("\t  ");
            for(c = a; c <= b; c+=2)
                System.out.print(c + "\t \t");
            System.out.println("\n");

            for (c = a; c <= b; c+=2) {
                  System.out.print(c + "\t| ");
                  for (d = a; d <= b; d+=2) {
                     System.out.print((c*d) + "\t|\t");
                  }
                  System.out.println();
            }
        } 
    }
link

answered 27 Jul, 19:06

anonymous_'s gravatar image

4★anonymous_
113
accept rate: 0%

edited 27 Jul, 19:07

Hmm..anyway thank you. But i want output look like this 1x6=6 2x6=12....until 10x6=60 then 1x8=8 2x8=16....10x8=80 then start back 1x10=10 2x10=20 until 10x10=100. :( my code's output looks 1x6=6 2x6=12....until 10x6=60 then 1x7=7....until 10x10=100. :(

(27 Jul, 19:34) vinita060★

if that's the case, just change your code to this

a = (a % 2 != 0 ? a+1 : a); b = (b % 2 != 0 ? b-1 : b);

for (c = a; c <= b; c+=2) { // other code here }

(27 Jul, 19:41) anonymous_4★

and also i want my input in list form. no matter what number i click as input,the output must be the multiplication of even number. your code is correct. but how can modify it for make it in list form and start from 1x6=6... ? sorry to bother you..

(27 Jul, 19:42) vinita060★

@vinita06 ignore my first code. I have a new comment above where my 2nd code is indicated. Just put that code to your original code. Hope it helps! :)

(27 Jul, 19:46) anonymous_4★

Thank you so much..it helps me alot..Once again thank you very much :)

(27 Jul, 20:19) vinita060★

@vinita06 You're welcome. I hope it solved your problem :)

(27 Jul, 20:26) anonymous_4★

a = (a % 2 != 0 ? a+1 : a); b = (b % 2 != 0 ? b-1 : b); is there any other way to write this command?

(29 Jul, 18:47) vinita060★
showing 5 of 7 show all
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,060
×30
×23
×5

question asked: 27 Jul, 18:28

question was seen: 170 times

last updated: 29 Jul, 18:47