Write a program to print all numbers from 1 to 1000 which leave a remainder of 3 when divided by 11.
Output format:
3, 14, 25, 36, 47…. 993
Approach: To solve the problem, follow the below idea:
The problem can be solved by simply using for loop and iterate from 3 up to the 1000 and increment by 11 as we are dividing by 11.
Step-by-step approach:
- Iterate from 3 to limit, in each iteration increment by 11.
- Print the number for each iteration.
Below is the implementation of the above approach:
C++
#include <iostream> using namespace std;
void printNumbers( int limit)
{ // Input the limit from the user
cout << "Numbers leaving a remainder of 3 when divided "
"by 11 up to "
<< limit << ":" << endl;
// Iterate through numbers and print those meeting the
// condition
for ( int number = 3; number < limit; number += 11) {
cout << number << " ";
}
cout << endl;
} int main()
{ int limit = 1000;
printNumbers(limit);
return 0;
} |
Java
import java.util.Scanner;
public class Main {
// Function to print numbers leaving a remainder of 3 when divided by 11
static void printNumbers( int limit) {
// Input the limit from the user
System.out.println( "Numbers leaving a remainder of 3 when divided by 11 up to " + limit + ":" );
// Iterate through numbers and print those meeting the condition
for ( int number = 3 ; number < limit; number += 11 ) {
System.out.print(number + " " );
}
System.out.println();
}
public static void main(String[] args) {
// Create a Scanner object for user input
Scanner scanner = new Scanner(System.in);
// Set the limit to 1000 (as in the original C++ code)
int limit = 1000 ;
// Call the function to print numbers
printNumbers(limit);
// Close the Scanner
scanner.close();
}
} |
Python3
def print_numbers(limit):
# Input the limit from the user
print (f "Numbers leaving a remainder of 3 when divided by 11 up to {limit}:" )
# Iterate through numbers and print those meeting the condition
for number in range ( 3 , limit, 11 ):
print (number, end = " " )
print ()
if __name__ = = "__main__" :
limit = 1000
print_numbers(limit)
|
C#
using System;
class Program
{ // Function to print numbers leaving a remainder of 3 when divided by 11 up to a given limit
static void PrintNumbers( int limit)
{
// Input the limit from the user
Console.WriteLine($ "Numbers leaving a remainder of 3 when divided by 11 up to {limit}:" );
// Iterate through numbers and print those meeting the condition
for ( int number = 3; number < limit; number += 11)
{
Console.Write(number + " " );
}
Console.WriteLine();
}
static void Main()
{
int limit = 1000;
// Call the function to print numbers
PrintNumbers(limit);
// Ensure the console window stays open
Console.ReadLine();
}
} |
Javascript
// Function to print numbers leaving a remainder of 3 when divided by 11 function printNumbers(limit) {
// Input the limit from the user
console.log(`Numbers leaving a remainder of 3 when divided by 11 up to ${limit}:`);
// Iterate through numbers and print those meeting the condition
for (let number = 3; number < limit; number += 11) {
process.stdout.write(`${number} `);
}
console.log(); // Move to the next line after printing all numbers
} // Set the limit to 1000 (as in the original Java code) const limit = 1000; // Call the function to print numbers printNumbers(limit); |
Output
Numbers leaving a remainder of 3 when divided by 11 up to 1000: 3 14 25 36 47 58 69 80 91 102 113 124 135 146 157 168 179 190 201 212 223 234 245 256 267 278 289 300 311 322 333 344 355 366 377 388 39...
Time complexity: O(1)
Auxiliary Space: O(1)
Article Tags :
Recommended Articles