Following Code Calculate The Square Root of a Number Without using sqrt function:
Code::
import java.util.*;
public class Squareroot
{
public static void main(String[] nt)
{ Scanner in=new Scanner(System.in);
double beg=0,end,n;
System.out.print("Enter Number:");
n=in.nextDouble();
end=n;
double prevmid=0,mid=(beg+end)/2;
double dif=Math.abs(mid-prevmid);
while(mid*mid!=n&&dif>0.000005)
{
if(mid*mid>n)
{
end=mid;
}
else
{
beg=mid;
}
prevmid=mid;
mid=(beg+end)/2;
dif=Math.abs(mid-prevmid);
}
System.out.println("Square Root is: "+mid);
}
}
Output::
Following Code will print a Diamond Pattern of User Specified Number of Rows:
Code::
import java.util.*;
public class Pattern
{
public static void main(String[] nt)
{
Scanner in=new Scanner(System.in);
System.out.print("Enter number of Rows: ");
int n=in.nextInt();
for(int i=1;i<=n;i++)
{
for(int sp=n-i;sp>0;sp--)
{
System.out.print(" ");
}
for(int j=0;j<i;j++)
{
System.out.print("* ");
}
System.out.println();
}
for(int i=n;i>0;i--)
{
for(int sp=0;sp<n-i;sp++)
{
System.out.print(" ");
}
for(int j=0;j<i;j++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
Output::
Following code will define a lower function to convert all letters of a string to lower case:
Code::
def lower(a):
x=""
for i in a:
if(i>='A' and i<'a'):
x=x+((chr((ord(i)+32)))) #ord to convert character to ascii value
else:
x=x+i
return x
i=input("Enter any String :")
print(lower(i))
Output::
Following code defines a function upper which changes the entered string to Upper Case:
Code::
def upper(a):
x="" #for returning Upper Case
for i in a:
if(i>='a'): #for Not Changing Already upper Case Letters
x=x+((chr((ord(i)-32))))
else:
x=x+i
return x
i=input("Enter any String :")
print(upper(i))
Output::
Following Code will convert a number entered by user to the Same Number in Words
Code::
def numtostring(n):
if (n==0):return ""
elif(n==1):return "one"
elif(n==2):return "two"
elif(n==3):return "three"
elif(n==4):return "four"
elif(n==5):return "five"
elif(n==6):return "six"
elif(n==7):return "seven"
elif(n==8):return "eight"
elif(n==9):return "nine"
elif(n==10):return "ten"
elif(n==11):return "eleven"
elif(n==12):return "twelve"
elif(n==13):return "thirteen"
elif(n==14):return "fourteen"
elif(n==15):return "fifteen"
elif(n==16):return "sixteen"
elif(n==17):return "seventeen"
elif(n==18):return "eighteen"
elif(n==19):return "nineteen"
elif(n<=29):return "twenty "+numtostring(n%10)
elif(n<=39):return "thirty "+numtostring(n%10)
elif(n<=49):return "forty "+numtostring(n%10)
elif(n<=59):return "fifty "+numtostring(n%10)
elif(n<=69):return "sixty "+numtostring(n%10)
elif(n<=79):return "seventy "+numtostring(n%10)
elif(n<=89):return "eighty "+numtostring(n%10)
elif(n<=99):return "ninety "+numtostring(n%10)
def convert(n):
if(n<=99):
return numtostring(n)
elif(n<=999):
return numtostring(n//100)+' hundred '+numtostring(n%100)
elif(n<=9999):
return numtostring(n//1000)+' thousand '+numtostring((n//100)%10)+' hundred '+numtostring((n%100))
i=int(input("Enter Any Number Upto 9999: "))
print(convert(i))
Output:
Following Code will print the Equivalent Roman Numeral of Input Number :
Code::
def romandigit(n,a,b,c):
if(n==0):return ""
elif(n==1):return a
elif(n==2):return a+a
elif(n==3):return a+a+a
elif(n==4):return a+b
elif(n==5):return b
elif(n==6):return b+a
elif(n==7):return b+a+a
elif(n==8):return b+a+a+a
elif(n==9):return a+c
def romannumber(n):
if(n>999):
raise RuntimeError('Not Allowed')
if(n<=99):
return romandigit((n//10)%10,'X','L','C')+romandigit(n%10,'I','V','X')
elif(n<=999):
return romandigit((n//100)%10,'C','D','M')+romandigit((n//10)%10,'X','L','C')+romandigit(n%10,'I','V','X')
def main():
i=int(input("Enter Any Number Upto 999:"))
print(romannumber(i))
main()
Output::
Fibonacci Sequence:
Basic rule:: next term = Sum of previous two terms
Code::
import java .util.*;
public class Fibonacci
{
public static void main(String[] nt)
{
Scanner in=new Scanner(System.in);
System.out.print("Enter Number of Terms:");
int a=in.nextInt();
for(int i=0;i<a;i++)
{
System.out.print(fibonacci(i)+" ");
}
}
public static int fibonacci(int n)
{
if(n<2)
{
return n;
}
else
{
return (fibonacci(n-2)+fibonacci(n-1));
}
}
}
Output::