Program to print all multiples of 7 till 1000
Last Updated :
14 Feb, 2024
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:
- This program uses a
for
loop to iterate through numbers starting from 7 and incrementing by 7 each time.
- Inside the loop, each multiple of 7 is printed.
- The loop continues until the number exceeds 1000.
Below is the implementation of the approach:
C++
#include <iostream>
using namespace std;
int main()
{
for ( int i = 7; i <= 1000; i += 7) {
cout << i << " " ;
}
return 0;
}
|
Java
public class Main {
public static void main(String[] args) {
for ( int i = 7 ; i <= 1000 ; i += 7 ) {
System.out.print(i + " " );
}
}
}
|
Python3
def print_multiples_of_seven(up_to):
for i in range ( 7 , up_to + 1 , 7 ):
print (i, end = " " )
if __name__ = = "__main__" :
limit = 1000
print_multiples_of_seven(limit)
|
C#
using System;
class Program
{
static void Main()
{
for ( int i = 7; i <= 1000; i += 7)
{
Console.Write(i + " " );
}
Console.ReadLine();
}
}
|
Javascript
function printMultiplesOfSeven(upTo) {
for (let i = 7; i <= upTo; i += 7) {
process.stdout.write(i + " " );
}
}
if (require.main === module) {
const limit = 1000;
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)
Share your thoughts in the comments
Please Login to comment...