fill an array of size n with the first n ugly numbers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

fill an array of size n with the first n ugly numbers

29th Aug 2020, 8:33 AM
Venkatesh Aanand
Venkatesh Aanand - avatar
3 Answers
0
Yes it is a question.
29th Aug 2020, 8:40 AM
Venkatesh Aanand
Venkatesh Aanand - avatar
0
import java.io.*; class array { int [] arr; int n; void input()throws IOException { DataInputStream in=new DataInputStream(System.in); System.out.println("enter value of n"); n=Integer.parseInt(in.readLine()); arr=new int[n]; } int check(int x) { if(x%2==0||x%3==0||x%5==0) return 1; else return 0; } public static void main(String a[])throws IOException { DataInputStream in=new DataInputStream(System.in); input(); int c=0; int no=1; for(int i=0;i<=n;) { int r=check(no); if(r==1) { arr[i]=no; c++; no++; i++; } } System.out.print("the array:"); for(int i=0;i<=n;i++) { System.out.print(arr[i]+" "); } } }
29th Aug 2020, 8:40 AM
Venkatesh Aanand
Venkatesh Aanand - avatar
0
This was my attempt. Can you please spot the error.
29th Aug 2020, 8:41 AM
Venkatesh Aanand
Venkatesh Aanand - avatar