An Introduction To Programming Through C++
IIT Bombay.
Week 5- Programming Assignment 2:
The "signature" of a function is the first line which gives the return type, the name of the function and the parameters with their types. As an example the signature of the gcd function is
int gcd(int m, int n)
Write a function with the following signature
bool perfect(int n)
It should return true if the sum of the divisors of n smaller than n add to n. Otherwise it should return false. As an example, for n = 28, the function should return true, because the divisors are 1, 2, 4, 7, 14. To find the sum of the divisors simply go over the numbers from 1 to n-1 and see if they divide n, if yes add that number to a running total that you maintain.
There will invisible code which tests the function. The code will read a value from the test case and print out whether the value read is perfect.
Sample Test Cases
Input Output
Test Case 1
28
1
Test Case 2
6
1
Test Case 3
9
0
Test Case 4
23
0
int gcd(int m, int n)
Write a function with the following signature
bool perfect(int n)
It should return true if the sum of the divisors of n smaller than n add to n. Otherwise it should return false. As an example, for n = 28, the function should return true, because the divisors are 1, 2, 4, 7, 14. To find the sum of the divisors simply go over the numbers from 1 to n-1 and see if they divide n, if yes add that number to a running total that you maintain.
There will invisible code which tests the function. The code will read a value from the test case and print out whether the value read is perfect.
Sample Test Cases
Input Output
Test Case 1
28
1
Test Case 2
6
1
Test Case 3
9
0
Test Case 4
23
0
WRITE/COPY THESE CODE DIRECTLY IN NPTEL TERMINAL AND COMPILE AND RUN THEN SUBMIT.
PROGRAM:bool perfect(int n)
{ int i,j=0;
for(i=1;i<=n-1;i++)
{
if(n%i==0)
{
j=j+i;
}
}
if(j==n)
return true;
else
return false;
}
CLICK ON BELOW LINK.
<<CLICK HERE FOR WEEK 5:PROGRAMMING -01>>
<<CLICK HERE FOR WEEK 6: QUIZ ASSIGNMENT>> FOLLOW OUR WEBSITE FROM THE BUTTON PROVIDED TO THE BOTTOM OF PAGE TO GET SOON ANSWERS OF PROGRAMS.
Disclaimer: Here you can find all nptel assignment solutions related to CS stream.These may help you for your assignment.The answers are only for verification.You cannot copy these directly from the post(code of conduct of NPTEL).These is not 100% correct solutions.
No comments:
Post a Comment
If you have any problems related to solutions or any concept please let me know.