Program to Print Numbers Between 1 and n Divisible By m using While loop

Program to Print Numbers Between 1 and n Divisible By m using While loop

  • Write a program to Program to Print Numbers Between 1 and n Divisible By m using While loop in C
  • Write a program to Program to Print Numbers Between 1 and n Divisible By m using While loop in C++
  • Write a program to Program to Print Numbers Between 1 and n Divisible By m using While loop in Python
  • Write a program to Program to Print Numbers Between 1 and n Divisible By m using While loop in PHP
  • Write a program to Program to Print Numbers Between 1 and n Divisible By m using While loop in Java
  • Write a program to Program to Print Numbers Between 1 and n Divisible By m using While loop in Java Script
  • Write a program to Program to Print Numbers Between 1 and n Divisible By m using While loop in C#

Explanation:

To print numbers between 1 and n that are divisible by m using a for loop, follow these steps:

Steps:

  1. Take the input values for n and m.
  2. For loop: Iterate through the numbers from 1 to n.
    • In each iteration, check if the current number is divisible by m using the modulo operation (i % m == 0).
    • If the condition is true, print the number.

Logic:

  • Iterate from 1 to n.
  • In each iteration, check if the number is divisible by m (i.e., i % m == 0).
  • If it is divisible, print the number.

Explanation:

  • For input n = 20 and m = 5:
  • The loop iterates from 1 to 20.
  • It checks if each number is divisible by 5 (i % 5 == 0).
  • The numbers divisible by 5 are 5, 10, 15, 20.

Program to Print Numbers Between 1 and n Divisible By m using While loop

#include <stdio.h>

int main() {
    int n, m, i;

    printf("Enter the value of n: ");
    scanf("%d", &n);
    printf("Enter the value of m: ");
    scanf("%d", &m);

    i = 1;
    // Print numbers divisible by m using a while loop
    while (i <= n) {
        if (i % m == 0) {
            printf("%d ", i);
        }
        i++;
    }

    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int n, m, i;

    cout << "Enter the value of n: ";
    cin >> n;
    cout << "Enter the value of m: ";
    cin >> m;

    i = 1;
    // Print numbers divisible by m using a while loop
    while (i <= n) {
        if (i % m == 0) {
            cout << i << " ";
        }
        i++;
    }

    return 0;
}

n = int(input("Enter the value of n: "))
m = int(input("Enter the value of m: "))

i = 1
# Print numbers divisible by m using a while loop
while i <= n:
    if i % m == 0:
        print(i, end=" ")
    i += 1

<?php
$n = (int)readline("Enter the value of n: ");
$m = (int)readline("Enter the value of m: ");

$i = 1;
# Print numbers divisible by m using a while loop
while ($i <= $n) {
    if ($i % $m == 0) {
        echo $i . " ";
    }
    $i++;
}
?>

import java.util.Scanner;

public class DivisibleNumbers {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the value of n: ");
        int n = sc.nextInt();
        System.out.print("Enter the value of m: ");
        int m = sc.nextInt();

        int i = 1;
        // Print numbers divisible by m using a while loop
        while (i <= n) {
            if (i % m == 0) {
                System.out.print(i + " ");
            }
            i++;
        }

        sc.close();
    }
}

let n = parseInt(prompt("Enter the value of n: "));
let m = parseInt(prompt("Enter the value of m: "));

let i = 1;
// Print numbers divisible by m using a while loop
while (i <= n) {
    if (i % m === 0) {
        console.log(i);
    }
    i++;
}

using System;

class Program {
    static void Main() {
        Console.Write("Enter the value of n: ");
        int n = int.Parse(Console.ReadLine());
        Console.Write("Enter the value of m: ");
        int m = int.Parse(Console.ReadLine());

        int i = 1;
        // Print numbers divisible by m using a while loop
        while (i <= n) {
            if (i % m == 0) {
                Console.Write(i + " ");
            }
            i++;
        }
    }
}

List of All Programs