rss
Twitter Delicious Facebook Digg Stumbleupon Favorites

Thursday, October 13, 2011

Check how many vowels and consonants

In phonetics, a vowel is a sound in spoken language, such as English ah! [ɑː] or oh! [oʊ], pronounced with an open vocal tract so that there is no build-up of air pressure at any point above the glottis. This contrasts with consonants, such as English sh! [ʃː], where there is a constriction or closure at some point along the vocal tract. A vowel is also understood to be syllabic: an equivalent open but non-syllabic sound is called a semivowel.

In all languages, vowels form the nucleus or peak of syllables, whereas consonants form the onset and (in languages that have them) coda. However, some languages also allow other sounds to form the nucleus of a syllable, such as the syllabic l in the English word table [ˈteɪ.bl̩] (the stroke under the l indicates that it is syllabic; the dot separates syllables), or the r in Serbo-Croatian vrt [vr̩t] "garden".
There is a conflict between the phonetic definition of "vowel" (a sound produced with no constriction in the vocal tract) and the phonological definition (a sound that forms the peak of a syllable). The approximants [j] and [w] illustrate this conflict: both are produced without much of a constriction in the vocal tract (so phonetically they seem to be vowel-like), but they occur on the edge of syllables, such as at the beginning of the English words "yet" and "wet" (which suggests that phonologically they are consonants). The American linguist Kenneth Pike suggested the terms 'vocoid' for a phonetic vowel and "vowel" for a phonological vowel,so using this terminology, [j] and [w] are classified as vocoids but not vowels.
The word vowel comes from the Latin word vocalis, meaning "speaking", because in most languages words and thus speech are not possible without vowels. In English, the word vowel is commonly used to mean both vowel sounds and the written symbols that represent them.

class Vowel
{
public static void main(String args[])
{
String a="Saumav";
char d;
int b,v=0,x=0;
int c=a.length();
for(b=0;b<=c-1;b++)
{
d=a.charAt(b);
if
(d=='a' || d=='A' || d=='e' || d=='E' || d=='i' || d=='I' || d=='o' || d=='O' || d=='u' || d=='U'){
v++;
}
else
{
x++;
}
}
System.out.println("The no of vowels are"+v);
System.out.println("the no of consonants are"+x);
}
}


Write a program to find out power of any given number.
class Power
{
public static void main(String args[])
{
double b=64;
double x;
x=Math.pow(b,2);
System .out.println("the 2 power is"+x);
}
}

Wednesday, October 12, 2011

Display Triangle as follow


Display:  1
                  2 3
                  4 5 6
                   7 8 9 10 ..
class Traingle{
      public static void main(String args[]){
          int c=0;
          int n = Integer.parseInt(args[0]);
         loop1: for(int i=1;i<=n;i++){
         loop2: for(int j=1;j<=i;j++){
                       if(c!=n){
                            c++;
                            System.out.print(c+" ");
                       }
                       else
                           break loop1;
                    }
                    System.out.print("\n");
                 }
     }
}

Tuesday, October 11, 2011

Write a program to find average of consecutive N Odd number and Even number.


In mathematics, the parity of an object states whether it is even or odd.
This concept begins with integers. An even number is an integer that is "evenly divisible" by 2, i.e., divisible by 2 without remainder; an odd number is an integer that is not evenly divisible by 2. (The old-fashioned term "evenly divisible" is now almost always shortened to "divisible".) A formal definition of an even number is that it is an integer of the form n = 2k, where k is an integer; it can then be showed that an odd number is an integer of the form n = 2k + 1. An even number has the form n = 2k where k is an integer.
Examples of even numbers are −4, 8, and 1728. Examples of odd numbers are −5, 9, 3, and 71. This classification only applies to integers, i.e., a fractional number like 1/2 or 4.201 is neither even nor odd.
The sets of even and odd numbers can be defined as following:
  • Even = \{ 2k; \forall k \in \mathbb{Z} \}
  • Odd = \{ 2k+1; \forall k \in \mathbb{Z} \}
A number (i.e., integer) expressed in the decimal numeral system is even or odd according to whether its last digit is even or odd. That is, if the last digit is 1, 3, 5, 7, or 9, then it's odd; otherwise it's even. The same idea will work using any even base. In particular, a number expressed in the binary numeral system is odd if its last digit is 1 and even if its last digit is 0. In an odd base, the number is even according to the sum of its digits – it is even if and only if the sum of its digits is even.

class EvenOdd{
      public static void main(String args[]){
      int n = Integer.parseInt(args[0]);
      int cntEven=0,cntOdd=0,sumEven=0,sumOdd=0;
      while(n > 0){
           if(n%2==0){
               cntEven++;
               sumEven = sumEven + n;
           }
           else{
               cntOdd++;
               sumOdd = sumOdd + n;
           }
           n--;
      }
      int evenAvg,oddAvg;
      evenAvg = sumEven/cntEven;
      oddAvg = sumOdd/cntOdd;
      System.out.println("Average of first N Even no is "+evenAvg);
      System.out.println("Average of first N Odd no is "+oddAvg);

  }
}

Sunday, October 9, 2011

A program to generate Harmonic Series


In mathematics, the harmonic series is the divergent infinite series:
\sum_{n=1}^\infty\,\frac{1}{n} \;\;=\;\; 1 \,+\, \frac{1}{2} \,+\, \frac{1}{3} \,+\, \frac{1}{4} \,+\, \frac{1}{5} \,+\, \cdots.\!
Its name derives from the concept of overtones, or harmonics in music: the wavelengths of the overtones of a vibrating string are 1/2, 1/3, 1/4, etc., of the string's fundamental wavelength. Every term of the series after the first is the harmonic mean of the neighboring terms; the term harmonic mean likewise derives from music.
The fact that the harmonic series diverges was first proven in the 14th century by Nicole Oresme, but this achievement fell into obscurity. Proofs were given in the 17th century by Pietro Mengoli,Johann Bernoulli, and Jakob Bernoulli.
Historically, harmonic sequences have had a certain popularity with architects. This was so particularly in the Baroque period, when architects used them to establish the proportions of floor plans, ofelevations, and to establish harmonic relationships between both interior and exterior architectural details of churches and palaces.
The harmonic series is counterintuitive to students first encountering it, because it is a divergent series though the limit of the nth term as n goes to infinity is zero. The divergence of the harmonic series is also the source of some apparent paradoxes. One example of these is the "worm on the rubber band". Suppose that a worm crawls along a 1 meter rubber band and, after each minute, the rubber band is uniformly stretched by an additional 1 meter. If the worm travels 1 centimeter per minute, will the worm ever reach the end of the rubber band? The answer, counterintuitively, is "yes", for after n minutes, the ratio of the distance travelled by the worm to the total length of the rubber band is
\frac{1}{100}\sum_{k=1}^n\frac{1}{k}.
Because the series gets arbitrarily large as n becomes larger, eventually this ratio must exceed 1, which implies that the worm reaches the end of the rubber band. The value of n at which this occurs must be extremely large, however, approximately e100, a number exceeding 1040. Although the harmonic series does diverge, it does so very slowly.
Another example is: given a collection of identical dominoes, it is clearly possible to stack them at the edge of a table so that they hang over the edge of the table. The counterintuitive result is that one can stack them in such a way as to make the overhang arbitrarily large, provided there are enough dominoes.

class Harmonic{
      public static void main(String args[]){
      int num = Integer.parseInt(args[0]);
      double result = 0.0;
      while(num > 0){
            result = result + (double) 1 / num;
            num--;
      }
      System.out.println("Output of Harmonic Series is "+result);
  }
}

Switch case

In computer programming, a switch, case, select or inspect statement is a type of selection control mechanism that exists in most imperative programming languages such as Pascal, Ada, C/C++, C#, Java, and so on. It is also included in several other types of languages. Its purpose is to allow the value of a variable or expression to control the flow of program execution via a multiway branch . The main reasons for using a switch include improving clarity, by reducing otherwise repetitive coding, and (if the heuristics permit) also offering the potential for faster execution through easier compiler optimization in many cases.

In his 1952 text Introduction to Metamathematics, Stephen Kleene formally proves that the CASE function (the IF-THEN-ELSE function being its simplest form) is a primitive recursive function, where he defines the notion definition by cases in the following manner:
"#F. The function φ defined thus
φ(x1 , ... , xn ) =
  • φ1(x1 , ... , xn ) if Q1(x1 , ... , xn ),
  • . . . . . . . . . . . .
  • φm(x1 , ... , xn ) if Qm(x1 , ... , xn ),
  • φm+1(x1 , ... , xn ) otherwise,
where Q1 , ... , Qm are mutually exclusive predicates (or φ(x1 , ... , xn) shall have the value given by the first clause which applies) is primitive recursive in φ1, ..., φm+1, Q1, ..., Qm+1. (Definition by cases)." (Kleene 1952:229)
Kleene provides a proof of this in terms of the Boolean-like recursive functions "sign-of" sg( ) and "not sign of" ~sg( ) (Kleene 1952:222-223); the first returns 1 if its input is positive and −1 if its input is negative.
Boolos-Burgess-Jeffrey make the additional observation that "definition by cases" must be both mutually exclusive and collectively exhaustive. They too offer a proof of the primitive recursiveness of this function (Boolos-Burgess-Jeffrey 2002:74-75).
The IF-THEN-ELSE is the basis of the McCarthy formalism – its usage replaces both primitive recursion and the mu-operator.

class SwitchCase{
      public static void main(String args[]){
          try{
          int num = Integer.parseInt(args[0]);
          int n = num;
          int reverse=0,remainder;
          while(num > 0){
                remainder = num % 10;
                reverse = reverse * 10 + remainder;
                num = num / 10;
           }
          String result="";
          while(reverse > 0){
               remainder = reverse % 10;
               reverse = reverse / 10;
               switch(remainder){
                    case 0 :
                             result = result + "Zero ";
                             break;
                    case 1 :
                             result = result + "One ";
                             break;
                    case 2 :
                             result = result + "Two ";
                             break;
                    case 3 :
                             result = result + "Three ";
                             break;
                    case 4 :
                             result = result + "Four ";
                             break;
                    case 5 :
                             result = result + "Five ";
                             break;
                    case 6 :
                             result = result + "Six ";
                             break;
                    case 7 :
                             result = result + "Seven ";
                             break;
                    case 8 :
                             result = result + "Eight ";
                             break;
                    case 9 :
                             result = result + "Nine ";
                             break;
                    default:
                             result="";
                 }
            }
                System.out.println(result);
        }catch(Exception e){
             System.out.println("Invalid Number Format");
         }
     }
}

Saturday, October 8, 2011

A program to find whether number is palindrome or not.

A palindromic number or numeral palindrome is a 'symmetrical' number like 16461, that remains the same when its digits are reversed. The term palindromic is derived from palindrome, which refers to a word like rotor that remains unchanged under reversal of its letters. The first palindromic numbers (in decimal) are:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, ….
Palindromic numbers receive most attention in the realm of recreational mathematics. A typical problem asks for numbers that possess a certain property and are palindromic. For instance,
  • the palindromic primes are 2, 3, 5, 7, 11, 101, 131, 151, …
  • the palindromic square numbers are 0, 1, 4, 9, 121, 484, 676, 10201, 12321, …
Buckminster Fuller referred to palindromic numbers as Scheherazade numbers in his book Synergetics, because Scheherazade was the name of the story-telling wife in the 1001 Nights.
It is fairly straightforward to appreciate that in any base there are infinitely many palindromic numbers, since in any base the infinite sequence of numbers written (in that base) as 101, 1001, 10001, etc. (in which the nth number is a 1, followed by n zeros, followed by a 1) consists of palindromic numbers only.
Although palindromic numbers are most often considered in the decimal system, the concept of palindromicity can be applied to the natural numbers in any numeral system. Consider a number n > 0 in base b ≥ 2, where it is written in standard notation with k+1 digits ai as:
n=\sum_{i=0}^ka_ib^i
with, as usual, 0 ≤ ai < b for all i and ak ≠ 0. Then n is palindromic if and only if ai = aki for all i. Zero is written 0 in any base and is also palindromic by definition.
class PalindromeNumbers{
      public static void main(String args[]){
          int num = Integer.parseInt(args[0]);
          int n = num;
          int reverse=0,remainder;
          while(num > 0){
                remainder = num % 10;
                reverse = reverse * 10 + remainder;
                num = num / 10;
           }
          if(reverse == n)
              System.out.println(n+" is a Palindrome Number");
          else
              System.out.println(n+" is not a Palindrome Number");
     }
}

A program to Find whether number is Prime or Not.

A prime number (or a prime) is a natural number that has exactly two distinct natural number divisors: 1 and itself. For example, 5 is prime, since no number except 1 and 5 divides it. On the other hand, 6 is not a prime (it is composite), since 6 = 2 × 3. The fundamental theorem of arithmetic establishes the central role of primes in number theory: any positive integer n can be expressed as the product of powers of primes in a way that is unique except for a possible reordering of the factors. This theorem requires excluding 1 as a prime. There are infinitely many primes, as demonstrated by Euclid around 300 BC.
The property of being prime is called primality. A simple but slow method of verifying the primality of a given number n is known as trial division. It consists of testing whether n is a multiple of any integer between 2 and n. Algorithms that are much more efficient than trial division have been devised to test the primality of large numbers. Particularly fast methods are available for primes of special forms, such as Mersenne primes. As of 2011, the largest known prime number has about 13 million decimal digits.
There is no known useful formula that yields all of the prime numbers and no composites. However, the distribution of primes, that is to say, the statistical behaviour of primes in the large, can be modeled. The first result in that direction is the prime number theorem which says that the probability that a given, randomly chosen number n is prime is inversely proportional to its number of digits, or the logarithm of n. Therefore, the density of prime numbers within natural numbers is 0, but in a sense, primes occur more often than squares of integers.

The prime number theorem was proven at the end of the 19th century using methods of analytic number theory. The unproven Riemann hypothesis dating from 1859 implies a refined statement concerning the distribution of primes. Many questions around prime numbers remain open, many of which can be stated simply. For example, Goldbach's conjecture, which asserts that every even integer greater than 2 can be expressed as the sum of two primes, and the twin prime conjecture, which says that there are infinitely many twin primes (pairs of primes whose difference is 2), have been unresolved for more than a century. Such questions spurred the development of various branches of number theory, focusing on analytic or algebraic aspects of numbers.
Primes are applied in several routines in information technology, such as public-key cryptography, which makes use of properties such as the difficulty of factoring large numbers into their prime factors. Prime numbers give rise to various generalizations in other mathematical domains, mainly algebra, such as prime elements and prime ideals.

class PrimeNumbers{
      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 Number");
                 flag = 1;
                 break;
              }
         }
         if(flag==0)
             System.out.println(num+" is a Prime Number");
    }
}

A program to find whether given number is Armstrong or not.

In recreational number theory, a narcissistic number (also known as a pluperfect digital invariant (PPDI), an Armstrong number (after Michael F. Armstrong) or a plus perfect number) is a number that is the sum of its own digits each raised to the power of the number of digits. This definition depends on the base b of the number system used, e.g. b = 10 for the decimal system or b = 2 for the binary system.
The definition of a narcissistic number relies on the decimal representation n = dkdk-1...d1d0 of a natural number n, e.g.
n = dk·10k-1 + dk-1·10k-2 + ... + d2·10 + d1,
with k digits di satisfying 0 ≤ di ≤ 9. Such a number n is called narcissistic if it satisfies the condition
n = dkk + dk-1k + ... + d2k + d1k.
For example the 3-digit decimal number 153 is a narcissistic number because 153 = 13 + 53 + 33.
Narcissistic numbers can also be defined with respect to numeral systems with a base b other than b = 10. The base-b representation of a natural number n is defined by
n = dkbk-1 + dk-1bk-2 + ... + d2b + d1,
where the base-b digits di satisfy the condition 0 ≤ di ≤ b-1. For example the (decimal) number 17 is a narcissistic number with respect to the numeral system with base b = 3. Its three base-3 digits are 122, because 17 = 1·32 + 2·3 + 2 , and it satisfies the equation 17 = 13 + 23 + 23.
If the constraint that the power must equal the number of digits is dropped, so that for some m possibly different from k it happens that
n = dkm + dk-1m + ... + d2m + d1m,
then n is called a perfect digital invariant or PDI. For example, the decimal number 4150 has four decimal digits and is the sum of the fifth powers of its decimal digits
4150 = 45 + 15 + 55 + 05,
so it is a perfect digital invariant but not a narcissistic number.
In "A Mathematician's Apology", G. H. Hardy wrote:
There are just four numbers, after unity, which are the sums of the cubes of their digits:
153 = 13 + 53 + 33
370 = 33 + 73 + 03
371 = 33 + 73 + 13
407 = 43 + 03 + 73.
These are odd facts, very suitable for puzzle columns and likely to amuse amateurs, but there is nothing in them which appeals to the mathematician.

The sequence of "base 10" narcissistic numbers starts: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474 ...
The sequence of "base 3" narcissistic numbers starts: 0, 1, 2, 12, 122
The sequence of "base 4" narcissistic numbers starts: 0, 1, 2, 3, 313
The number of narcissistic numbers in a given base is finite, since the maximum possible sum of the kth powers of a k digit number in base b is
k(b-1)^k\, ,
and if k is large enough then
k(b-1)^k<b^{k-1}\, ,
in which case no base b narcissistic number can have k or more digits.
There are 88 narcissistic numbers in base 10, of which the largest is
115,132,219,018,763,992,565,095,597,973,971,522,401
with 39 digits.
Unlike narcissistic numbers, no upper bound can be determined for the size of PDIs in a given base, and it is not currently known whether or not the number of PDIs for an arbitrary base is finite or infinite.
class ArmstrongNumbers{
      public static void main(String args[]){
      int num = Integer.parseInt(args[0]);
      int n = num;
      int check=0,remainder;
      while(num > 0){
           remainder = num % 10;
           check = check + (int)Math.pow(remainder,3);
           num = num / 10;
      }
      if(check == n)
            System.out.println(n+" is an Armstrong Number");
      else
            System.out.println(n+" is not a Armstrong Number");
   }
}

Powered by Blogger.