NPTEL Problem Solving Through Programming In C Week 5 : Programming Assignment 1 2 3 and 4

 

Week 5 : Programming Assignment 1

Due on 2023-08-31, 23:59 IST
Write a C program to check whether a given number (N) is a perfect number or not.
[Perfect Number - A perfect number is a positive integer number which is equals to the sum of its proper positive divisors. For example 6 is a perfect number because its proper divisors are 1, 2, 3 and it’s sum is equals to 6.]
Select the Language for this assignment. 
#include <stdio.h>
int main()
{
    int N; 
    scanf("%d",&N); /* An integer number taken as input from test cases */
 
/*Complete the program by writing the rest of the code in the space provided.
Copy and paste the printf statement given below wherever required to avoid errors.
printf("\n%d is a perfect number.",N);
printf("\n%d is not a perfect number.",N);
*/
int i,sum=0;
for(i=1;i<N;i++)
  if(N%i==0)
   sum+=i;
 
if(sum==N)
  printf("%d is a perfect number.",N);
else
  printf("%d is not a perfect number.",N);
 
return 0;
 
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
6
6 is a perfect number.
6 is a perfect number.
Passed
Test Case 2
87
87 is not a perfect number.
87 is not a perfect number.
Passed


Week 5 : Programming Assignment 2

Due on 2023-08-31, 23:59 IST
Write a C program to count total number of digits of an Integer number (N).
Select the Language for this assignment. 
#include <stdio.h>
 int main()
{
    int N; 
    scanf("%d",&N); /*The number is accepted from the test case data*/
 
/* Complete the rest of the code. Please use the printf statements as below
by just changing the variables used in your program 
 
printf("The number %d contains %d digits.",N,count);
 
*/
int temp, count; 
count=0;
    temp=N;
    while(temp>0)
    {
        count++;
        temp/=10;
    }
     printf("The number %d contains %d digits.",N,count);
}
 
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
3008
The number 3008 contains 4 digits.
The number 3008 contains 4 digits.
Passed
Test Case 2
123456
The number 123456 contains 6 digits.
The number 123456 contains 6 digits.
Passed


Week 5 : Programming Assignment 3

Due on 2023-08-31, 23:59 IST
Write a C program to check whether the given number(N) can be expressed as Power of Two (2) or not.
For example 8 can be expressed as 2^3.
Select the Language for this assignment. 
#include <stdio.h>
int main()
{
    int N;
    scanf("%d",&N); /* The value of N is taken from the test case data */
 
/* Complete the code.
Use the printf statements as below
printf("%d is a number that can be expressed as power of 2.",N);
printf("%d cannot be expressed as power of 2.",N);
*/
int temp, flag;
    temp=N;
    flag=0;
   
    while(temp!=1)
    {
        if(temp%2!=0){
            flag=1;
            break;
        }
        temp=temp/2;
    }
  
    if(flag==0)
        printf("%d is a number that can be expressed as power of 2.",N);
    else
        printf("%d cannot be expressed as power of 2.",N);
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
8
8 is a number that can be expressed as power of 2.
8 is a number that can be expressed as power of 2.
Passed
Test Case 2
66
66 cannot be expressed as power of 2.
66 cannot be expressed as power of 2.
Passed


Week 5 : Programming Assignment 4

Due on 2023-08-31, 23:59 IST
Write a C program to find sum of following series where the value of N is taken as input

 1+ 1/2 + 1/3 + 1/4 + 1/5 + .. 1/N

Select the Language for this assignment. 
#include<stdio.h>
int main()
{
int N;
float sum = 0.0;
scanf("%d",&N); /*Read the value of N from test cases provided*/
 
/* Complete the program. Please use the printf statement given below:
 
printf("Sum of the series is: %.2f\n",sum);
 
*/
int i;
for(i=1;i<=N;i++)
sum = sum + ((float)1/(float)i);
printf("Sum of the series is: %.2f", sum);
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
6
Sum of the series is: 2.45
Sum of the series is: 2.45
Passed
Test Case 2
50
Sum of the series is: 4.50
Sum of the series is: 4.50
Passed

No comments

JavaFX Scene Builder

  This is an article about the JavaFX Scene Builder. You will get a short introduction about the installation and usage of the software. The...