# CS101 ASSIGNMENT 2 SOLUTION 2023

CS101 ASSIGNMENT 1 SOLUTION 2023. CS101 ASSIGNMENT 2 SOLUTION SPRING 2023. The CS101 ASSIGNMENT 2 SOLUTION 2023, CS101 ASSIGNMENT NO 2 SOLUTION 2023, CS101 ASSIGNMENT NO 2 SOLUTION SPRING 2023. CS101 Assignment 2 Solution Spring 2023.

Introduction:

Welcome to the CS101 Assignment 2 Solution for the year 2023. In this assignment, you will be exploring the fundamentals of computer science, including programming concepts and algorithms. The assignment is designed to help you build your problem-solving skills and understand how to write efficient and effective programs. Let’s dive in!

Question 1:

The first question of this assignment focuses on writing a Python program that computes the sum of the first N positive integers. To solve this problem, we can use a simple loop that iterates from 1 to N and accumulates the sum of each number in a variable. The program can be written as follows:

N = int(input(“Enter a positive integer: “))
sum = 0
for i in range(1, N+1):
sum += i
print(“The sum of the first”, N, “positive integers is”, sum)

This program uses a loop to add up the numbers from 1 to N, which results in the sum of the first N positive integers. The `input()` function is used to get the value of N from the user, and the `range()` function is used to create a range of values from 1 to N.

Question 2:

The second question of this assignment is about sorting a list of numbers in ascending order using the Bubble Sort algorithm. The Bubble Sort algorithm is a simple sorting algorithm that works by repeatedly swapping adjacent elements if they are in the wrong order.

To implement the Bubble Sort algorithm in Python, we can write the following program:

def bubbleSort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]

arr = [64, 34, 25, 12, 22, 11, 90]

bubbleSort(arr)

print (“Sorted array is:”)
for i in range(len(arr)):
print (“%d” %arr[i])

This program defines a function called `bubbleSort()` that takes an array of numbers as its argument. The function then uses nested loops to compare adjacent elements in the array and swap them if they are in the wrong order. The sorted array is then printed using a loop that iterates through the elements of the array.

Conclusion:

Congratulations! You have completed the CS101 Assignment 2 for the year 2023. By completing this assignment, you have gained valuable knowledge in the fundamentals of computer science, including programming concepts and algorithms. We hope that you found this assignment challenging and informative, and we encourage you to continue exploring the world of computer science.