Sunteți pe pagina 1din 14

//1.

MOBIUS FUNCTION
import java.util.*;
public class mobiusfn
{
int n;
mobiusfn()
{
n=0;
}

void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter a number");
n=sc.nextInt();
}

int primefac()
{
int a=n, i=2, m=0, c=0, f=0;
while(a>1)
{
c=0;
while(a%i==0)
{
c++;
f++;
a=a/i;
i++;
}
if(c>1)
return 0;
}
return f;
}
void display()
{
int mob,x;
if(n == 1)
mob = 1;
else
{
x = primefac();
if(x == 0)
mob = 0;
else
mob = (int)Math.pow(-1,x);
}
System.out.println("Value of Mobius Function : "+mob);
}

public static void main(String args[])


{
mobiusfn ob = new mobiusfn();
ob.input();
ob.display();
}
}
// 2. FASCINATING NUMBERS
import java.util.*;
class fascinating_numbers
{
public static void main(String[] args)
{
int i,j,n;
int flag;
String c="123456789";
String a="",str2="";
Scanner sc=new Scanner(System.in);
System.out.println("Please input a number to check whether it is a fascinating
number");
n=sc.nextInt();
for(i=1;i<=3;i++)
{
a=a+(n*i);
}
System.out.println(a);
for(char ch='1';ch<='9';ch++)
{
for(j=0;j<a.length();j++)
{
if(ch==a.charAt(j))
str2=str2+a.charAt(j);
}
}

if(str2.equalsIgnoreCase(c))
System.out.println("Fascinating Number");
else
System.out.println("Not a Fascinating Number");
}
}
// 3. PRONIC NUMBER
import java.util.*;
class pronic_number
{
public static void main(String [] args)
{
int n,i,k=1;
int flag=0;
Scanner sc =new Scanner(System.in);
System.out.println("input a number to check whether it is a pronic number or
not.");
n=sc.nextInt();
for(i=0;i<n;i++)
{
if((i*k)==n)
flag=1;
k++;
}
if(flag==1)
System.out.println(n+" is a Pronic Number.");
else
System.out.println(n+" is not a Pronic Number.");
}
}
// 4. DECIMAL TO HEXADECIMAL
import java.util.*;
class decitohexa
{
public static void main(String[] args)
{
int i,d,n;
String s="";
Scanner sc =new Scanner(System.in);
System.out.println("please give a number");
n=sc.nextInt();
for(i=n;i>0;i=i/16)
{
d=i%16;
if(d>9)
{
switch(d)
{
case 10:
s='A'+s;
break;
case 11:
s='B'+s;
break;
case 12:
s='C'+s;
break;
case 13:
s='D'+s;
break;
case 14:
s='E'+s;
break;
case 15:
s='F'+s;
break;
}
}
else
{
s=d+s;
}
}
System.out.println("the hexadecimal value of "+n+" is "+s);
}
}
// 5. FINDDAYS
import java.util.*;
public class finddays
{
public void main(String[] args)
{
int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
finddays ob=new finddays();
Scanner sc=new Scanner(System.in);
System.out.println("give date1");
System.out.println("give date");
int dt1=sc.nextInt();
System.out.println("give month");
int mn1=sc.nextInt();
System.out.println("give year");
int yr1=sc.nextInt();
System.out.println("give date2");
System.out.println("give date");
int dt2=sc.nextInt();
System.out.println("give month");
int mn2=sc.nextInt();
System.out.println("give year");
int yr2=sc.nextInt();
if(validdate(dt1,mn1,yr1) && validdate(dt2,mn2,yr2))
{
month[2]=ob.isleapyr(yr1);
int a=ob.dayno(dt1,mn1,yr1);
month[2]=ob.isleapyr(yr2);
int b=ob.dayno2(dt2,mn2,yr2);
if(yr2!=yr1)
{
double tot=Math.abs((yr1+1)-(yr2-1))*365.25;
double totdays=tot+a+b;
System.out.println(totdays);
}
else
{
int totdays=a+b;
System.out.println(totdays);
}

}
}

int isleapyr(int yr)


{
if(yr%400==0||yr%4==0)
{
return 29;
}
else
{
return 28;
}
}

boolean validdate(int d,int m,int y)


{
if(y<0||y>9999||m<=0||m>12||d<1||d>31)

return false;

else
return true;
}

int month[];
int dayno(int d,int m,int y)
{
int d1=0;
for(int i=m;i<13;i++)
{
d1=month[i]+d1;
}
return d1;
}

int dayno2(int d,int m,int y)


{
int d1=0;
for(int i=m;i<13;i++)
{
d1=month[i]+d1;
}
d1=d1-(month[m]-d);
return d1;
}
}
// 6. DOUBLE DIMENSION ARRAY
import java.util.*;
public class ddasort
{
int r,c,i,j,k,tmp,max,n;
Scanner sc= new Scanner(System.in);
public void main (String[] args)
{
System.out.println("give the size of array");
n=sc.nextInt();
if (n<0 && n>20)
{
System.out.println("Invalid row column number");
System.exit(0);
}
else
{
int a[][]=new int[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]);
}
System.out.println();
}
System.out.println();System.out.println();
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]>max)
{
max=a[i][j];
r=i+1;
c=j+1;
}
}
}
System.out.println("the largest element "+max+" is in row "+r+" and column "+c);
int p=max;
max=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]>max && a[i][j]!=p)
{
max=a[i][j];
r=i+1;
c=j+1;
}
}
}
System.out.println("the second largest element "+max+" is in row "+r+" and
column "+c);

// sorting the array


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n-j-1;k++)
{
if(a[i][k]>a[i][k+1])
{
tmp=a[i][k];
a[i][k]=a[i][k+1];
a[i][k+1]=tmp;
}
}
}
}

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]);
}
System.out.println();
}
}
}
}
// 7. ARRAY
import java.util.*;
public class arrayy
{
public static void main(String[]args)
{
int tmp,i,j,n,m;
Scanner sc=new Scanner(System.in);
System.out.println("input the number of rows in the array");
m=sc.nextInt();
System.out.println("input the number of columns in the array");
n=sc.nextInt();
if(m>20&&n>20 || m<2&&n<2)
{
System.out.println("Invalid row column number");
System.exit(0);
}
else
{
int a[][]=new int[m][n];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
// display original array
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]);
}
System.out.println();
}
// sorting the border elements
for(j=0;j<n;j++)
{
for(i=0;i<m;i++)
{
if(a[j][i]>a[j][m-1])
{
tmp=a[j][i];
a[j][i]=a[j][m-1];
a[j][m-1]=tmp;
}
}

}
System.out.println(); System.out.println();

// display the sorted array


for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]);
}
System.out.println();
}
}
}
}

S-ar putea să vă placă și