0% found this document useful (0 votes)
9 views4 pages

DSA Assignment - 1 2

Uploaded by

aryan.23bce8252
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

DSA Assignment - 1 2

Uploaded by

aryan.23bce8252
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DSA Assignment - 1

Thopireddy Aryan Reddy


23BCE8252
1Q. Write a Program to nd the factorial of a given number using
recursion and analyze the me complexity.

import java.util.Scanner;

public class main{

static int Rec(int y){

if(y==0)

return 0;

else if (y==1)

return 1;

else{

return y*Rec(y-1);

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.print("Enter the number for which you want


to find the factorial of : ");

int x = s.nextInt();

System.out.println("The factorial of " + x + " is : "


+ Rec(x));

}
fi
ti
-> Because of the Recursion call The big-o run me for a recursive
func on is equivalent to the number of recursive func on calls.

2Q. Write a Program to nd the transpose of a given matrix and


display its me complexity.

import java.util.Scanner;

public class main{

static void transpose(int n, int m){

Scanner s = new Scanner(System.in);

int[][] A = new int[n][m];

int[][] B = new int[n][m];

for(int i = 0; i<n;++i){

System.out.println("Line : ");

for(int j = 0;j<m;++j){

A[i][j] = s.nextInt();

for(int i = 0; i<n;++i){

for(int j = 0;j<m;++j){

System.out.print(A[i][j]);

System.out.println();

for(int i = 0; i<n;++i){
ti
ti
fi
ti
ti
for(int j = 0;j<m;++j){

B[i][j] = A[j][i];

System.out.println();

for(int i = 0; i<n;++i){

for(int j = 0;j<m;++j){

System.out.print(B[i][j]);

System.out.println();

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.println("Enter the number of rows and


columns you want in the matrix : ");

int n = s.nextInt();

int m = s.nextInt();

transpose(n,m);

The time complexity of this code is O(n^2)


because of the extensive use of nested for-
loop in the program.
1.1Q Write a Program to illustrate the
difference between recursion and iteration
by giving its time complexities.

public static long rec(int n) {

if (n == 0 || n == 1) {

return 1;

} else {

return n * rec(n - 1);

public static long itr(int n) {

long result = 1;

for (int i = 2; i <= n; i++) {

result *= i;

return result;

-> Both of their time complexities is O(n).


As one program uses a for loop.
And the other uses

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy