DAA Programs

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

// Permutation problem :

import java.util.Scanner;
class Permutations
{
public static void permutation(String prefix, String remaining) {
int n = remaining.length();
if (n == 0)
{
System.out.println(prefix);
}
else
{
for (int i = 0; i < n; i++)
{
permutation(prefix + remaining.charAt(i), remaining.substring(0, i)
+ remaining.substring(i + 1, n));
}
}
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = input.nextLine();
System.out.println();
permutation("", str);
}
}

// Euler's Totient

import java.util.Scanner ;
class Main
{
public static int phi(int n)
{
int result = n;
for (int p = 2; p * p <= n; ++p)
{
if (n % p == 0)
{
while (n % p == 0)
n /= p;
result -= result / p;
}
}
if (n > 1)
result -= result / n;
return result;
}
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in) ;
int n = sc.nextInt() ;
System.out.print( phi(n) ) ;
}
}
// Sort students based on average marks

import java.util.Scanner;

class Student {
int rollNo;
String name;
int marks1, marks2, marks3;
int totalMarks;
double averageMarks;

public Student(int rollNo, String name, int marks1, int marks2, int marks3) {
this.rollNo = rollNo;
this.name = name;
this.marks1 = marks1;
this.marks2 = marks2;
this.marks3 = marks3;
this.totalMarks = marks1 + marks2 + marks3;
this.averageMarks = (double) totalMarks / 3;
}

public void display() {


System.out.printf("%d\t%s\t%d\t%.2f\n", rollNo, name, totalMarks,
averageMarks);
}
}

public class Main {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Student[] students = new Student[n];
for (int i = 0; i < n; i++) {
int rollNo = sc.nextInt();
String name = sc.next();
int marks1 = sc.nextInt();
int marks2 = sc.nextInt();
int marks3 = sc.nextInt();
Student student = new Student(rollNo, name, marks1, marks2, marks3);
students[i] = student;
}
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (students[i].averageMarks < students[j].averageMarks)
{
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
for (int i = 0; i < n; i++)
{
students[i].display();
}
}
}

You might also like