Prime Number Program in C++

Prime number is a number that is greater than 1 and divided by 1 or itself. In other words, prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17, 19, 23.... are the prime numbers.
Let's see the prime number program in C++. In this C++ program, we will take an input from the user and check whether the number is prime or not.

#include <iostream>   
using  namespace  std;  
int  main()  
{  
   int  n, i, m=0, flag=0;  
  cout <<  "Enter the Number to check Prime: " ;  
  cin >> n;  
  m=n/2;  
   for (i = 2; i <= m; i++)  
  {  
      if (n % i == 0)  
      {  
          cout<< "Number is not Prime." <<endl;  
          flag=1;  
           break ;  
      }  
  }  
   if  (flag==0)  
      cout <<  "Number is Prime." <<endl;  
   return  0;  
} 

Output:
Enter the Number to check Prime: 17  
Number is Prime.   
Enter the Number to check Prime: 57  
Number is not Prime.

Fibonacci Series in C++

Fibonacci Series in C++

Fibonacci Series in C++: In case of fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two numbers of fibonacci series are 0 and 1.
There are two ways to write the fibonacci series program:
  • Fibonacci Series without recursion
  • Fibonacci Series using recursion

Fibonaccci Series in C++ without Recursion

Let's see the fibonacci series program in C++ without recursion.

#include <iostream>
using namespace std;
int main()
{
    int f0 = 0,f1 = 1,f,i, num;
    cout<<"Enter the number of elements: ";
    cin>>num;
    cout<<f0<<" "<<f1<<" "; //printing 0 and 1 
    for(i=2;i<num;i++)//loop starts from 2 because 0 and 1 are already printed
    {
        f=f0+f1;
        cout<<" "<< f;
        f0 = f1;
        f1 = f;
    }
    return 0;
}
Output:



Enter the number of elements: 8
1  1 2 3 5 8 13 21 

Fibonnaci series using recursion in C++

Let's see the fibonacci series program in C++ using recursion.

#include<iostream>    
using namespace std;      
void printFibonacci(int n){    
    static int n1=0, n2=1, n3;    
    if(n>0){    
         n3 = n1 + n2;    
         n1 = n2;    
         n2 = n3;    
 cout<<n3<<" ";    
         printFibonacci(n-1);    
    }    
}    
int main(){    
    int n;    
    cout<<"Enter the number of elements: ";    
    cin>>n;    
    cout<<"Fibonacci Series: ";    
    cout<<"0 "<<"1 ";  
    printFibonacci(n-2);  //n-2 because 2 numbers are already printed    
     return 0;  
}

Output:
Enter the number of elements: 12 
 Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89 


                                                              C++Programs

Java Tutorial



      Our core Java programming tutorial is designed for students and working professionals. Java is an object-oriented, class-based, concurrent, secured and general-purpose computer-programming language. It is a widely used robust technology.

What is Java

  Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. This tutorial gives a complete understanding of Java. This reference will take you through simple and practical approaches while learning Java Programming language.

SIMPLE JAVA PROGRAM 


Let's have a quick look at Java programming example. A detailed description of hello Java example is available in next page.

class Simple{  
    public static void main(String args[]){  
     System.out.println("Hello Java");  
    }  
}

Simple java programs for beginners

    
  We begin with a very simple program that prints a line of text as output.


class SimpleOne{  
    public static void main(String args[]){  
     System.out.println("Hello Java");  
    }  
}



is perhaps the simplest of all Java programs. Nevertheless, it brings out some salient features
f the language. Let us therefore discuss the program line by line and understand the unique features that constitute a Java program.


Class Declaration

 The first line
                 class SampleOne

declares a class, which is an object-oriented construct. As stated earlier Java is a true object-orient
language and therefore, everything must be placed inside a class. class is a keyword and declares that 
new class definition follows. SampleOne is a Java identifier that specifies the name of the class to be defined.

Opening Brace

Every class definition in Java begins with an opening brace "{" and ends with a matching closing brace "}" , appearing in the last line in the example. This is similar to C++ class construct. (Note that a class definition  in C++ ends with a semicolon.)

The Main Line


The third line
                    public static void main (String args[ ])

defines a method named main. Conceptually, this is similar to the main( ) function in C/C++. Every Java application program must include the main() method. This is the starting point for the interpreter to be the execution of the program. A Java application can have any number of classes but only one of them must include a main method to initiate the execution. (Note that Java applets will not use the main method at all.)
This line contains a number of keywords, public, static and void.

Public :
         The keyword public is at access specifier that declares the main method as unprotected and       therefore making it accessible to all other classes This is similar to the C++ public modifier,

Static:
          next appears the keyword static, which declares this method as one that belongs to the entire class and not a port of any objects of the class. The main must always be declared as static since the interpreter uses this method before any objects are created. More about static methods and variables 
will be discussed later.

Void:
       The type modifier void states that the main method does not return any value (but simply pr
ints some  to the screen.)
All parameters to a method are declared inside a pair of parentheses, Here, String args[] declares a
parameter named args, which contains an array of objects of the class type String