0% found this document useful (0 votes)
6 views6 pages

CS Practical 1-4

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)
6 views6 pages

CS Practical 1-4

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/ 6

Computer Science Practical

Practical 1
Aim: Write a program to check whether a given number is prime or not.
Software Required: IDLE Python
Theory: Prime numbers are natural numbers that are divisible by only 1 and the
number itself. In other words, prime numbers are positive integers greater than 1
with exactly two factors, 1 and the number itself.
Algorithm:

STEP 1: Take a number num as input.

STEP 2: Initialize a variable temp to 0.

STEP 3: Iterate a “for” loop from 2 to num/2.

STEP 4: If num is divisible by loop iterator, then increment temp.

STEP 5: If the temp is equal to 0,

Return “Num IS PRIME”.

Else,

Return “Num IS NOT PRIME”.

Program:

num = int(input("Enter a number"))

if num > 1:

for i in range(2, (num//2)+1):

if (num % i) == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")

Output:
1. Enter a number 3
3 is a prime number
2. Enter a number 12
12 is not a prime number

Result :
Thus, we have studied and implemented the program to check whether a given n
umber is prime or not.
Practical 2
Aim: Write a Program to enter the string and to check if it’s palindrome or not
using loop.
Software Required: IDLE Python
Theory: In Python, strings are used for representing textual data. A string is a
sequence of characters enclosed in either single quotes ('') or double quotes
(“”). A palindrome is a word, number, phrase, or other sequence of symbols that
reads the same backwards as forwards, such as madam or racecar
Algorithm:

STEP 1: Take a string str as input.

STEP 2: Initialize a variable temp to 1.

STEP 3: Convert the string to lowercase to make the comparison case-insensitive.

STEP 4: Now, iterate through the string forward and backward, compare one
character at a time till middle of the string is reached.

STEP 5: If any of the character doesn't match, set temp to 0 and break the loop.

Step 6: If the temp is equal to 1,

Return “string is a palindrome.”.

Else,

Return “string is not a palindrome.”

Program:

string = input("Enter a string");


temp_var = True;
string = string.lower();
for i in range(0, len(string)//2):
if(string[i] != string[len(string)-i-1]):
temp_var = False;
break;
if(temp_var):
print("Given string is palindrome");
else:
print("Given string is not a palindrome");
Output:
1. Enter a string Hello
Given string is not a palindrome
2. Enter a string Madam
Given string is palindrome

Result :
Thus, we have studied and implemented the Program to enter the string and to c
heck if it’s palindrome or not using loop.
Practical 3
Aim: Write a program to find sum of elements of List using loop.
Software Required: IDLE Python
Theory: A list is a data structure in Python that is a mutable, or changeable,
ordered sequence of elements. Each element or value that is inside of a list is
called an item.
Algorithm:

Step 1: Define a recursive function that calculates the sum of a list.

Step 2: If the list is empty, return 0.

Step 3: Otherwise, return the first element added to the sum of the remaining
list.

Step 4: Print the result of the recursive sum function.


Program:
def findSum(lst,num):
if num==0:
return 0
else:
return lst[num-1]+findSum(lst,num-1)
mylist = []
num = int(input("Enter how many number :"))
for i in range(num):
n = int(input("Enter Element "+str(i+1)+":"))
mylist.append(n)
sum = findSum(mylist,len(mylist))
print("Sum of List items ",mylist, " is :",sum)
Output:
Enter how many number :2
Enter Element 1:12
Enter Element 2:21
Sum of List items [1, 2] is : 33

Result :
Thus, we have studied and implemented the program to find sum of elements of
List using loop.
Practical 4
Aim: Write a Python program to pass a string to a user-defined function and
display the number of vowels present in the string.
Software Required: IDLE Python
Theory: A function is a set of statements that take inputs, do some specific
computation, and produce output. Python provides built-in functions like print(),
etc. but we can also create your own functions. These functions are known
as user defines functions.
Algorithm:
Step 1: Take user input.
Step 2: Initialize count variable.
Step 3: Iterate through the string to find number of vowels.
Step 4: Check if the alphabet of the string lies under the group of vowels.
Step 5: If TRUE increment count by 1.
Step 6: Print count.
Program:

def vowel_count(str):
count = 0
vowel = ['a','e','i','o','u']
for alphabet in str:
if alphabet.lower() in vowel:
count = count + 1
print("No. of vowels :", count)
str = input("Enter a string")
vowel_count(str)
Output:
Enter a stringRainbow
No. of vowels : 3

Result :
Thus, we have studied and implemented program to pass a string to a user-
defined function and display the number of vowels present in the string

You might also like