Search This Blog

Showing posts with label programme. Show all posts
Showing posts with label programme. Show all posts

Program to Check Armstrong Number

An Armstrong number is a positive integer for which the sum of 'n'th power of its digits is equal to the number itself where n is the number of digits in the number. This is a c program to check whether a given number is an Armstrong number or not. For example, 153 is a 3 digit Armstrong number because 13+53+33=153.

# include <stdio.h>
# include <conio.h>
# include <math.h>
void main ()
{
int a,b=0,sum=0;
long int n;
printf("Enter the number to check\n ");
scanf("%i",&n);
if(n<1)
{
printf ("\nThe number should be greater than 0");
}
else
{
a=n;
//counting the digits
while (a>0)
  {
  a=a/10;
  b++;
  }
a=n;
//adding up bth power of digits
while(a>0)
  {
  sum=sum+pow(( a%10) ,b);
  a=a/10;
  }
if(sum==n)
  printf ("\nThe number is an ARMSTRONG number");
else
  printf ("\nThe number is NOT an ARMSTRONG number");
}
getch();
}

C Program to Sort a Matrix Column wise using Pointers

This is a c program to sort a mxn matrix column wise in ascending order using pointers. To sort column wise in descending order, just replace the '>' symbol in the comparing line of code into '<'. That part of the code is marked with a comment 'comparison'.

#include<stdio.h>
main()
{
int a[7][7],i,j,k,m,n,temp;

//reading
printf("enter number of rows of matrix\n");
scanf("%i",&m);
printf("enter number of columns of matrix\n");
scanf("%i",&n);
printf("enter the elements\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
//displaying
printf("the matrix you entered:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%4d",*(*(a+i)+j));
}
}


//sorting
for(i=0;i<n;i++)
{
for(k=0;k<m-1;k++)
{
for(j=0;j<m-k-1;j++)
{
//comparison
if( *(*(a+j)+i)> *(*(a+(j+1))+i)) 
{
temp=*(*(a+j)+i);
*(*(a+j)+i)=*(*(a+(j+1))+i);
*(*(a+(j+1))+i)=temp;
}
}
}
}
//displaying
printf("\nSorted Matrix:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%4d",*(*(a+i)+j));
}
}
}