Program to Print Numbers Between 1 and n Divisible By m using For loop
- Write a program to Program to Print Numbers Between 1 and n Divisible By m using For loop in C
- Write a program to Program to Print Numbers Between 1 and n Divisible By m using For loop in C++
- Write a program to Program to Print Numbers Between 1 and n Divisible By m using For loop in Python
- Write a program to Program to Print Numbers Between 1 and n Divisible By m using For loop in PHP
- Write a program to Program to Print Numbers Between 1 and n Divisible By m using For loop in Java
- Write a program to Program to Print Numbers Between 1 and n Divisible By m using For loop in Java Script
- Write a program to Program to Print Numbers Between 1 and n Divisible By m using For loop in C#
Explanation:
To print numbers between 1 and n that are divisible by m using a for loop, follow these steps:
Steps:
- Take the input values for n and m.
- 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 For loop
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h>
int main() {
int n, m;
printf("Enter n: ");
scanf("%d", &n);
printf("Enter m: ");
scanf("%d", &m);
// Loop to print numbers divisible by m between 1 and n
printf("Numbers divisible by %d between 1 and %d are:\n", m, n);
for (int i = 1; i <= n; i++) {
if (i % m == 0) {
printf("%d ", i);
}
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n, m;
cout << "Enter n: ";
cin >> n;
cout << "Enter m: ";
cin >> m;
// Loop to print numbers divisible by m between 1 and n
cout << "Numbers divisible by " << m << " between 1 and " << n << " are:" << endl;
for (int i = 1; i <= n; i++) {
if (i % m == 0) {
cout << i << " ";
}
}
return 0;
}
n = int(input("Enter n: "))
m = int(input("Enter m: "))
# Loop to print numbers divisible by m between 1 and n
print(f"Numbers divisible by {m} between 1 and {n} are:")
for i in range(1, n+1):
if i % m == 0:
print(i, end=" ")
<?php
$n = (int)readline("Enter n: ");
$m = (int)readline("Enter m: ");
// Loop to print numbers divisible by m between 1 and n
echo "Numbers divisible by $m between 1 and $n are:\n";
for ($i = 1; $i <= $n; $i++) {
if ($i % $m == 0) {
echo "$i ";
}
}
?>
import java.util.Scanner;
public class DivisibleNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter n: ");
int n = scanner.nextInt();
System.out.print("Enter m: ");
int m = scanner.nextInt();
// Loop to print numbers divisible by m between 1 and n
System.out.println("Numbers divisible by " + m + " between 1 and " + n + " are:");
for (int i = 1; i <= n; i++) {
if (i % m == 0) {
System.out.print(i + " ");
}
}
scanner.close();
}
}
let n = parseInt(prompt("Enter n:"));
let m = parseInt(prompt("Enter m:"));
// Loop to print numbers divisible by m between 1 and n
console.log(`Numbers divisible by ${m} between 1 and ${n} are:`);
for (let i = 1; i <= n; i++) {
if (i % m === 0) {
console.log(i);
}
}
using System;
class Program {
static void Main() {
Console.Write("Enter n: ");
int n = int.Parse(Console.ReadLine());
Console.Write("Enter m: ");
int m = int.Parse(Console.ReadLine());
// Loop to print numbers divisible by m between 1 and n
Console.WriteLine($"Numbers divisible by {m} between 1 and {n} are:");
for (int i = 1; i <= n; i++) {
if (i % m == 0) {
Console.Write(i + " ");
}
}
}
}