Program 3
Multiple of 3 and 5
You are required to write the logical part for the function that takes an integer ‘n’ as input and returns a string array ‘answer’ of length ‘n’. Each element ‘answer[i]’ in the array is determined based on the following rules:
If ‘i’ is divisible by both 3 and 5, ‘answer[i]’ should be set to “PrepInsta”.
If ‘i’ is divisible by 3 but not by 5, ‘answer[i]’ should be set to “Prep”.
If ‘i’ is divisible by 5 but not by 3, ‘answer[i]’ should be set to “Insta”.
If none of the above conditions are true, ‘answer[i]’ should be set to the string representation of ‘i’.
Incorrect Code
Correct Code
Incorrect Code
vectorprepInsta(int n) { vector st; for(int i=1;i<=n;i++){ Write your code here// } }
Correct Code
vectorprepInsta(int n) { vector st; for(int i=1;i<=n;i++){ if (i%15==0){ st[i]=”PrepInsta”; } else if(i%5==0){ st[i]=”Prep” } else if (i%3==0){ st[i]=”insta”; } else { st[i]=toString(i); } } }
for(int i=1;i<=n;i++){
if(i%3==0 && i%5==0){
ans[i-1]="PrepInsta";
}else if(i%3==0 && i%5!=0){
ans[i-1]="Prep";
}else if(i%3!=0 && i%5==0){
ans[i-1]="Insta";
}
else{
ans[i – 1] = String.valueOf(i);
}
if (i%3 == 0) then “Prep” right?
Join our Discord for all your technical queries