Tuesday, 17 December 2013

Armstrong number

/*
Q17.    Write a program to check whether a number is armstrong or not.
*/

#include<stdio.h>

main()
{
    int num,temp,flag=0,sum=0;  
    printf("\nEnter a number=");
    scanf("%d",&num);
    temp=num;
    while(num)
    {
    r=num%10;
        sum=sum+r*r*r;
        num=num/10;
    }          
    if(sum==temp)
        printf("\n%d is an armstrong number",temp);
    else
        printf("\n%d is not an armstrong number",temp);          
}

Monday, 16 December 2013

Insertion sort.

/* 
Write a program to sort n integers using insertion sort.
*/
#include <stdio.h>
void insert_sort(int arr[],int n)
{
    int i,j,temp;
    for(j=1;j<n;j++)
    {
        temp=arr[j];
        for(i=j-1;i>=0 && temp<arr[i];i--)
            arr[i+1]=arr[i];
        arr[i+1]=temp;
    }
}

main()
{
    int arr[100],i,j,n;
    printf("Enter the number of elements : ");
    scanf("%d",&n);
    for (i = 0; i < n; i++)
    {
        printf("Enter element %d : ",i+1);
        scanf("%d", &arr[i]);
    }
    printf("Unsorted list is :\n");
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
    insert_sort(arr,n);
    printf("Sorted list is :\n");
    for (i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");
}