A blog to help students to learn programming in various languages including C, C++, Java, Python and many more.

Subscribe For Free Latest Updates!

We'll not spam mate! We promise.

Sunday 10 July 2016


 In mathematics, the Fibonacci numbers or Fibonacci sequence are the numbers in the following integer sequence:

01, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144.....

The Fibonacci spiral: an approximation of the golden spiral created by drawing circular arcs connecting the opposite corners of squares in the Fibonacci tiling; this one uses squares of sizes 1, 1, 2, 3, 5, 8, 13, 21, and 34.
By definition, the first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two. 
Information source: wikipedia.org


Program:->

Program Output
Output

def fibo( x ):
    f = -1; s = 1
    for i in range(0,x):
        n = f + s
        f = s
        s = n
        print n
    return;
n = input ("Enter the total elements in the series: ")
print "The Fibonacci series is: "
fibo(n)
Read More >>

Saturday 9 July 2016


  In mathematics, the Fibonacci numbers or Fibonacci sequence are the numbers in the following integer sequence:

01, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144.....

The Fibonacci spiral: an approximation of the golden spiral created by drawing circular arcs connecting the opposite corners of squares in the Fibonacci tiling; this one uses squares of sizes 1, 1, 2, 3, 5, 8, 13, 21, and 34.
By definition, the first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two. 
Information source: wikipedia.org


Program:->
Program Output
Output


import java.util.Scanner;
public class seFibo {
    public static int fibo(int x) {
        int f = -1, s = 1, n;
        for (int i = 0; i < x; ++i) {
            n = f + s;
            f = s;
            s = n;
            System.out.println( n );
        }
        return (0);
    }
    public static void main(String[] args) {
        int n, i;
        Scanner sc = new Scanner (System.in);
        System.out.print("Enter the total elements in the series: ");
        n = sc.nextInt();
        System.out.println("The Fibonacci series is: ");
        fibo(n);
    }
}

Note: The program must be saved with name "SeFibo.java". 
Read More >>

Friday 8 July 2016


In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,

5! = 5  *  4  *  3  *  2  * 1 = 120.
The value of 0! is 1, according to the convention for an empty product.

The factorial operation is encountered in many areas of mathematics, notably in combinatorics, algebra, and mathematical analysis. Its most basic occurrence is the fact that there are n! ways to arrange n distinct objects into a sequence (i.e., permutations of the set of objects). This fact was known at least as early as the 12th century, to Indian scholars. Fabian Stedman in 1677 described factorials as applied to change ringing.

Information source: wikipedia.org


Program:->

Program Output
Output

f = 1
x = input("Enter a number: ")
for i in range(1,x+1):
    f = f * i
print "Factorial: %d" % f
Read More >>


  In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,

5! = 5  *  4  *  3  *  2  * 1 = 120.
The value of 0! is 1, according to the convention for an empty product.

The factorial operation is encountered in many areas of mathematics, notably in combinatorics, algebra, and mathematical analysis. Its most basic occurrence is the fact that there are n! ways to arrange n distinct objects into a sequence (i.e., permutations of the set of objects). This fact was known at least as early as the 12th century, to Indian scholars. Fabian Stedman in 1677 described factorials as applied to change ringing.

Information source: wikipedia.org


Program:->

Program Output
Output

import java.util.Scanner;
public class SeFact {
    public static void main(String[] args) {
        int x, f = 1;
        System.out.print("Enter a number: ");
        Scanner sc = new Scanner(System.in);
        x = sc.nextInt();
        for (int i = 1; i <= x; ++i) {
            f = f * i;
        }
        System.out.println("Factorial: " + f);
    }
}


Note: The program must be saved with name "SeFact.java" 
Read More >>

Saturday 19 March 2016


     In computer science, a search allows the efficient retrieval of specific items from a set of items, such as a specific record from a database.

The simplest, most general, and least efficient search structure is merely an unordered sequential list of all the items. Locating the desired item in such a list, by the linear search method, inevitably requires a number of operations proportional to the number n of items, in the worst case as well as in the average case. Useful search data structures allow faster retrieval; however, they are limited to queries of some specific kind. Moreover, since the cost of building such structures is at least proportional to n, they only pay off if several queries are to be performed on the same database (or on a database that changes little between queries).
Information source: wikipedia.org


Program:->

Program Output
Output

#include <stdio.h>
#include <conio.h>
int main()
{
int loc, n, data, x, a[100];
printf("Enter no. of values: ");
scanf("%d", &n);
printf("Enter %d values: ", n);
for(x=0; x<n; x++) {
scanf("%d", &a[x]);
}
printf("Entered array is: \n");
for(x=0; x<n; x++) {
printf("%d  ", a[x]);
}
printf("\nEnter data to search: ");
scanf("%d", &data);
for(x=0; x<n; x++) {
if(data==a[x]) {
printf("Location is: %d\n", x);
loc=x;
break;
}
loc=x;
}
if(data!=a[loc]) {
printf("Element not found\n");
}
return(0);
}
Read More >>


         In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,

5! = 5  *  4  *  3  *  2  * 1 = 120.
The value of 0! is 1, according to the convention for an empty product.

The factorial operation is encountered in many areas of mathematics, notably in combinatorics, algebra, and mathematical analysis. Its most basic occurrence is the fact that there are n! ways to arrange n distinct objects into a sequence (i.e., permutations of the set of objects). This fact was known at least as early as the 12th century, to Indian scholars. Fabian Stedman in 1677 described factorials as applied to change ringing.


Information source: wikipedia.org


Program:->

Program Output
Output

#include <stdio.h>
#include <conio.h>
int main() {
int x,f=1,i;
printf("Enter a number: ");
scanf("%d", &x);
for (i = 1; i <= x; ++i)
{
f=f*i;
}
printf("Factorial: %d\n",f);
return(0);
}
Read More >>


         In mathematics, the Fibonacci numbers or Fibonacci sequence are the numbers in the following integer sequence:

01, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144.....

The Fibonacci spiral: an approximation of the golden spiral created by drawing circular arcs connecting the opposite corners of squares in the Fibonacci tiling; this one uses squares of sizes 1, 1, 2, 3, 5, 8, 13, 21, and 34.
By definition, the first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two. 
Information source: wikipedia.org


Program:->

Program Output
Output

#include <stdio.h>
#include <conio.h>
int fibo(int);
int main() {
int n;
printf("Enter the total elements in the series: ");
scanf("%d", &n);
printf("The Fibonacci series is: \n");
fibo(n);
return(0);
}
int fibo(int x) {
int f=-1,s=1,n, i;
for (i = 0; i < x; i++) {
n=f+s;
f=s;
s=n;
printf("%d\n", n);
}
return(0);
}
Read More >>

Thursday 18 February 2016


ScreenShot
Output

#include <iostream>
using namespace std;
int search(int *a,int x,int n);
int main()
{
int loc,n,data,a[100];
cout<<"Enter no. of values: ";
cin>>n;
cout<<"Enter "<<n<<" values: ";
for(int x=0;x<n;x++) {
cin>>a[x];
}
cout<<"Entered array is: "<<"\n";
for(int x=0;x<n;x++) {
cout<<a[x]<<"  ";
}
cout<<"\nEnter element you want to delete: ";
cin>>data;
loc=search(&a[0],data,n);
if ( loc== -1 ) {
cout<<"Entered element is not present in array.";
} else {
for(int x=loc;x<n-1;x++) {
a[x]=a[x+1];
}
cout<<"Array after deletion:"<<"\n";
for(int x=0;x<n-1;x++) {
cout<<a[x]<<"  ";
}
}
return(0);
}

int search(int *a,int data,int n)
{
for(int x=0;x<n;x++) {
if(data==a[x]) {
return(x);
}
}
return(-1);
}

Read More >>

Sunday 24 January 2016


        In mathematics, the Fibonacci numbers or Fibonacci sequence are the numbers in the following integer sequence:

01, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144.....

The Fibonacci spiral: an approximation of the golden spiral created by drawing circular arcs connecting the opposite corners of squares in the Fibonacci tiling; this one uses squares of sizes 1, 1, 2, 3, 5, 8, 13, 21, and 34.
By definition, the first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two. 
Information source: wikipedia.org


Program:->
Program Screenshot
Output

#include <iostream>
using namespace std;
int fibo(int);
int main() {
int n,i;
cout<<"Enter the total elements in the series: ";
cin>>n;
cout<<"The Fibonacci series is: \n";
fibo(n);
return(0);
}
int fibo(int x) {
int f=-1,s=1,n;
for (int i = 0; i < x; ++i) {
n=f+s;
f=s;
s=n;
cout<<n<<"\n";
}
return(0);
}
Read More >>


     In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,

5! = 5  *  4  *  3  *  2  * 1 = 120.
The value of 0! is 1, according to the convention for an empty product.

The factorial operation is encountered in many areas of mathematics, notably in combinatorics, algebra, and mathematical analysis. Its most basic occurrence is the fact that there are n! ways to arrange n distinct objects into a sequence (i.e., permutations of the set of objects). This fact was known at least as early as the 12th century, to Indian scholars. Fabian Stedman in 1677 described factorials as applied to change ringing.

Information source: wikipedia.org


Program:->

Program Screenshot
Output
#include <iostream>
using namespace std;
int main() {
int x,f=1;
cout<<"Enter a number: ";
cin>>x;
for (int i = 1; i <= x; ++i) {
f=f*i;
}
cout<<"Factorial: "<<f<<"\n";
return(0);
}
Read More >>


Program Screenshot
Output
#include <iostream>
using namespace std;
struct node {
int info;
node* link;
}*start,*nNode;
node * createNew() {
nNode = new node;
cout<<"Enter info for node: ";
cin>>nNode->info;
return (nNode);
}
node display(node* temp) {
while(temp!=NULL) {
cout<<temp->info<<"\n";
temp=temp->link;
}
}
int main() {
int n=2,s,af;
start=NULL;
node* tempPtr;
cout<<"Press 1 to enter new node: ";
cin>>n;
while(n==1) {
cout<<"Where you want to insert node: \n1- In begining\t\t2- In between\t\t3- At end\n";
cin>>s;
switch(s) {
case 1:
if(start==NULL) {
start=createNew();
start->link=NULL;
} else {
nNode=createNew();
nNode->link=start;
start=nNode;
}
break;
case 2:
cout<<"\nInfo in linked list: \n";
display(start);
cout<<"After which info you want new element: ";
cin>>af;
tempPtr=start;
while(tempPtr!=NULL && tempPtr->info!=af) {
tempPtr=tempPtr->link;
}
if (tempPtr==NULL) {
cout<<"Entered info not found.\n";
} else {
nNode=createNew();
nNode->link=tempPtr->link;
tempPtr->link=nNode;
}
break;
case 3:
tempPtr=start;
while(tempPtr->link!=NULL && tempPtr!=NULL) {
tempPtr=tempPtr->link;
}
nNode=createNew();
tempPtr->link=nNode;
nNode->link=NULL;
break;
default:
cout<<"\n**************Error**************\n";
}
cout<<"Press 1 to enter new node or other no. to exit: ";
cin>>n;
}
tempPtr=start;
cout<<"\nInfo in linked list:\n";
display(start);
return (0);
}
Read More >>


Program Screenshot
Output

#include <iostream>
using namespace std;
struct node {
int info;
node* link;
}*start,*nNode;
node * createNew(int in) {
nNode = new node;
nNode->info=in;
return (nNode);
}
node display(node* temp) {
while(temp!=NULL) {
cout<<temp->info<<"  ";
temp=temp->link;
}
cout<<"\n";
}
node * search(node* tempPtr,int item) {
while(tempPtr!=NULL && tempPtr->info!=item) {
tempPtr=tempPtr->link;
}
return (tempPtr);
}
int main()
{
int n=2,s,item;
int a[8]={7,9,8,5,4,6,2,1};
start=NULL;
node* tempPtr;
for (int i = 0; i < 8; ++i)
{
if(start==NULL) {
start=createNew(a[i]);
start->link=NULL;
} else {
nNode=createNew(a[i]);
nNode->link=start;
start=nNode;
}
}
cout<<"Elements of linked list: \n";
display(start);
cout<<"Enter element you want to search: ";
cin>>item;
tempPtr=search(start,item);
if (tempPtr!=NULL && tempPtr->info==item) {
cout<<"Item found in list.\n";
} else {
cout<<"Item doesn't exists in list.\n";
}
return(0);
}
Read More >>

Saturday 23 January 2016


Program Screenshot
Output

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int a,b,c,d=0,x;
cout<<"Enter a,b & c:";
cin>>a>>b>>c;
d=(b*b)-4*a*c;
cout<<"Result:\n";
if(d<0) {
cout<<"d<0"<<endl;
cout<<(-b-sqrt(d))/2*a<<endl;
cout<<(-b+sqrt(d))/2*a<<endl;
} else if(d==0) {
cout<<"D=0"<<endl;
cout<<-b/2*a<<endl;;
} else {
cout<<"D>0"<<endl;
cout<<"Imaginary number: "<<endl;
}
}

Read More >>


Program Screenshot
Output

#include <iostream>
using namespace std;
int mergeSort(int a[],int beg,int end);
int merge(int a[],int beg,int mid,int end);
int main()
{
int n,a[100];
cout<<"Enter no. of values: ";
cin>>n;
cout<<"Enter "<<n<<" values: ";
for(int x=0;x<n;x++) {
cin>>a[x];
}
mergeSort(a,0,n-1);
cout<<"Array after Sorting using Merge Sort:\n";
for(int x=0;x<n;x++) {
cout<<a[x]<<"\n";
}
return(0);
}
int mergeSort(int a[],int beg,int end)
{
if(end <= beg) {
return(0);
}
int mid=(beg+end)/2;
mergeSort(a,beg,mid);
mergeSort(a,mid+1,end);
merge(a,beg,mid,end);
}
int merge(int a[],int beg,int mid,int end)
{
int h=beg,i=beg,j=mid+1;
int b[100];
while(h<=mid && j<=end) {
if(a[h]<=a[j]) {
b[i]=a[h];
i++;
h++;
} else {
b[i]=a[j];
i++;
j++;
}
}
if(h>mid) {
for(int k=j;k<=end;k++) {
b[i]=a[k];
i++;
}
} else {
for(int k=h;k<=mid;k++) {
b[i]=a[k];
i++;
}
}
for(int k=beg;k<=end;k++) {
a[k]=b[k];
}
}

Read More >>


Program Screenshot
Output

#include <iostream.h>
using namespace std;
int a[100];
int main()
{
int n,item,loc;
int binary(int beg,int end,int item);
cout<<"Enter no. of elements: ";
cin>>n;
cout<<"Enter "<<n<<" elements in Sorted Order: ";
for(int i=0;i<n;i++) {
cin>>a[i];
}
cout<<"Enter item you want to search: ";
cin>>item;
loc=binary(0,n,item);
if(a[loc]==item) {
cout<<"\nData is Found at Location: "<<loc<<"\n";
} else {
cout<<"Data is Not Found";
}
}
int binary(int beg,int end,int item)
{
int mid=(beg+end)/2;
if (end < beg) {
    return -1;
}
if (a[mid]==item) {
return(mid);
} else if(a[mid]<item) {
binary(mid+1,end,item);
} else {
binary(beg,mid-1,item);
}
}
Read More >>


     In computer science, a search data structure is any data structure that allows the efficient retrieval of specific items from a set of items, such as a specific record from a database.

The simplest, most general, and least efficient search structure is merely an unordered sequential list of all the items. Locating the desired item in such a list, by the linear search method, inevitably requires a number of operations proportional to the number n of items, in the worst case as well as in the average case. Useful search data structures allow faster retrieval; however, they are limited to queries of some specific kind. Moreover, since the cost of building such structures is at least proportional to n, they only pay off if several queries are to be performed on the same database (or on a database that changes little between queries).
Information source: wikipedia.org


Program:->

Program Screenshot
Output

#include <iostream>
using namespace std;
int main()
{
int loc,n,data,a[100];
cout<<"Enter no. of values: ";
cin>>n;
cout<<"Enter "<<n<<" values: ";
for(int x=0;x<n;x++) {
cin>>a[x];
}
cout<<"Entered array is: "<<"\n";
for(int x=0;x<n;x++) {
cout<<a[x]<<"  ";
}
cout<<"\nEnter data to search: ";
cin>>data;
for(int x=0;x<n;x++) {
if(data==a[x]) {
cout<<"Location is: "<<x<<"\n";
loc=x;
break;
}
loc=x;
}
if(data!=a[loc]) {
cout<<"Element not found\n";
}
return(0);
}

Read More >>


Program Screenshot
Output


#include <iostream>

using namespace std;
int insertionSort(int a[],int n);
int main()
{
int n,data,a[100];
cout<<"Enter no. of values: ";
cin>>n;
cout<<"Enter "<<n<<" values: ";
for(int x=0;x<n;x++) {
cin>>a[x];
}
insertionSort(a,n);
cout<<"Array after Sorting using Insertion Sort: \n";
for(int x=0;x<n;x++) {
cout<<a[x]<<"\n";
}
return(0);
}
int insertionSort(int a[],int n)
{
int pivot,temp;
for(int startPos=1;startPos<n;startPos++) {
pivot=startPos;
while(pivot >= 1 && a[pivot] < a[pivot-1]) {
temp=a[pivot];
a[pivot]=a[pivot-1];
a[pivot-1]=temp;
pivot--;
}
}
}

Read More >>

Monday 18 January 2016


Program ScreenShot
Output

#include <iostream.h>

using namespace std;
int main()
{
   int n, i = 3, count, c;
   cout<<"Enter the number of prime numbers required: ";
   cin>>n;
   if ( n >= 1 ) {
      cout<<"First "<<n<<" prime numbers are:\n";
      cout<<"2\n";
   }
   count = 2 ;
   while (count <= n) {
      for ( c = 2 ; c <= i - 1 ; c++ ) {
         if ( i%c == 0 )
            break;
      }
      if ( c == i ) {
         cout<<i<<"\n";
         count++;
      }
      i++;
   }
   return 0;
}
Read More >>

Sunday 17 January 2016


Program ScreenShot
Output

#include <iostream>

using namespace std;
int main()
{
int z,n,a[100];
cout<<"Enter no. of values: ";
cin>>n;
cout<<"Enter "<<n<<" values: ";
for(int x=0;x<n;x++)
{
cin>>a[x];
}
z=a[0];
for(int x=1;x<n;x++)
{
if(z<a[x]) {
z=a[x];
}
}
cout<<"Largest: "<<z<<"\n";
}

Read More >>

Saturday 16 January 2016


Program ScreenShot

#include<iostream>
using namespace std;
int main()
{
int a[100][100];
int r,c;
cout<<"Enter no. of rows & columns: ";
cin>>r>>c;;
cout<<"Enter "<<r*c<<" values : ";
for(int i=0;i<r;i++) {
for(int j=0;j<c;j++) {
cin>>a[i][j];
}
}
for(int i=0;i<r;i++) {
for(int j=0;j<c;j++) {
a[i][c]+=a[i][j];
}

}
for(int i=0;i<r;i++) {
for(int j=0;j<c+1;j++) {
a[r][j]+=a[i][j];
}

}
cout<<"SUM : "<<"\n";
for(int i=0;i<r+1;i++) {
for(int j=0;j<c+1;j++) {
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
}

Read More >>


Program ScreenShot

#include <iostream>

using namespace std;
int quickSort(int a[],int l,int r);
int main()
{
int n,data,a[100];
cout<<"Enter no. of values: ";
cin>>n;
cout<<"Enter "<<n<<" values : ";
for(int x=0;x<n;x++) {
cin>>a[x];
}
quickSort(a,0,n);
cout<<"Array after Sorting using Quick Sort: \n";
for(int x=0;x<n;x++) {
cout<<a[x]<<"\n";
}
return(0);
}
int quickSort(int a[],int l,int r)
{
int temp;
if(r-l<=1) {
return(0);
}
int y=l+1;
for(int g=l+1;g<r;g++) {
if(a[g]<=a[l]) {
temp=a[y];
a[y]=a[g];
a[g]=temp;
y++;
}
}
temp=a[y-1];
a[y-1]=a[l];
a[l]=temp;
quickSort(a,l,y-1);
quickSort(a,y,r);
}
Read More >>


Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. 

The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort. It can be practical if the input is usually in sorted order but may occasionally have some out-of-order elements nearly in position.

Performance:->

  • Best case: O(n)
  • Average case: О(n2)
  • Worst case: О(n2)

Information source: wikipedia.org


Program:->

Program Output
Output
#include <iostream>
using namespace std;
int bubble(int *ptr,int n);
int main()
{
int n,data,a[100];
int *ptr;
ptr=a;
cout<<"Enter no. of values: ";
cin>>n;
cout<<"Enter "<<n<<" values: ";
for(int x=0;x<n;x++) {
cin>>*(ptr+x);
}
bubble(ptr,n);
cout<<"Array after Sorting using BUBBLE Sort: \n";
for(int x=0;x<n;x++) {
cout<<*(ptr+x)<<"\n";
}
return(0);
}
int bubble(int *ptr,int n)
{
int x,temp;
for(int k=0;k<n;k++) {
x=0;
while(x<n-k-1) {
if(*(ptr+x)>*(ptr+x+1)) {
temp=*(ptr+x+1);
*(ptr+x+1)=*(ptr+x);
*(ptr+x)=temp;
}
x=x+1;
}
}
}

Read More >>


An array type is a data type that is meant to describe a collection of elements (values or variables), each selected by one or more indices (identifying keys) that can be computed at run time by the program. Such a collection is usually called an array variablearray value, or simply array. By analogy with the mathematical concepts of vector and matrix, array types with one and two indices are often called vector type and matrix type, respectively.

In order to effectively implement variables of such types as array structures (with indexing done by pointer arithmetic), many languages restrict the indices to integer data types (or other types that can be interpreted as integers, such as bytes and enumerated types), and require that all elements have the same data type and storage size. Most of those languages also restrict each index to a finite interval of integers, that remains fixed throughout the lifetime of the array variable. In some compiled languages, in fact, the index ranges may have to be known at compile time.
Information source: wikipedia.org


Program:->
Program ScreenShot
Output


#include <iostream.h>
using namespace std;
int main() {
   int arr[30], i, j, num, temp;

   cout<<"Enter no. of elements : ";
   cin>>num;
   cout<<"Enter "<<num<<" values : ";
   for (i = 0; i < num; i++) {
      cin>>arr[i];
   }
   j = num - 1;
   i = 0;  
   while (i < j) {
      temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
      i++;
      j--;
   }
   cout<<"\nResult after reversal : ";
   for (i = 0; i < num; i++) {
      cout<<arr[i]<<"   ";
   }
   return (0);
}
Read More >>


Copyright © 2016 - ProgrmIt - All Rights Reserved
(Articles Cannot Be Reproduced Without Author Permission.)
Design By : | Powered By: Blogger