Question

In: Computer Science

public void printNumbers(int low, int high) { // using a for loop, print all numbers from...

public void printNumbers(int low, int high) {
   // using a for loop, print all numbers from low to high
}

public int sumOfNumbers(int n) {
   // using a for loop, calculate and return the sum of first n numbers
   // i.e n = 5, answer = 5+4+3+2+1 = 15
}

public void printMultiplicationTable(int num) {
   // using a for loop, print the multiplication table of num (up to first 10!)
   // i.e. num = 5, 5*1=5, 5*2=10, 5*3=15, 5*4=20, 5*5=25, 5*6=30, 5*7=35...
}

Solutions

Expert Solution

public void printNumbers(int low, int high) {
    for (int i = low; i <= high; i++) {
        System.out.println(i);
    }
}

public int sumOfNumbers(int n) {
    int sum = 0;
    for (int i = 1; i <= n; i++) {
        sum += i;
    }
    return sum;
}

public void printMultiplicationTable(int num) {
    // using a for loop, print the multiplication table of num (up to first 10!)
    // i.e. num = 5, 5*1=5, 5*2=10, 5*3=15, 5*4=20, 5*5=25, 5*6=30, 5*7=35...
    for (int i = 1; i <= 10; i++) {
        System.out.println(num + "*" + i + "=" + (num * i));
    }
}

public class LoopNumbers {

    public void printNumbers(int low, int high) {
        for (int i = low; i <= high; i++) {
            System.out.println(i);
        }
    }

    public int sumOfNumbers(int n) {
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += i;
        }
        return sum;
    }

    public void printMultiplicationTable(int num) {
        // using a for loop, print the multiplication table of num (up to first 10!)
        // i.e. num = 5, 5*1=5, 5*2=10, 5*3=15, 5*4=20, 5*5=25, 5*6=30, 5*7=35...
        for (int i = 1; i <= 10; i++) {
            System.out.println(num + "*" + i + "=" + (num * i));
        }
    }

    public static void main(String[] args) {
        new LoopNumbers().printNumbers(5, 9);
        System.out.println(new LoopNumbers().sumOfNumbers(100));
        new LoopNumbers().printMultiplicationTable(5);
    }
}


Related Solutions

class Loops{ public void printNumbers(int low, int high){ // using a for loop, print all numbers...
class Loops{ public void printNumbers(int low, int high){ // using a for loop, print all numbers from low to high for(int i = low; i <= high; i++){ System.out.println(i); } } public int sumOfNumbers(int n){ // using a for loop, calculate and return the sum of first n numbers // i.e n = 5, answer = 5+4+3+2+1 = 15 int sum = 0; for(int i = 1; i <= n; i++){ sum += i; } return sum; } public void...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
Using a “for loop” print all even numbers in range from 1 to 1000. Also, please...
Using a “for loop” print all even numbers in range from 1 to 1000. Also, please count and print how many even numbers you have found.
JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print...
JAVA RECURSION public void recMethod(int[] array, int start, int end) { if(start < end) { print the array double the value in the array at position start recMethod(array, start+1, end) print the array } else { print "done" } } Assume the method is invoked with array = [3, 4, 6, 7, 8, 10, 4], start = 2, and end = 5 1.How many times is the array printed before the word "done" is printed? 2.How many times is the...
Write the pseudocodes for these programs: a) #include <iostream> using namespace std; void print(int arr[],int n){...
Write the pseudocodes for these programs: a) #include <iostream> using namespace std; void print(int arr[],int n){ if(n==0) return; cout<<arr[n-1]<<" "; print(arr,n-1); } int main() { int n; cout<<"Enter the size of the array:"; cin>>n; int arr[n]; int i; cout<<"Enter the elements of the array:"<<endl; for(i=0;i<n;i++){ cin >> arr[i]; } cout<<"Displaying the elements of the array in reverse order:"<<endl; print(arr,n); return 0; } b) #include<iostream> #include<fstream> using namespace std; //student structure struct student { int id; float gpa; }; //node structure...
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method...
(JAVA) InvertArrangement +invert(int[] a) : int [] +print(int[] a) : void Example 1: the invert method receives the following arrangement: [1,2,3,4,5] The invert method returns the array [5,4,3,2,1] Example 2: the print method receives the following array: [5,4,3,2,1] The print method prints: 5,4,3,2,1 (data separated by commas). TIP: for the print method use System.out.print () without the "ln".
USING a LOOP for C++ In this lab the completed program should print the numbers 0...
USING a LOOP for C++ In this lab the completed program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. You should accomplish this using a for loop instead of a counter-controlled while loop. Instructions Write a for loop that uses the loop control variable to take on the values 0 through 10. In the body of the loop, multiply the value of the loop control variable by 2 and by 10....
What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }
#include<iostream> using namespace std; void calcSumAndDiff(int ,int, int &, int &); void calcSumAndDiff(int n1,int n2, int...
#include<iostream> using namespace std; void calcSumAndDiff(int ,int, int &, int &); void calcSumAndDiff(int n1,int n2, int &sum, int &diff){ sum = n1 + n2; diff = n1 - n2; } int main() { int n1,n2,sum,diff; n1=30;n2=10; calcSumAndDiff(n1,n2,sum,diff); cout<<"Sum is :"<<sum<<endl; cout<<"Diff is:"<<diff<<endl; system("pause"); }
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {     ...
REWRITE FOLLOWING CODES USING DO...WHILE LOOP. BY USING C LANGUAGE #include <stdio.h> int main(void) {      int count =2; // COUNT TAKEN 2 AS TO PRINT 2 TIMES           while(count--){                printf("\--------------------------------------------\ \n"); printf("\          BBBBB               A                   \ \n"); printf("\          B    B             A A                  \ \n"); printf("\          BBBB              A   A                 \ \n"); printf("\          B    B           AAAAAAA                \ \n"); printf("\          BBBBB           A       A               \ \n"); printf("\---------------------------------------------\ \n");             }                                 return 0; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT