Open In App

Program to print all multiples of 7 till 1000

Write a program to print all multiples of 7 till 1000.

Output Format:



7, 14, 21, 28, 35 ….. 994

Approach: To solve the problem, follow the below idea:



The approach of this program is to run a loop starting from 7 and in each iteration add 7 to the current multiple to get the next multiple.

Step-by-step algorithm:

Below is the implementation of the approach:




#include <iostream>
using namespace std;
int main()
{
 
    // Print multiples of 7 up to 1000
    for (int i = 7; i <= 1000; i += 7) {
        cout << i << " ";
    }
 
    return 0;
}




public class Main {
    public static void main(String[] args) {
 
        // Print multiples of 7 up to 1000
        for (int i = 7; i <= 1000; i += 7) {
            System.out.print(i + " ");
        }
 
    }
}




def print_multiples_of_seven(up_to):
    # Print multiples of 7 up to the specified limit
    for i in range(7, up_to + 1, 7):
        print(i, end=" ")
 
if __name__ == "__main__":
    # Specify the limit
    limit = 1000
 
    # Call the function to print multiples of 7 up to the limit
    print_multiples_of_seven(limit)




using System;
 
class Program
{
    static void Main()
    {
        // Print multiples of 7 up to 1000
 
        // Loop through multiples of 7 starting from 7 and up to 1000
        for (int i = 7; i <= 1000; i += 7)
        {
            // Output the current multiple of 7
            Console.Write(i + " ");
        }
 
        // Ensure the console window stays open
        Console.ReadLine();
    }
}




function printMultiplesOfSeven(upTo) {
    // Print multiples of 7 up to the specified limit
    for (let i = 7; i <= upTo; i += 7) {
        process.stdout.write(i + " ");
    }
}
 
// Check if the script is executed as the main program
if (require.main === module) {
    // Specify the limit
    const limit = 1000;
 
    // Call the function to print multiples of 7 up to the limit
    printMultiplesOfSeven(limit);
}

Output
7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 112 119 126 133 140 147 154 161 168 175 182 189 196 203 210 217 224 231 238 245 252 259 266 273 280 287 294 301 308 315 322 329 336 343 350 357 364 371 378...

Time Complexity: O(1000) ~ O(1)
Auxiliary space: O(1)


Article Tags :