TASKS
• Task 1: Install Microsoft Visual Studio C++ 2022 Community IDE on your laptop.
• Task 2: Write a program that stores the first N square integers (starting from 1) in a single array. You should allow the value N to be entered from the command line and use dynamic allocation to create the array.
Print the array to the screen at the end of your program. Modify your program so that the array can be extended by a number M also entered from the command line and then print it again. Sample output is shown below.
Enter N: 5 1 4 9 16 25
Enter M: 3 1 4 9 16 25 36 49 64
#include <iostream>
#include <math.h>
using namespace std;
void perfectSquares(int n)
{
int i=1;
int count=0;
while(true){
if (sqrt(i) == (int)sqrt(i)){
cout << i << " ";
count++;
}
if(count==n){
break;
}
i++;
}
}
int main()
{
int N;
cout<<"\nEnter N: ";
cin>>N;
perfectSquares(N);
int M;
cout<<"\nEnter M: ";
cin>>M;
perfectSquares(N+M);
return 0;
}
Comments
Leave a comment