NPTEL Programming In Java Programming Assignment July-2024 Swayam Week-1

 

            NPTEL » Programming in Java


  Please scroll down for latest Programs. 👇 


Week 01 : Programming Assignment 1

Due on 2024-08-08, 23:59 IST

Write a Java program to print the area and perimeter of a rectangle.

Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
public class W01_P1 { 
   public static void main(String[] strings) {
       double width ;
       double height;
 
       Scanner in = new Scanner(System.in);
       width = in.nextDouble();
       height = in.nextDouble();
// Calculate the perimeter of the rectangle
double perimeter = 2 * ( width + height );
 
// Calculate the area of the rectangle
double area = width * height ;
// Print the calculated perimeter using placeholders for values
       System.out.printf("Perimeter is 2*(%.1f + %.1f) = %.2f\n", height, width, perimeter);
 
// Print the calculated area using placeholders for values
       System.out.printf("Area is %.1f * %.1f = %.2f", width, height, area);    
   }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5.6 8.5
Perimeter is 2*(8.5 + 5.6) = 28.20\n
Area is 5.6 * 8.5 = 47.60
Perimeter is 2*(8.5 + 5.6) = 28.20\n
Area is 5.6 * 8.5 = 47.60
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 01 : Programming Assignment 2

Due on 2024-08-08, 23:59 IST

Write a Java program and compute the sum of an integer's digits.

Your last recorded submission was on 2024-07-22, 08:13 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
public class W01_P2 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
 
        // Prompt the user to input an integer
        //System.out.print("Input an integer: ");
 
        // Read the integer from the user
        long n = input.nextLong();
 
        // Calculate and display the sum of the digits
        System.out.print("The sum of the digits is: " + sumDigits(n));
    }
// Calculate the sum of the digits by defining a sumDigits() function
// This should return the sum
public static int sumDigits(long n){
  int sum = 0;
  while( n!=0 )
  {
    sum += n%10;
    n/=10;
  }
  return sum;
}
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
00020
The sum of the digits is: 2
The sum of the digits is: 2
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 01 : Programming Assignment 3

Due on 2024-08-08, 23:59 IST

Write a Java program to display n terms of natural numbers and their sum.

(Remember to match the output given exactly, including the spaces and new lines)

(passed with presentation error means you will get full marks)

Your last recorded submission was on 2024-07-22, 08:19 IST
Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
 
public class W01_P3 {
 
  public static void main(String[] args)
 
  {
    int i, n, sum = 0;
      Scanner in = new Scanner(System.in);
      // System.out.print("Input number: ");
      n = in.nextInt();
    System.out.printf("The first %d natural numbers are : \n",n);
// Use loop to display n natural numbers
//sum the natural numbers up to n.
for ( i = 1; i <=n ; i++)
{
  System.out.println(i);
  sum +=i;
}
System.out.printf("The Sum of Natural Number upto %d terms : %d",n,sum);
  }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
5
The first 5 natural numbers are : \n
1\n
2\n
3\n
4\n
5\n
The Sum of Natural Number upto 5 terms : 15
The first 5 natural numbers are : \n
1\n
2\n
3\n
4\n
5\n
The Sum of Natural Number upto 5 terms : 15
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 01 : Programming Assignment 4

Due on 2024-08-08, 23:59 IST

Write a Java program to make such a pattern like a right angle triangle with the number increased by 1.

(Remember to match the output given exactly, including the spaces and new lines)

(Ignore presentation errors for this and all future programming assignments)
(passed with presentation error means you will get full marks)

for n=3:

2 3

4 5 6

Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
 
public class W01_P4 {
 
  public static void main(String[] args)
 
  {
    int i, j, n, k = 1;
 
    //System.out.print("Input number of rows : ");
 
    Scanner in = new Scanner(System.in);
    n = in.nextInt();
//use nested for loop for printing pattern like a right angle triangle with the number increased by 1.
for( i = 1 ; i <= n ; i++)
{
  for( j = 1; j <= i ; j++)
  {
    System.out.print(k + " ");
    k++;
  }
  System.out.print("\n");
}
}
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
4
1 \n
2 3 \n
4 5 6 \n
7 8 9 10
1 \n
2 3 \n
4 5 6 \n
7 8 9 10 \n
Passed after ignoring Presentation Error





Private Test cases used for EvaluationStatus
Test Case 1
Passed



Week 01 : Programming Assignment 5

Due on 2024-08-08, 23:59 IST

Write a Java program to convert an integer number to a binary number.

Select the Language for this assignment. 
File name for this program : 
import java.util.Scanner;
 
public class W01_P5 {
    public static void main(String args[]) {
        // Declare variables to store decimal number, quotient, and an array for binary
        // digits
        int dec_num, quot, i = 1, j;
        int bin_num[] = new int[100];
 
        // Create a Scanner object to read input from the user
        Scanner scan = new Scanner(System.in);
 
        // Prompt the user to input a decimal number
        // System.out.print("Input a Decimal Number: ");
        dec_num = scan.nextInt();
 
        // Initialize the quotient with the decimal number
        quot = dec_num;
// Convert the decimal number to binary and store binary digits
for( ; quot!=0; i++){
  bin_num[i] = quot%2;
  quot/=2;
}
// Display the binary representation of the decimal number
        System.out.print("Binary number is: ");
        for (j = i - 1; j > 0; j--) {
            System.out.print(bin_num[j]);
        }
        //System.out.print("\n");
    }
}
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
47
Binary number is: 101111
Binary number is: 101111
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed

No comments

VR23 Web Technologies Lab MCA II Year I Semester Experiment 7:

  Aim: PHP script to a) Find the length of a string. b) Count no of words in a string. c) Reverse a string. d) Search for a specific string....