C. Let the database assign a unique number in a new field
Answer:
// here is code in C++.
#include <bits/stdc++.h>
using namespace std;
// function
string is_perfect(int n)
{
// string variables
string str;
// if number is negative
if (n < 0)
{
str="invalid input.";
}
// if number is positive
else
{
int a=sqrt(n);
// if number is perfect square
if((a*a)==n)
{
str="True";
}
// if number is not perfect square
else
{
str="False";
}
}
// return the string
return str;
}
// main function
int main()
{
// variables
int n;
cout<<"enter the number:";
// read the value of n
cin>>n;
// call the function
string value=is_perfect(n);
// print the result
cout<<value<<endl;
return 0;
}
Explanation:
Read a number from user and assign it to variable "n".Then call the function is_perfect() with parameter n.First check whether number is negative or not. If number is negative then return a string "invalid input".If number is positive and perfect square then return a string "True" else return a string "False".
Output:
enter the number:-3
invalid input.
enter the number:5
False
enter the number:16
True
Answer:
Check the explanation
Explanation:
//GenerateTriangleNumbers.java
import java.util.Arrays;
import java.util.Scanner;
public class GenerateTriangleNumbers {
public static int[] generateTriangleNumbers(int x){
int arr[] = new int[x];
int sum = 0;
for(int i = 1;i<=x;i++){
sum += i;
arr[i-1] = sum;
}
return arr;
}
public static void main( String [] args ){
int n;
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
n = in.nextInt();
System.out.println(Arrays.toString(generateTriangleNumbers(n)));
}
}
Kindly check the attached image below for the code output.