Search This Blog

C Program for Subtraction Without Using Minus Sign

The following is a program to subtract a number from another without using minus symbol. Instead of doing a normal subtract operation, the two's complement of the subtrahend is added to the minuend. (In 3-4, 3 is minuend and 4 is subtrahend.) In other words, the 1's complement and 1 is added to minuend.

#include<stdio.h>
void main()
{
int a,b,sum;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
sum = a + ~b + 1;
printf("Difference of two integers: %d",sum);
}


Program to Calculate Permutations and Combinations (nPr and nCr)

This is a C program to calculate number of permutations (nPr) and combinations (nCr) for given values of n and r. This program calculates nPr and nCr using functions.

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

long double fact( int);
int ncr( int ,int);
long npr( int ,int);
main()
{
int n,r;
printf(" Enter value of n & r \n");
scanf("%d %d",&n ,&r);
if( n>= r)
{
printf( "%d C %d is %d \n",n,r,ncr( n ,r));
printf("%d P %d is %ld\n",n,r,npr( n, r));
}
else
{
printf("\n n should be greater than r");
getch ();
}


long double fact( int p) { long double facts = 1; int i; for( i = 1; i<= p; i++) facts = facts * i; return(facts); } int ncr ( int n, int r) { return( fact( n) / (fact( r) * fact(n- r) ) ) ; } long npr( int n ,int r) { return( fact( n) / fact( n- r)); }

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();
}

Sizeof operator in C

Sizeof operator is a unary operator used in both C and C++ programming languages. This operator is used to get the size of a data type, variable, object of a class, structure variable etc in bytes. In some programs it may be necessary to use the size of certain variable or data type. Such situations often occur in file handling programs. But the size of data type may vary from machine to machine. In most of the 32 bit systems, int has a size of 4 bytes. But in some systems it is 2 bytes. So, if a program is written by assuming the size to be known, it will affect the portability of the program since a change in size of a data type may result in unexpected output. In such cases, the sizeof operator helps to obtain the size of data type or variable in bytes. The argument of sizeof can be a variable of primitive days types (int, char etc), an object (of class), a structure variable, union variable, pointer variables etc. The sizeof operator is very useful in dynamic memory allocation.

C program to show working of sizeof operator:

#include<stdio.h>

struct student

{

char name[5];

int roll;

int mysize;

};

main()
{
unsigned long int b;
printf("size of variable b: %d",sizeof b);
printf("\nsize of int:%d",sizeof(int));
printf("\nsize of float:%d",sizeof(float));
printf("\nsize of char:%d",sizeof(char));
printf("\nsize of long:%d",sizeof(long));
printf("\nsize of double:%d",sizeof(double));
printf("\nsize of short:%d",sizeof(short));
struct student s1;
s1.mysize=sizeof(s1.name)+sizeof(s1.roll)+sizeof(s1.mysize);
printf("\nSizeof structure student :%d",s1.mysize);
/*see the difference shown in size of structure */
printf("\nsize of structure student : %d",sizeof(struct student));
}

Output:



size of variable b: 4

size of int:4

size of float:4

size of char:1

size of long:4

size of double:8

size of short:2

Sizeof structure student :13

size of structure student : 16


Remember that the size may be different in different c compilers.


Program to Display All Armstrong Numbers upto 1000

This post is for C program to display all Armstrong numbers less than 1000. But before all, I should tell what an Armstrong number is. An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits. Consider an example. 153 is an Armstrong number. Since it is a 3 digit number, to check whether it is an Armstrong number or not, we should add the 3rd power digits together. So, in the program, we should have a code to determine number of digits in number, then add the nth powers (where n is the number of digits) of each digit in the number and check whether both the sum and original number are the same.

#include<stdio.h>
#include<math.h>
void main()
{
int n,sum,r,digits;
for(n=1;n<=1000;n++)
  {
  digits=0;
  r=n;
  sum=0;
  while(r!=0)  //Counting Digits
    {
    digits++;
    r/=10;
    }
  r=n;
  while(r!=0)  /*Adding Digits Raised To a Power That Is Equal To Number of Digits *
    {
    sum+=pow((r%10),digits);
    r=r/10;
    }
  if(sum==n)
    {
    printf("%5d",sum);
    }
  }
}

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));
}
}
}

Program to Understand Bitwise Operators

This is a C program that shows working of bitwise operators. The program takes two values, a and b. And displays the different bitwise operations' results. The program performs bitwise AND, bitwise OR, bitwise XOR, bitwise inversion (NOT), right bit shift and left bit shift operations.
#include<stdio.h>
main ()
{
unsigned int a = 60; /* 60 = 0011 1100*/
unsigned int b = 13; /* 13 = 0000 1101 */
printf ("a=60 ( 0011 1100)\nb=13 ( 0000 1101)\n"); 
int c = 0;
c = a&b ; /* 12 = 0000 1100 */
printf ("a&b=%d\n",c );
c = a | b ; /* 61 = 0011 1101 */
printf ("a|b=%d\n",c );
c = a ^ b ; /* 49 = 0011 0001 */
printf ("a^b=%d\n", c );
c = ~ a; /*-61 = 1100 0011 */
printf ("~a=%d\n" , c );
c = a << 2 ; /* 240 = 1111 0000 */
printf ("a<<2=%d\n" , c );
c = a >> 2 ; /*15 = 0000 1111*/
printf ("a>>2=%d\n" , c );
}

Determinant of Matrix Program - Program to Find Determinant of Matrix

In this post i am sharing a C program to calculate determinant of matrices. Determinant exists only for square matrix. Here i have wrote a c program to find determinant of matrices of order less than or equal to 3.

#include<stdio.h>
void main() { int a[10][10],i,m,j; int det=0; printf("\nEnter the order of matrix:\n "); scanf("%i",&m); printf("\nenter elements\n"); for(i=0;i<m;i++)  {  for(j=0;j<m;j++)   scanf("%d",&a[i][j]);  } printf("\nThe matrix is\n"); for(i=0;i<m;i++)  {  printf("\n");  for(j=0;j<m;j++)   printf("%3d",a[i][j]);  } printf("\nthe transpose is\n"); for(i=0;i<m;i++) { for(j=0;j<m;j++) printf("%3i",a[j][i]); printf("\n"); } if(m==1) { det=a[0][0]; printf("det=%i",det); } else if(m==2) { int k=1,l=1; for(i=0;i<2;i++) for(j=0;j<2;j++) { if(i==j) k*=a[i][j]; else l*=a[i][j]; } printf("\ndeterminant=%i",k-l); } else if(m==3) { for(i=0;i<3;i++) det=det+(a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3]-a[1][(i+2)%3]*a[2][(i+1)%3])); printf("\nDeterminant of matrix is: %d",det); } else printf("This program can't calculate determinant of matrix of order greater than 3"); }

C Program to Display Pascal's Triangle

Here is a C program to display given number of rows of pascal's triangle. The user can enter the number of rows of pascal triangle to be displayed.


#include<stdio.h>
#include<conio.h>
long factorial(int);
main()
{
int i, n, c;
printf("Enter the number of rows you wish to see in pascal triangle\n");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
for (c = 0; c <= (n - i - 2); c++)
printf(" ");
for (c = 0; c <= i; c++)
printf("%ld ", factorial(i) / (factorial(c) * factorial(i - c)));
printf("\n");
}
getch();
}

long factorial(int n)
{
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result * c;
return (result);
}