Encode, compile and run the following programs and do the following:
1. Identify the format of the function used: no return type & no parameter, with return type & no parameter, no return type & with parameter and with return type & with parameter.
2. Identify the format and actual parameters.
3. Identify the function signature
4. Identify parameter if value parameter, reference parameter or constant reference parameter is used.
5. Identify the the scope of variable used.
6. Identify the type of C++ function is used.
*Note see picture: https://drive.google.com/file/d/1BMDWtJ-Rn32VD8JYDIXCIK2I73AL3zOQ/view?usp=sharing
code (1) "Ch6_Largest.cp" "download the code"
https//drive.google.com/file/d/1s_57xZgbzTUs2MExfziY36jkMPp7svYq/view?usp=sharing
use this for example: https://drive.google.com/file/d/18Qsl8Kc1yn6VU-jLIqgFgJgRxJdjqMQc/view?usp=sharing
download them first.
#include <stdio.h>
void checkPrimeNumber();
int main()
{
checkPrimeNumber(); // argument is not passed
return 0;
}
// return type is void meaning doesn't return any value
void checkPrimeNumber()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2; i <= n/2; ++i)
{
if(n%i == 0)
{
flag = 1;
}
}
if (flag == 1)
printf("%d is not a prime number.", n);
else
printf("%d is a prime number.", n);
}
The checkPrimeNumber() function takes input from the user, checks whether it is a prime number or not and displays it on the screen.
The empty parentheses in checkPrimeNumber(); statement inside the main() function indicates that no argument is passed to the function.
The return type of the function is void. Hence, no value is returned from the function.
#include <stdio.h>
int getInteger();
int main()
{
int n, i, flag = 0;
// no argument is passed
n = getInteger();
for(i=2; i<=n/2; ++i)
{
if(n%i==0){
flag = 1;
break;
}
}
if (flag == 1)
printf("%d is not a prime number.", n);
else
printf("%d is a prime number.", n);
return 0;
}
// returns integer entered by the user
int getInteger()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
return n;
}
The empty parentheses in the n = getInteger(); statement indicates that no argument is passed to the function. And, the value returned from the function is assigned to n.
Here, the getInteger() function takes input from the user and returns it. The code to check whether a number is prime or not is inside the main() function.
#include <stdio.h>
void checkPrimeAndDisplay(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
// n is passed to the function
checkPrimeAndDisplay(n);
return 0;
}
// return type is void meaning doesn't return any value
void checkPrimeAndDisplay(int n)
{
int i, flag = 0;
for(i=2; i <= n/2; ++i)
{
if(n%i == 0){
flag = 1;
break;
}
}
if(flag == 1)
printf("%d is not a prime number.",n);
else
printf("%d is a prime number.", n);
}
The integer value entered by the user is passed to the checkPrimeAndDisplay() function.
Here, the checkPrimeAndDisplay() function checks whether the argument passed is a prime number or not and displays the appropriate message.
#include <stdio.h>
int checkPrimeNumber(int n);
int main()
{
int n, flag;
printf("Enter a positive integer: ");
scanf("%d",&n);
// n is passed to the checkPrimeNumber() function
// the returned value is assigned to the flag variable
flag = checkPrimeNumber(n);
if(flag == 1)
printf("%d is not a prime number",n);
else
printf("%d is a prime number",n);
return 0;
}
// int is returned from the function
int checkPrimeNumber(int n)
{
int i;
for(i=2; i <= n/2; ++i)
{
if(n%i == 0)
return 1;
}
return 0;
}
The input from the user is passed to the checkPrimeNumber() function.
The checkPrimeNumber() function checks whether the passed argument is prime or not.
If the passed argument is a prime number, the function returns 0. If the passed argument is a non-prime number, the function returns 1. The return value is assigned to the flag variable.
Depending on whether flag is 0 or 1, an appropriate message is printed from the main() function.
Comments
Leave a comment