CS Practical 1-4
CS Practical 1-4
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:
Else,
Program:
if num > 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 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.
Else,
Program:
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 3: Otherwise, return the first element added to the sum of the remaining
list.
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