MY mENU


Showing posts with label Programs. Show all posts
Showing posts with label Programs. Show all posts

Friday, 19 April 2013

programm to reading values from keyboard into an arraylist untill 'z' keyword enterd, stops reading and than sorting them in ascending order.

import java.io.*;
import java.util.*;

public class ArraysOne {

public static void main(java.lang.String[] args) 
{
//int i;
ArrayList al=new ArrayList();
try{
Scanner input = new Scanner(System.in);

int check=0;
while(true){
check = input.nextInt();
if(check == 'z' || check=='Z') break;
al.add(check);
}

} catch(Exception e){
e.printStackTrace();}

Iterator itr=al.iterator();
while(itr.hasNext()){
Object i=itr.next();
System.out.println(i);
}
int j=al.size();
System.out.println("Size"+j);

Collections.sort(al);
System.out.println("ArrayList is sorted");

for(Integer temp: al){
System.out.println(temp);

}
/*for(int i=j;i>=i-1;i--){
for(int k=i-1;k>=;k--){
if(al.get(i)al.get(k)){
System.out.println(al.get(k));}
else{ System.out.println(al.get(i));}
}
}*/

}

}

Adding two Arrays and finding the third array is positive or negative???

import java.util.*;
import java.lang.*;
import java.io.*;
class AddArrays1
{
public static void main(String[] args)
{
//Scanner sc=new Scanner(System.in);

int a[]={1,2,3};
int b[]={2,3,-4};
int c[]=new int[3];
for(int i=0;i{
c[i]=a[i]+b[i];
System.out.println(c[i]);
}
for (int i=0; i{
if (c[i] > 0) {
System.out.println(c[i]+ " is positive");
} else {
System.out.println(c[i] + " is not positive");
}
}
}
}

Friday, 15 March 2013

One of Logical Interview Question I faced:

A good Employee is defined who has all the following properties:
1. Employee must be married.
2. Employee has at least 2 children 
3. His Middle Name Start with “K” and Ends With “E”.
4. His Last Name has at least 4 characters, and starts with “A”
5. In His childrens have at least one name “Raja”.
Write a method:
boolean isGoodEmployee( boolean isMarried, int noOfChild , String middleName , String lastName , String[] childNames){

Answer:
import java.io.*;
class Employ
{
boolean isMarried;
int noOfChild;
String middleName;
String lastName;
String[] childNames;

boolean isGoodEmployee( boolean isMarried, int noOfChild , String middleName , String lastName , String[] childNames){
this.isMarried=isMarried;
this.noOfChild=noOfChild;
this.middleName=middleName;
this.lastName=lastName;
this.childNames=childNames;

int mlength=middleName.length();
int ml=mlength-1;
int lnLength=lastName.length();
int count=0,name=0;

int childln=childNames.length;

if(isMarried==true)
{
//return true;
}else {
System.out.println("He is not Married");
}

if (noOfChild<=2){

//return true;
}else {
System.out.println("not about children");
}

//System.out.println(ml);
//System.out.println(middleName.charAt(ml));
//System.out.println(middleName.charAt(0));

if(middleName.charAt(0)=='k') {
if(middleName.charAt(ml)!='e'){
// return true;
}else{
System.out.println("Name not in COrrect formate");
}
}

if(lnLength>4){
for(int i=0;iif(lastName.charAt(i)=='a')
count=count+1;
}
//System.out.println(count);
if(count>=2){
}else{
System.out.println("Last Name not in Correct form");
}

}

//System.out.println(childNames[0]);
//System.out.println(childln);

for(int i=0;i{

if(childNames[i].equals("Raja")){
name=name+1;
}
if(name>=1){

//return true;
}else{
System.out.println("NO One Children is RAJA");
}
}

System.out.println(" Good Employee");
return true;

}

public static void main(String[] args)
{
String mName="kiranew";
String lName="Maadhu";
boolean married=true;
String[] childN={"Raja","Latha"};
int noChild=2;
Employ em=new Employ();
em.isGoodEmployee(married,noChild,mName,lName,childN);

}
}

Wednesday, 23 January 2013

A MobileNumber is a VIP number if it satisfy the following conditions.


  1. The operator should be Vodafone.
  2. Atleast one 0 (Zero) should be exist in mobile number.
  3. The number should not end with 8.
  4. The single digit sum of all the digits in the number should be equal to 9. For example if the number is 9876543210, the sum is 9+8+7+...+1+0 = 45. Sum of 4+5 = 9.
Write a method: 
private boolean isVIPMobileNumber(String mobileNum, String operator)
mobileNum     phone number
operator

  
mobile operator as bsnl, Vodafone

import java.io.*;
import java.lang.*;
class Phno
{
String mobileNum;
String operator;
int count=0;
long sum=0;
long total_value=0;
 long value;
 long rem;
 long rem1;

private boolean isVIPMobileNumber(String mobileNum, String operator)
{
   this.mobileNum=mobileNum;
   this.operator=operator; 
  

   int len=mobileNum.length();
if(operator!="vadafone")
{
System.out.println("network is not vadafone");
    }

//System.out.println(len);
for(int i=0;i
{
   if(mobileNum.charAt(i)=='0')
{
count=count+1;
}
}
if(count>0)
{
}
else
{
System.out.println("at least one zero not avaliablee");
}

if(mobileNum.charAt(len-1)=='8'){
System.out.println("mobile number should not end with 8");
  }
value=Long.parseLong(mobileNum);

while(value!=0)
{
rem=value%10;
value=value/10;
sum=sum+rem;
}
//System.out.println(sum);
while(sum!=0)
{
    rem1=sum%10;
sum=sum/10;
total_value=total_value+rem1;
        }
if(total_value!=9)
{
System.out.println("sum of individual no's not equal to 9");
}
//System.out.println(total_value);
  
 System.out.println("VIP number");
 
return true;


}

public static void main(String[] args) 
{
String mno="9885603834";
String network="vadafone";
Phno pn=new Phno();
pn.isVIPMobileNumber(mno,network);

}
}

Thursday, 17 January 2013

Prime Number Programme in Java


Program for Check the Given Num Prime Or NOT::

class PrimeNo
{
public static void main(String[] args) 
{
       int num=Integer.parseInt(args[0]);
  int flag=0;
  "  for(int i=2;i<=num;i++)>

{
            if(num%i==0)
{
System.out.println(num+" is not a prime num");
flag=1;
break;
}
}
if(flag==0)
{
System.out.println(num+" is a prime num");
        }
}
}


Program for Find the Prime Numbers up to Given Number:


import java.util.*;
class PrimeNum2
{
public static void main(String[] args) 
{
int i,j,s=0,d=0;
int n;
Scanner sn=new Scanner(System.in);
System.out.println("Enter the Number YOu Want::");
         n=sn.nextInt();

"for(i=1;i<=n;i++)">

{
s=0;
"for(j=1;j<=i;j++)"
{
if(i%j==0)
s=s+1;
}
if(s==2)
{
System.out.println(i);
d=d+1;
}
}

System.out.println("The Number Of Prime Numbers Are==:"+d);
}
}



Program for Find the AlterNative Prime Numbers up to Given Number:

import java.util.*;
class PrimeNum2
{
public static void main(String[] args) 
{
int i,j,s=0,d=0;
int n;
Scanner sn=new Scanner(System.in);
System.out.println("Enter the Number YOu Want::");
         n=sn.nextInt();
System.out.println("AlterNative Prime NUmber:");

"for(i=1;i<=n;i++)">

{
s=0;
"for(j=1;j<=i;j++)"
{
if(i%j==0)
s=s+1;
}
if(s==2)
{

d=d+1;
if(d%2==0)
System.out.println(i);
}
}

System.out.println("The Number Of Prime Numbers Are==:"+d);
}
}




Sunday, 20 May 2012

program to generate and print armstrong numbers

program to generate and print armstrong numbers
armstrong number in c: This program prints armstrong number. In our program we ask the user to enter a number and then we use a loop from one to the entered number and check if it is an armstrong number and if it is then the number is printed on the screen. Remember a number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are 0, 1, 153, 370, 407.

C code
#include 
#include 
 main() {
 int r;
 long number = 0, c, sum = 0, temp;
 printf("Enter the maximum range upto which you want to find armstrong numbers ");
 scanf("%ld",&number);
 printf("Following armstrong numbers are found from 1 to %ld\n",number);
 for( c = 1 ; c <= number ; c++ ) { 
 temp = c;
 while( temp != 0 ) {
 r = temp%10;
 sum = sum + r*r*r;
 temp = temp/10; 
 }
 if ( c == sum )
 printf("%ld\n", c);
 sum = 0;
 }
 getch();
 return 0; 
}
Output of program

armstrong number

armstrong number c program
  c programming code to check whether a number is armstrong or not. A number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are: 0, 1, 153, 370, 407.

C programming code
#include  
 main() {
 int number, sum = 0, temp, remainder;
 printf("Enter a number\n");
 scanf("%d",&number);
 temp = number;
 while( temp != 0 ) { 
 remainder = temp%10;
 sum = sum + remainder*remainder*remainder; 
 temp = temp/10;
 }
 if ( number == sum )
 printf("Entered number is an armstrong number."); 
    else 
     printf("Entered number is not an armstrong number."); 
 return 0; 
}

Output of program:

Prime number program

Prime number program in c language

#include 
   main() {
  int n, i = 3, count, c;
  printf("Enter the number of prime numbers required\n");
 scanf("%d",&n);  
if ( n >= 1 ) {
  printf("First %d prime numbers are :\n",n);
 printf("2\n");
 }
 for ( count = 2 ; count <= n ; ) {
 for ( c = 2 ; c <= i - 1 ; c++ ) {
 if ( i%c == 0 )
 break;
 }
 if ( c == i ) { 
 printf("%d\n",i);
 count++; 
 }
 i++;
 }
 return 0; 
}

There are many logic to check prime numbers, one given below is more efficient then above method.
for ( c = 2 ; c <= (int)sqrt(n) ; c++ )
//only checking from 2 to square root of number is sufficient.


C program for prime number or not
#include
 main() { 
 int n, c = 2; 
 printf("Enter a number to check if it is prime\n"); 
 scanf("%d",&n);
 for ( c = 2 ; c <= n - 1 ; c++ ) {
 if ( n%c == 0 ) {
 printf("%d is not prime.\n", n);
 break;
 }
 }
 if ( c == n ) 
 printf("%d is prime.\n", n); 
 return 0; 
}
C program for prime number using function
#include
 int check_prime(int);
 main() {
 int n, result;
 printf("Enter an integer to check whether it is prime or not.\n");
 scanf("%d",&n); 
 result = check_prime(n);
 if ( result == 1 )
 printf("%d is prime.\n", n);
 else
 printf("%d is not prime.\n", n);
 return 0;
 }
 int check_prime(int a) {
 int c;
 for ( c = 2 ; c <= a - 1 ; c++ ) {
 if ( a%c == 0 )
 return 0;
 }
 if ( c == a )
 return 1;
 }

to print diamond pattern

c program to print diamond pattern

Diamond pattern in c: This code print diamond pattern of stars. Diamond shape is as follows:

     * 
   *** 
 ***** 
   ***
     * 

#include  
 int main() {
 int n, c, k, space = 1;
 printf("Enter number of rows\n");
 scanf("%d", &n);
 space = n - 1; 
 for (k = 1; k <= n; k++) { 
   for (c = 1; c <= space; c++) 
      printf(" "); 
       space--; 
 for (c = 1; c <= 2*k-1; c++)
   printf("*");
    printf("\n"); 
     }
 space = 1; 
      for (k = 1; k <= n - 1; k++) { 
       for (c = 1; c <= space; c++)
         printf(" ");
           space++;
               for (c = 1 ; c <= 2*(n-k)-1; c++)
                printf("*");
                 printf("\n");
                } 
                 return 0;
  }

To print Pattern

#include 
 main() {
 int row, c, n, temp;
 printf("Enter the number of rows in pyramid of stars you wish to see ");
 scanf("%d",&n); 
 temp = n;
 for ( row = 1 ; row <= n ; row++ ) {
 for ( c = 1 ; c < temp ; c++ ) 
     printf(" ");
           temp--;
 for ( c = 1 ; c <= 2*row - 1 ; c++ )
 printf("*");
 printf("\n"); 
 } 
 return 0;
 }

Output:



Consider the pattern
*
**
***
****
*****

to print above pattern see the code below:

#include
 main() { 
 int n, c, k;
 printf("Enter number of rows\n");
 scanf("%d",&n); 
 for ( c = 1 ; c <= n ; c++ ) { 
 for( k = 1 ; k <= c ; k++ ) 
 printf("*");
 printf("\n");
 }
 return 0;
 }

Palindrome number

#include

 main() {
 int n, reverse = 0, temp; 
 printf("Enter a number to check if it is a palindrome or not\n");
 scanf("%d",&n);
 temp = n; 
 while( temp != 0 ) {
 reverse = reverse * 10;
 reverse = reverse + temp%10; 
 temp = temp/10;
 }
 if ( n == reverse ) 
 printf("%d is a palindrome number.\n", n); 
    else
       printf("%d is not a palindrome number.\n", n); 
 return 0;
 }

Reverse a Number

#include
 main() {
 int n, reverse = 0;
 printf("Enter a number to reverse\n");
 scanf("%d",&n);
 while (n != 0) {
 reverse = reverse * 10;
 reverse = reverse + n%10;
 n = n/10;
 }
 printf("Reverse of entered number is = %d\n", reverse);
 return 0;
 }

Output of program:

Swapping of two number

Swapping of two numbers in c

#include

 int main() {
 int x, y, temp;
 printf("Enter the value of x and y\n");
 scanf("%d%d", &x, &y);
 printf("Before Swapping\nx = %d\ny = %d\n",x,y);
 temp = x; x = y; y = temp;
 printf("After Swapping\nx = %d\ny = %d\n",x,y);
 return 0;
 }

Swapping of two numbers without third variable: You can also swap two numbers without using temp or temporary or third variable. In that case c program will be as shown :-

#include  
 int main() { 
 int a, b; 
 printf("Enter two integers to swap\n");
 scanf("%d%d", &a, &b);
 a = a + b; 
 b = a - b;
 a = a - b;
 printf("a = %d\nb = %d\n",a,b); 
 return 0; 
}

Swap two numbers using pointers

#include  
 int main() { 
 int x, y, *a, *b, temp;
 printf("Enter the value of x and y\n"); 
 scanf("%d%d", &x, &y); 
 printf("Before Swapping\nx = %d\ny = %d\n", x, y);
 a = &x; 
 b = &y;
 temp = *b; 
 *b = *a;
 *a = temp; 
 printf("After Swapping\nx = %d\ny = %d\n", x, y);
 return 0;
 }

Swapping numbers using call by reference
#include  
 void swap(int*, int*);
 int main() { 
 int x, y;
 printf("Enter the value of x and y\n"); 
 scanf("%d%d",&x,&y);
 printf("Before Swapping\nx = %d\ny = %d\n", x, y);
 swap(&x, &y); 
 printf("After Swapping\nx = %d\ny = %d\n", x, y);
 return 0; 
} 
 void swap(int *a, int *b) { 
 int temp; 
 temp = *b; 
 *b = *a;
 *a = temp; 
 }

C programming code to swap using bitwise XOR
#include  
 int main() { 
 int x, y;
 scanf("%d%d", &x, &y);
 printf("x = %d\ny = %d\n", x, y);
 x = x ^ y; 
 y = x ^ y;
 x = x ^ y;
 printf("x = %d\ny = %d\n", x, y);
 return 0; 
}

Output of code:

to Add n Numbers

#include  
 int main() { 
 int n, sum = 0, c, value;
 printf("Enter the number of integers you want to add\n");
 scanf("%d", &n); 

 printf("Enter %d integers\n",n);
     for (c = 1; c <= n; c++)
     { 
       scanf("%d",&value); 
         sum = sum + value; 
      }
 printf("Sum of entered integers = %d\n",sum); 
 return 0;
 }

Output of program:
C programming code using array
#include  
 int main() { 
 int n, sum = 0, c, array[100];
 scanf("%d", &n); 

 for (c = 0; c < n; c++) {
 scanf("%d", &array[c]);
 sum = sum + array[c];
 }
 printf("Sum = %d\n",sum); 
 return 0;
 }

add digits of number

#include
 main() {
 int n, sum = 0, remainder;
 printf("Enter an integer\n");
 scanf("%d",&n); 
 while(n != 0) {
 remainder = n % 10;
 sum = sum + remainder;
 n = n / 10;
 }
 printf("Sum of digits of entered number = %d\n",sum); 
 return 0; 
}
Add digits using recursion
#include  
 int add_digits(int);
 int main() { 
 int n, result;
 scanf("%d", &n); 
 result = add_digits(n);
 printf("%d\n", result);
 return 0; 
}
 int add_digits(int n) { 
 static int sum = 0;
 if (n == 0) 
{
 return 0;
 } 
 sum = n%10 + add_digits(n/10);
 return sum;
 }

Factorial

Factorial program in c using for loop


#include
#include
main()
{
int c, n, fact = 1;
printf("Enter a number to calculate it's factorial\n");
scanf("%d",&n);

for( c = 1 ; c <= n ; c++ )
fact = fact*c;

printf("Factorial of %d = %d\n",n,fact);
getch();
return 0;
}


Factorial program in c using function

#include
long factorial(int);
main()
{
int number;
long fact = 1;

printf("Enter a number to calculate it's factorial\n");
scanf("%d",&number);
printf("%d! = %ld\n", number, factorial(number));
return 0;
}

long factorial(int n)
{
int c;
long result = 1;
for( c = 1 ; c <= n ; c++ )

result = result*c;

return ( result );

}

Factorial program in c using recursion

#include

long factorial(int);
main()
{
int num;
long f;
printf("ENTER A NUMBER TO FIND FACTORIAL :");
scanf("%d",&num);

if(num<0)
printf("NEGATIVE NUMBERS ARE NOT ALLOWED");
else
{
f = factorial(num);
printf("%d!=%ld",num,f);
}
return(0);
}

long factorial(int n)

{
if(n==0)
return(1);
else
return(n*factorial(n-1));
}

Vowel or Not


#include

main()
{

char ch;
printf("Enter a character\n");
scanf("%c", &ch);

if (ch == 'a' || ch == 'A'  || ch == 'e'  || ch == 'E'  || ch == 'i' || ch == 'I' || ch =='o' || ch=='O'  || ch == 'u'  ||  ch == 'U')

printf("%c is a vowel.\n", ch);

else

printf("%c is not a vowel.\n", ch);
return 0;
}

Check vowel using switch:

#include
main()
{
char ch;
printf("Enter a character\n");
scanf("%c", &ch);

switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':

printf("%c is a vowel.\n", ch);
break;

default: printf("%c is not a vowel.\n", ch);

}
return 0;
}

Function to check vowel

int check_vowel(char a)
{

 if (a >= 'A' && a <= 'Z')
a = a + 'a' - 'A'; /* Converting to lower case or use a = a + 32 */

if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')

return 1;
return 0;
}

program to check whether input alphabet is a vowel or not

check out odd or even


C program to check odd or even using modulus operator:

#include

main()
{

int n;
printf("Enter an integer\n");
scanf("%d",&n);

if ( n%2 == 0 )

printf("Even\n");
else
printf("Odd\n");

return 0;
}

C program to check odd or even using bitwise operator

#include

main()

{

int n;

printf("Enter an integer\n");

scanf("%d",&n);

 
if ( n & 1 == 1 )

 
printf("Odd\n");
else
 printf("Even\n");
return 0;
}


C program to check odd or even without using bitwise or modulus operator


#include

main()

{

int n;



printf("Enter an integer\n");

scanf("%d",&n);

if ( (n/2)*2 == n )

 
printf("Even\n");

else

printf("Odd\n");

 
return 0;

}


Find odd or even using conditional operator


#include

main()

{

int n;


printf("Enter an integer\n");

scanf("%d",&n);



n%2 == 0 ? printf("Even number\n") : printf("Odd number\n");
return 0;
}