Sunday, 29 September 2013

Prime Number

A prime number is a natural number greater than 1 that has no positive divisor other than 1 and itself.

A Composite number is a positive integer that has at least one positive integer other than one or itself. 



Check number entered is  Prime or Composite.


PROGRAM                                              OUTPUT
#include <iostream>
using namespace std;

int main (){
  int num,n,prime;
  cout<<"Enter a number";
  cin>>num;

  if(num <=1 ){
    cout<<"Not Prime or Composite";
  }
else {
    prime = 1;
    for(n=2;n<=num-1;n++){
      if(num%n == 0){
       prime = 0;
       }
    }
    if(prime ){
    cout<<"Prime Number "<<endl;
    }
     else{
       cout<<"Composite Number";
    } 
   }
return 0;
}
Case 1:

Enter a number: 23

Prime Number.

Case 2:

Enter a number: 33

Composite Number

Prime Number

A prime number is a natural number greater than 1 that has no positive divisor other than 1 and itself.


Check number entered is  prime number or not.


PROGRAM                                              OUTPUT
#include <iostream>
using namespace std;

int main (){
  int num,n,prime;
  cout<<"Enter a number";
  cin>>num;

  if(num <=1 ){
    cout<<"NOT a Prime Number";
  }
else {
    prime = 1;
    for(n=2;n<=num-1;n++){
      if(num%n == 0){
       prime = 0;
       }
    }
    if(prime ){
    cout<<"Prime Number "<<endl;
    }
     else{
       cout<<"NOT a Prime Number";
    } 
   }
return 0;
}
Case 1:

Enter a number: 23

Prime Number.

Case 2:

Enter a number: 33

NOT a Prime Number

Prime Number

A prime number is a natural number greater than 1 that has no positive divisor other than 1 and itself.


Print prime number upto a user given number. 


PROGRAM                                              OUTPUT
#include <iostream>
using namespace std;

int main (){
  int num,i,n;
  bool prime; 
  cout<<"Prime no. upto:";
  cin>>num;
  for(i=3;i<=num;i++){
    prime = true;
    for(n=2;n<=i-1;n++){
      if(i%n == 0){
       prime = false;
       }
    }
    if(prime){
    cout<<i<<" is prime "<<endl;
    } 
   }
return 0;
}
Prime no. upto: 25
3
5
7
11
13
17
19
23

Saturday, 28 September 2013

FIZZBUZZ 

 A program that prints (to STDOUT) the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.



PROGRAM                                              OUTPUT
#include <iostream>
using namespace std;
int main()
{
    for(int i=1;i<=100;i++){

        if(i%15 == 0)

            cout<<"FizzBuzz\n";

        else if(i%3 == 0)

            cout<<"Fizz\n";

        else if(i%5 == 0)

            cout<<"Buzz\n";

        else

            cout<<i<<"\n";
     }
    return 0;
}
}
1
2
Fizz
4
Buzz
Fizz
7
.
.
14
FizzBuzz
.
.
.

.
.
.
Fizz
100

Shortest code in perl

for(1..100){$x=!($_%5);$y=!($_%3);$_=($y?"Fizz":"").($x?"Buzz":"")if($x+$y);print;print "\n";}

HackerRank


Sample Challenge 


This is a simple challenge to get things started. Given a sorted array (ar) and a number (V), can you print the index location of V in the array?
{The next section describes the input format. You can often skip it if you are using included methods. }

Input Format
There will be three lines of input:
  • V - the value you are looking for.
  • n - the size of the array.
  • ar - n numbers that makes up the array.
Output Format
Output the index of V in the array.
{The next section describes the constraints and ranges of the input. Occasionally you want to check here to see how big the input could be. }
Constraints

1<=n<=1000
-1000 <=x <= 1000 , x ∈ ar
{This “sample” shows the first input case. It is often useful to skip to the sample to understand a challenge. }
Sample Input
4
6
1 4 5 7 9 12
Sample Output
1
Explanation
V = 4. The 4 is located in the 1th spot on the array, so the answer is 1.
PROGRAM                                              OUTPUT
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    
    int n,*ar,V,a,c;
    cin>>V>>n;
    c=0;
    ar = new int[n];
    for(int i=0;i<n;i++)
    {
        cin>>ar[i];
        if(V==ar[i]&&c==0)
        {
            c=1;
            a=i;
        }
    }
    cout<<a;   
    return 0;
}
Input 

4
6
1 4 5 7 9 12
 Output

   1