NPTEL 2024 Problem Solving Through Programming In C Week 8 : Programming Assignment 1-5

 



Week 8 : Programming Assignment 1

Write a C Program to find HCF of 4 given numbers using recursive function
Select the Language for this assignment. 
#include<stdio.h>
int HCF(int, int); //You have to write this function which calculates the HCF. 
     
int main()
{
   int a, b, c, d, result;
   scanf("%d %d %d %d", &a, &b, &c, &d); /* Takes 4 number as input from the test data */
   result = HCF(HCF(a, b), HCF(c,d));
   printf("The HCF is %d", result);
}
 
//Complete the rest of the program to calculate HCF
int HCF(int x, int y)
{
   while (x != y)
   {
      if (x > y)
       {
           return HCF(x - y, y);
       }
       else
       {
       return HCF(x, y - x);
       }
    }
    return x;
}
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
100
30
50
70
The HCF is 10
The HCF is 10
Passed
Test Case 2
49
56
147
21
The HCF is 7
The HCF is 7
Passed







Week-08 Program-02

Due on 2024-03-21, 23:59 IST
Write a C Program to print Binary Equivalent of an Integer using Recursion
Select the Language for this assignment. 
#include <stdio.h>
int binary_conversion(int); //function to convert binary to decimal number
int main()
  {
  int num, bin;  //num is the decimal number and bin is the binary equivalent for the number
 
  scanf("%d", &num); //The decimal number is taken from the test case data
  bin = binary_conversion(num); //binary number is stored in variable bin
  printf("The binary equivalent of %d is %d", num, bin);
  return 0;
  }
int binary_conversion(int num)
  { 
     if (num == 0)
        {
            return 0;
        }
        else
        {
            return (num % 2) + 10 * binary_conversion(num / 2);
        }
  }
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
10
The binary equivalent of 10 is 1010
The binary equivalent of 10 is 1010
Passed
Test Case 2
257
The binary equivalent of 257 is 100000001
The binary equivalent of 257 is 100000001
Passed






Week-08 Program-03

Due on 2024-03-21, 23:59 IST
Write a C Program to reverse a given word using function.
e.g. INDIA should be printed as AIDNI
Select the Language for this assignment. 
#include<stdio.h>
#include<string.h>
 
void reverse(char[], int, int);
    int main()
    {
        char str1[20];
        scanf("%s", str1); //The string is taken as input form the test data. 
     
/* Complete the program to print the string in reverse order using the function
void reverse(char[], int, int);
Use the printf statement as
printf("The string after reversing is: %s\n", str1); 
You can use different variable also.
*/
reverse(str1, strlen(str1), 0);
        printf("The string after reversing is: %s", str1);
        return 0;
}
 
void reverse(char str1[], int len, int i)
{
  char temp;
  for(int i=0; i<len/2; i++)
  {
    temp = str1[i];
    str1[i] = str1[len-i-1];
    str1[len-i-1] = temp;
  }
  str1[len]='\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
computer
The string after reversing is: retupmoc
The string after reversing is: retupmoc
Passed
Test Case 2
NPTEL
The string after reversing is: LETPN
The string after reversing is: LETPN
Passed





Week 08: Program-04

Due on 2024-03-21, 23:59 IST
Write a C Program to find power of a given number using recursion. The number and the power to be calculated is taken from test case
Select the Language for this assignment. 
#include <stdio.h>  
long power(int, int);
int main()
{
int pow, num;
long result;
 
scanf("%d", &num); //The number taken as input from test case data 
 
scanf("%d", &pow); //The power is taken from the test case 
result = power(num, pow);
printf("%d^%d is %ld", num, pow, result);
return 0;
}
long power(int num, int pow)
{
if (pow)
{
return (num * power(num, pow - 1));
}
return 1;
}
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
5
2
5^2 is 25
5^2 is 25
Passed
Test Case 2
10
4
10^4 is 10000
10^4 is 10000
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...