0% found this document useful (0 votes)
557 views9 pages

1:-Write A Program in Python To Check The Number Is Prime or Not.?

The documents contain examples of Python programs to check if a number is prime or not, check if a number is even or odd, find the sum of first 10 natural numbers, find the largest number among three numbers, check if an employee is eligible for a bonus based on years of service, determine the oldest and youngest among three ages, create a square and cube of numbers in a list, create a Circle class with methods to calculate area and perimeter, and create a Student class with methods to display student details, set age and set marks.

Uploaded by

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

1:-Write A Program in Python To Check The Number Is Prime or Not.?

The documents contain examples of Python programs to check if a number is prime or not, check if a number is even or odd, find the sum of first 10 natural numbers, find the largest number among three numbers, check if an employee is eligible for a bonus based on years of service, determine the oldest and youngest among three ages, create a square and cube of numbers in a list, create a Circle class with methods to calculate area and perimeter, and create a Student class with methods to display student details, set age and set marks.

Uploaded by

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

1:- Write a program in python to check the number is

prime or not.?
# Program to check if a number is prime or not

num = 29

# To take input from the user


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

# define a flag variable


flag = False

# prime numbers are greater than 1


if num > 1:
# check for factors
for i in range(2, num):
if (num % i) == 0:
# if factor is found, set flag to True
flag = True
# break out of loop
break

# check if flag is True


if flag:
print(num, "is not a prime number")
else:
print(num, "is a prime number")

2:- Example to check whether an integer is a prime number or not using for loop
and if...else statement.?

#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);

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


// condition for non-prime
if (n % i == 0) {
flag = 1;
break;
}
}

if (n == 1) {
printf("1 is neither prime nor composite.");
}
else {
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}

return 0;
}

Output
Enter a positive integer: 29
29 is a prime number.

3:- Write a program to check the number is even or odd?

#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);

// true if num is perfectly divisible by 2


if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);

return 0;
}

Output
Enter an integer: -7
-7 is odd.

4:- Write a program to find the sum of first 10 natural numbers?

#include <stdio.h>

void main()

int j, sum = 0;

printf("The first 10 natural number is :\n");

for (j = 1; j <= 10; j++)

sum = sum + j;

printf("%d ",j);

printf("\nThe Sum is : %d\n", sum);

Copy
Sample Output:
The first 10 natural number is :
1 2 3 4 5 6 7 8 9 10
The Sum is : 55

Write a program in python to print the largest number among three numbers?
# Python program to find the largest number among the three input numbers

# change the values of num1, num2 and num3


# for a different result
num1 = 10
num2 = 14
num3 = 12

# uncomment following lines to take three numbers from user


#num1 = float(input("Enter first number: "))
#num2 = float(input("Enter second number: "))
#num3 = float(input("Enter third number: "))

if (num1 >= num2) and (num1 >= num3):


largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3

print("The largest number is", largest)

Output
The largest number is 14.0

6:- Write an if statement that asks for the user's name via input() function. If the
name is "Bond" make it print "Welcome on board 007." Otherwise make it print
"Good morning NAME". (Replace Name with user's name) .
def count_l(a):
c=0
for i in a.split():
if i[0].lower() == a:
c = c+1
else:
pass

return c
print(count_l(str))

8:- A company decided to give bonus of 5% to employee if his/her year of


service is more than 5 years.

# include<iostream>
using namespace std ;
int serviceyear ,salary ;
   cout<<" Dear employee  please enter ur Salary ";
   cin>>salary ;
   cout<<"Dear employee  please enter ur serviceyear  ";
   cin>>serviceyear;
   if(serviceyear>5)
   {
       cout<<"(Total salary)+(bonus) "<<salary*serviceyear+(salary*5/100);
   }else
   {
9:- Take input of age of 3 people by user and determine oldest and youngest
among them.
#include <stdio.h>
main ()
{
int a,b,c;
printf("\nType the ages of a,b,c:");
//correct format of scanf
scanf("%d %d %d",&a,&b,&c);
if ((a>b) && (a>c))
{
printf("\nThe biggest age is A");
}
else
{
printf("\nThe lesser age is A");
}
//(b>a) && (b>c)
if ((b>a) && (b>c))
{
printf("\nThe biggest age is B");
}
Else
{
printf("\nThe lesser age is B");
}
if ((c>a) && (c>a))
{
printf("\nThe biggest age is C");
}
else
{
printf("\nThe lesser age is C");
}
return 0;
}

10:- You are given with a list of integer elements. Make a new list which will
store square of elements of previous list.

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

print("Original list of integers:")

print(nums)

print("\nSquare every number of the said list:")

square_nums = list(map(lambda x: x ** 2, nums))

print(square_nums)

print("\nCube every number of the said list:")

cube_nums = list(map(lambda x: x ** 3, nums))

print(cube_nums)

Copy
Sample Output:
Original list of integers:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Square every number of the said list:


[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Cube every number of the said list:


[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
11.Create a Cricle class and intialize it with radius. Make two methods
getArea and getCircumference inside this class.

class Circle():

def __init__(self, r):

self.radius = r

def area(self):

return self.radius**2*3.14

def perimeter(self):

return 2*self.radius*3.14

NewCircle = Circle(8)

print(NewCircle.area())

print(NewCircle.perimeter())

Copy
Sample Output:
200.96
50.24

12:- Create a Student class and initialize it with name and roll number. Make
methods to : 1. Display - It should display all informations of the student. 2.
setAge - It should assign age to student 3. setMarks - It should assign marks to
the student.

class Student():

def __init__(self,name,roll):

self.name = name

self.roll= roll

def display(self):

print self.name

print self.roll

def setAge(self,age):

self.age=age

def setMarks(self,marks):

self.marks = marks

You might also like