In: Computer Science
Write a code in two ways to generate all multiple of n: from 0
to 158 included.
1) in procedural style,
2) in Object Oriented style
Input: just a value of n between 11 and 19 included.
1) procedural style :
code :
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n between 11 and 19 included : ");
scanf("%d", &n); // read the value of n
printf("Result : ");
for(int i = 1; i < 158; i++)
{
if(i % n == 0) // for multiples
{
printf("%d ", i);
}
}
return 0;
}
output :
2) object oriented style :
code :
import java.util.*;
public class Main
{
public static void print(int n)
{
System.out.println("Result : ");
for(int i = 1; i < 158; i++)
{
if(i % n == 0) // for multiples
{
System.out.print(i + " ");
}
}
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n;
System.out.print("Enter the value of n between 11 and 19 included :
");
n = s.nextInt(); // read the value of n
print(n); // call the fucntion
}
}
output: