Python Assignment Programs
Python Assignment Programs
Assignment 1:
# Assignment 1 Set A
# Q.1 Python Program to Calculate the Area of a Triangle
length = int(input("enter the length:"))
breadth = int(input("enter the breadth:"))
area = 1/2 * length * breadth
print("the area of rectangle is :",area)
OUTPUT:
enter the length:3
enter the breadth:5
the area of rectangle is : 7.5
# Assignment 1 Set A # Q.2 Python Program to Swap two Variable x = 5 y = 10 # To take inputs from the user x = input('Enter value of x: ') #y = input('Enter value of y: ') # create a temporary variable and swap the values temp = x x = y y = temp print('The value of x after swapping: {}'.format(x)) print('The value of y after swapping: {}'.format(y))
OUTPUT:
Enter value of x: 2
Enter value of y: 4
The value of x after swapping: 4
The value of y after swapping: 2
# Assignment 1 Set A # Q.3 Python program to generate random number import random print(random.randint(0,100))
OUTPUT:
50
# Assignment 1 Set B # Q.1 Write a Python Program to check if a number is Positive, Negative, or Zero num = int(input("enter the number:")) if num == 0: print("numebr is zero") elif num > 0: print("number is positive") else: print("numebr is negative")
OUTPUT:
enter the number:-333 numebr is negative
# Assignment 1 Set B # Q.2 Write a Python Program to Check if a Number is Odd or Even num = int(input("Enter the number:")) if num % 2 == 0 : print("number is even") else: print("number is odd")
OUTPUT:
Enter the number:88 number is even
# Assignment 1 Set B # Q.3 Write a Python Program to Check Prime Number 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")
OUTPUT:
Enter a number: 50
50 is not a prime number
# Assignment 1 Set B # Q.4 Write a Python Program to Check Armstrong Number num = int(input("Enter the number:")) order = len(str(num)) sum = 0 # find the sum of the cube of each digit temp = num while temp > 0: digit = temp % 10 sum += digit ** order temp //= 10 # display the result if num == sum: print(num,"is an Armstrong number") else: print(num,"is not an Armstrong number")
OUTPUT:
Enter the number:1634 1634 is an Armstrong number
# Assignment 1 Set B
# Q.5 Write a Python Program to Find a Factorial of a Number
num = int(input("Enter a number: "))
factorial = 1
# check if the number is negative, positive or zero
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
OUTPUT:
Enter a number: 5
The factorial of 5 is 120
public interface Department{
public void getDetpName();
public void getDetpHead();
}
class Hostel{
protected String hname,hlocation;
int noofroom;
void getHostelName(){
System.out.println("Name Of the Hostel : " + hname);
}
void getHostelLocation(){
System.out.println("Hostel Location : " + hlocation);
}
void getNoOfRoom(){
System.out.println("Total Room : " + noofroom);
}
}
import java.util.*;
class Student extends Hostel implements Department
{
String sname,regno,elesub;
String deptName,deptHead;
int avgMarks;
void getStudentName(){
System.out.println("Student : " + sname);
}
String getStudentRegNo(){
return regno;
}
void getElectiveSubject(){
System.out.println("Elective Subject : " + elesub);
}
void getAvgMarks(){
System.out.println("Average Marks : " + avgMarks);
}
public void getDetpName(){
System.out.println("Department Name : " + deptName);
}
public void getDetpHead(){
System.out.println("Department Head : " + deptHead);
}
void addStudent(){
Scanner sc=new Scanner(System.in);
System.out.print("Enter Student name : ");
sname=sc.nextLine();
System.out.print("Enter Registration Number : ");
regno=sc.nextLine();
System.out.print("Enter Elective Subject : ");
elesub=sc.nextLine();
System.out.print("Enter Hostel Name : ");
hname=sc.nextLine();
System.out.print("Enter Hostel Location : ");
hlocation=sc.nextLine();
System.out.print("Enter Department Name : ");
deptName=sc.nextLine();
System.out.print("Enter Department Head : ");
deptHead=sc.nextLine();
System.out.print("Enter No of room : ");
noofroom=sc.nextInt();
System.out.print("Enter Avg Marks : ");
avgMarks=sc.nextInt();
}
void migrate(){
Scanner sc=new Scanner(System.in);
System.out.print("Enter new Department Name : ");
deptName=sc.nextLine();
System.out.print("Enter new Department Head : ");
deptHead=sc.nextLine();
}
void display(){
getStudentName();
System.out.println(" Student Registration No is : " + getStudentRegNo());
getElectiveSubject();
getAvgMarks();
getDetpName();
getDetpHead();
}
}
import java.util.*;
class StudentMaster{
public static void main(String []args){
Scanner sc=new Scanner(System.in);
Student []st=new Student[100];
int sno=0;
String rno;
int ch;
boolean b;
while(true){
System.out.println("\n 1. Admit a student");
System.out.println(" 2. Migrate a student");
System.out.println(" 3. Display");
System.out.println(" 4. Exit");
System.out.println(" 5. Enter Your Choice");
ch=sc.nextInt();
switch(ch){
case 1:
st[sno]=new Student();
st[sno++].addStudent();
break;
case 2:
System.out.println("Enter Registration no : ");
rno=sc.next();
b=false;
for(int i=0;i<sno;i++){
if(st[i].getStudentRegNo().equals(rno)){
b=true;
st[0].migrate();
break;
}
}
if(b==false)
{
System.out.println("Student Not Found");
}
break;
case 3:
System.out.println("Enter Registration no : ");
rno=sc.next();
b=false;
for(int i=0;i<sno;i++){
if(st[i].getStudentRegNo().equals(rno)){
b=true;
st[0].display();
break;
}
}
if(b==false){
System.out.println("Student Not Found");
}
break;
case 4:
System.exit(0);
default:
System.out.println("--Invalid Entry--");
}
}
}
}
// program for accept student info and print using percentage sorting
ReplyDeleteimport java.util.Scanner;
class Student_Info
{
int rollno;
String name;
float per;
static int count = 0;
Student_Info()
{
rollno = 0;
name = null;
per = 0.0f;
}
void accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Plz Enter the id, name and percentage:");
rollno = sc.nextInt();
name = sc.next();
per = sc.nextFloat();
}
void display()
{
System.out.println("roll no :"+rollno);
System.out.println("name :"+name);
System.out.println("percentage :"+per);
}
static void bubblesort(Student_Info s[] )
{
Student_Info temp = new Student_Info();
for(int i=0; i<5-1; i++)
for(int j=i+1; j<=5-1; j++)
{
if(s[i].per > s[j].per)
{
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
public static void main(String ar[])
{
Student_Info sj[] = new Student_Info[5];
for(int i=0; i<5; i++)
{
sj[i] = new Student_Info();
sj[i].accept();
}
Student_Info.bubblesort(sj);
System.out.println("\n Display\n");
for(int i=0; i<5; i++)
{
sj[i].display();
}
}
}
The upper case Strig is:$s2";
ReplyDelete$s3 = $s1.$s2;
echo "
The String concatenation of s1 and s2 is :$s3";
echo "
The uppercase 1st character of 1st string is :", ucfirst($s1);
echo "
The uppercase 1st character of every word in string is :", ucwords($s1);
echo "
The length of first string is :", strlen($s1);
echo "
The length of second string is :", strlen($s2);
?>