Check Armstrong No B/W any Range 1 to100000

//code
#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;
main()
{
int num,rem,j=1,k=1,count=0,sum=0;
printf("Enter no\n");
scanf("%d",&num);
int copy=num;
while(num>0)
{
num=num/10;
count=count+1;
}
num=copy;
for(int i=0;i<count;i++)
{
rem=num%10;
while(j<=count)
{
k=k*rem;
j++;
}
//Instead of loop i can directly use pow(); function to get power
//k=pow(rem,count);
sum=sum+k;
j=1;
k=1;
num=num/10;
}
if(sum==copy)
{
printf("Armstrong no\n");
}
else
{
printf("Not an armstrong no\n");
}
}

Find Armstrong No B/W 1 to 1000000

//code

#include<iostream>
#include<math.h>
using namespace std;
main()
{
int sum,k,r,i,count=0;
for(i=1;i<100000;i++)
{
count=0;
k=i;
int num=i;
sum=0;
while(num>0)
{
num=num/10;
count=count+1;
}
while(k>0)
{
r=k%10;
//if count is 3 then it is r power 3 by using pow function
//if count is 4 then it is r power 4
sum=sum+pow(r,count);
k=k/10;
}
if(sum==i)
{
printf("Armstrong no=%d\n",i);
}
}
}

01_KnapSack Problem Dynamic Programming

// A Dynamic Programming based solution for 0-1 Knapsack problem
//0_1 knapsack because either we have to take value i.e 1 or discard 0 we can not take fraction.
#include<stdio.h>

// A utility function that returns maximum of two integers
int max(int a, int b) { return (a > b)? a : b; }

// Returns the maximum value that can be put in a knapsack of capacity W
int knapSack(int W, int wt[], int val[], int n)
{
   int i, w;
   int K[n+1][W+1];

   // Build table K[][] in bottom up manner
   for (i = 0; i <= n; i++)
   {
       for (w = 0; w <= W; w++)
       {
           if (i==0 || w==0)
               K[i][w] = 0;
           else if (wt[i-1] <= w)
              /*if weight of ith item is less than the weight of  bag then we have two choice
  1.TO go for optimal solution by comapring and find better option
                                   OR
                          2.Simply keep the weight of ith element*/

    K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]],  K[i-1][w]);
           else
                 K[i][w] = K[i-1][w];
       }
   }
printf("KnapSack Matrix");
for(int i=0;i<n+1;i++)
    {
    for(int j=0;j<W+1;j++)
    {
    printf("%d  ",K[i][j]);
    }
    printf("\n");
    }
 //Maximum value will be at the end of matrix
   return K[n][W];
}

int main()
{
    int val[] = {60, 100, 120,150,200,50};
    int wt[] = {10, 20, 30,40,50,60};
    int  W = 100;
    int n = sizeof(val)/sizeof(val[0]);
    printf("Maximum Values=%d", knapSack(W, wt, val, n));
    return 0;
}

Linux Shell Programming

Heap Sort

// C++ program for implementation of Heap Sort
#include <iostream>
using namespace std;

// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
void heapify(int arr[], int n, int i)
{

    int largest = i;  // Initialize largest as root
    int l = 2*i + 1;  // left = 2*i + 1
    int r = 2*i + 2;  // right = 2*i + 2

    // If left child is larger than root
    if (l < n && arr[l] > arr[largest])
        largest = l;
       
    // If right child is larger than largest so far
    if (r < n && arr[r] > arr[largest])
        largest = r;

    // If largest is not root
    if (largest!=i)
    {
   
    swap(arr[i], arr[largest]);
    //which ever is large left or right from ROOT will be swap by ROOT
    //a[i] is root,a[largest] is left or right node value.
   

        // Recursively heapify the affected sub-tree
        heapify(arr, n, largest);
    }
}

// main function to build max heap
void heapSort(int arr[], int n)
{
    // Build heap (rearrange array)
    for (int i = n/2-1; i >= 0; i--)
    {
        heapify(arr, n, i);
       
    }
   

    // One by one extract an element from heap
    for(int k=n-1;k>=0;k--)
    {
        // Move Root  Node to end
        swap(arr[0], arr[k]);
        // call max heapify on the reduced heap
        heapify(arr, k, 0);
    }
}

/* A utility function to print array of size n */
void printArray(int arr[], int n)
{
    for (int i=0; i<n; ++i)
        cout << arr[i] << " ";
    cout << "\n";
}

// Driver program
int main()
{
    int arr[] = {50,75,60,35,45,25,95};
    int n = sizeof(arr)/sizeof(arr[0]); 
    heapSort(arr, n);

    cout << "Sorted array is \n";
    printArray(arr, n);
}

Selection Sort

//Code

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
void selectionSort(int *p,int n)
{
int min_index;
for(int i=0;i<n-1;i++)
//last value ke liye loop nh chalana hai kyuki vo end mai 
//correct position pai aa jaegi
{  min_index=i;
for(int j=i+1;j<n;j++)
//har value ko baki n-1 values se comapre krega.
{
if(p[j]<p[min_index])
{
swap(p[min_index],p[j]);
}


}
}
}
main()
{

int a[]={5,4,7,8,2,1,9,10,6,3};
int n=sizeof(a)/sizeof(a[0]);
selectionSort(a,n);
printf("Sorted Array\n");
for(int i=0;i<n;i++)
{
printf("%d ",a[i]);
}

}
--------------------------------------------------------------------------------------------------------------------------
/*Alternatively you can use this method for user input

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
void selectionSort(int *p,int n)
{
int min_index;
for(int i=0;i<n-1;i++)
//last value ke liye loop nh chalana hai kyuki vo end mai 
//correct position pai aa jaegi
{  min_index=i;
for(int j=i+1;j<n;j++)
//har value ko baki n-1 values se comapre krega.
{
if(p[j]<p[min_index])
{
swap(p[min_index],p[j]);
}


}
}
}
main()
{
int n;
   printf("Enter Size of Array\n");
   scanf("%d",&n);
   printf("Enter the %d elements of array\n",n);
int *arr;
arr=(int*)malloc(100*sizeof(int));
//we can use either calloc or malloc.Syntax of calloc is
//arr=(int*)calloc(n,sizeof(int)); here you save the memory
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
selectionSort(arr,n);
printf("Sorted Array\n");
for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}


}*/