WAP TO CALCULATE FACTORIAL OF A NUMBER BY FOR LOOP.


import java.util.*;

class factorial

{   

public static void main(String args[])   

{       

Scanner ob=new Scanner(System.in);       
int f=1,num=0;       
System.out.println("ENTER NUM");       
num=ob.nextInt();       
for(int i=1;i<=num;i++)       
{           
f=f*i;       
}       

System.out.println("Factorial is-:        "+f);   


}


}
Advertisements

Q-HOW TO PRINT WORDS WHICH START WITH VOWEL ​?


import java.util.*;
class firstvowel
{   

public static void main(String args[])   

{       
Scanner ob=new Scanner(System.in);       
String str,t="",v="aeiouAEIOU",r="";       
System.out.println("enter string");       
str=ob.nextLine()+" ";       
for(int i=0;i<str.length();i++)       
{           
char ch=str.charAt(i);           
if(ch!=' ')         
{               
t=t+ch;         
}           
else         
{               
if(v.indexOf(t.charAt(0))!=-1)             
{                 
r=r+t+" ";               
t="";             
}     
}   
}   
System.out.println(r+" ");
}
}

Q- WAP TO CHECK IF A NUMBER IS PRONIC OR NOT?

import java.io.*; 
class PRONIC
{
public static void main(String args[]) throws IOException
{
int n=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Number");
n=Integer.parseInt(br.readLine());
int k=0,p=0;
for(int i=1;i<=n;i++)
{
p=i*(i+1);
if(p==n)
k=1;
}
if(k==1)
System.out.println("PRONIC NUMBER");
else
System.out.println("NOT PRONIC NUMBER");
}
}

Q- Write a program to check whether a number is Armstrong or not?


import java.io.*;
public class arm
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=0;
System.out.println("ENTER ANY NUMBER");
n=Integer.parseInt(br.readLine());
boolean f=arm.armstrong(n);
if(f==true)
System.out.println("the no. is armstrong");
else
System.out.println("not armstrong");
}
static boolean armstrong(int n)
{
int a=0,s=0,num=n;
while(n>0)
{
a=n%10;
s=s+a*a*a;
n=n/10;
}
if(num==s)
{
return true;
}
else
{
return false;
}
}
}

Q- Write a program to calculate the sum of 2 numbers ?


import java.io.*;
class sum
{
int add(int a, int b)
{
int c=a+b;
return c;
}
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a=0,b=0;
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
sum obj=new sum(a,b);
int t=obj.sum(a,b);
System.out.println("The sum is"+t);
}

Q-HOW TO CHECK IF A STRING IS OF EVEN LENGTH OR NOT ​?


import java.util.*;
class evenstring
{
public static void main(String args[])
{
Scanner ob=new Scanner(System.in);
String str="",t="",p="";
int r=0;
System.out.println("enter string");
str=ob.nextLine();
for(int i=0;i<str.length();i++)
{
char ch=str.charAt(i);
if(ch!=' ')
{
t=t+ch;
}
else
{
r=t.length();
if(r%2==0)
{
System.out.println("even is "+t);
}
else
{
System.out.println("odd"+t); } t="";
}
}
}
}