C and C++ Programs for String Sorting

Here i am writing programs in c and c++ languages to sort a given number of strings. Sorting is carried out based on the ASCII values of the letters in the strings. The string comparing function strcmp() is used in the program. These programs sort a given set of strings in ascending order.To change it into descending order just replace > with < in comparison line. The comparison line in the code is marked by comment.

C Program:
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{

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


C and C++ Programs to Find Sum of Digits of a Given Number

The following are C and C++ programs to find sum of digits of any given integer.

C Program

#include<stdio.h>
#include<conio.h>
void main()
{

C and C++ Program for Swapping Two Numbers Without Third Variable

This post contains C and C ++ programs for swapping two given numbers without using any temporary variable. Only the two variables are required, not a third.

C Program
#include<stdio.h>
void main()
{

C and C++ swapping numbers

The following are C and C++ programs to swap the values of two variables using a third variable.

C Program:
#include<stdio.h>
void main()
{
int x,y,temp;
printf("Enter the value of xand y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\n x= %d\n y = %d\n",x,y);
temp = x;
x= y;
y = temp;
printf("After Swapping\n x= %d\ny= %d\n",x,y);
}

2 Different C and C++ Programs to Check Whether a Given String is Palindrome or Not

Here i am writing two different programs to check whether a given string (text) us palindrome or not. There are C and C++ programs for this.

C Programs:

(i) Using string functions:




#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char a[100],b[100];
printf("Enter the string to check if palindrome or not\n");
gets(a);
strcpy(b,a);
strrev(b);
if(strcmp(a,b)==0)
printf("\nEntered string is palindrome");
else
printf("\nEntered string is not palindrome");
getch();
} 

(ii) Comparing Character by Character


#include<string.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int len,i;
char a[100];
printf("Enter the string to check whether it is palindrome or not\n");
gets(a);
len=strlen(a);
for(i=0;i<len/2;i++)
{
if(a[i]!=a[len-i-1])
break;
}

if(i==len/2)
printf("\nEntered string is palindrome");
else
printf("\nEntered string is not palindrome");
getch();
}

C++ Programs:

(i) Using String functions:


C and C++ Program to Check Whether a Given Number is Even or Odd

C and C++ programs to check whether a given number is Odd or even

C Program

#include<stdio.h>
void main()
{
int n;
printf("Enter an integer\n");
scanf("%d",&n);
if ( n%2 == 0 )
printf("Even");
else
printf("Odd");
}

C++ Program

#include<iostream.h>
void main()
{
int n;
cout<<"Enter an integer\n";
cin>>n;
if ( n%2 == 0 )
cout<<"\nEven";
else
cout <<"\nOdd";
}


C and C++ Program to Check Whether a Given Year is Leap Year or Not

This is post contains a C program and a C++ program to check whether an input year is leap year or not. A year is a leap year if:

  1. The year is divisible by 400
  2. The year is divisible by 4 but not divisible by 100.
Any other year is not a leap year.

C program
#include <stdio.h>
void main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if(year%400==0 || ((year%4=0) && (year%100!=0)))
printf("%d is a leap year.\n", year); else
printf("%d is not a leap year.\n", year);
}


C++ program
#include <iostream.h>
void main()
{
int year;
cout<<"Enter a year to check if it is a leap year or not";
cin>>year;
if(year%400==0 || ((year%4=0) && (year%100!=0)))
cout<<"\n"<<year<<"is a leap year."; else
cout<<"\n"<<year<<"is NOT a leap year."; 
}

C program to Calculate Permutations and Combinations (nPr and nCr)

This is a C program to calculate total number of permutations and combinations 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 ();
exit(0);
}
}


C and C++ Programs to Check Whether a Given Number is Armstrong Number or Not

This post contains two programs, a C++ program and a C program. These are programs to check whether a given number is an Armstrong number or not. An Armstrong number is a positive integer for which the sum of 'n'th power of all of its digits is equal to the number itself. For example, 153 is a 3 digit Armstrong number because 13+53+33=153.

C ++ program:

# include <iostream.h>

# include <conio.h>

# include <math.h>

void main ()

{

clrscr();

int a,b=0,sum=0;

long int n;

cout<<"Enter the number to be checked\n ";

cin>>n;

if(n<1)
{
cout<<"\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)
cout<<"\nThe number is an ARMSTRONG number";
else
cout<<"\nThe number is NOT an ARMSTRONG number";
}
getch();
}


C Program:
# include <stdio.h>

# include <conio.h>

# include <math.h>

void main ()

{

clrscr();

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 Check Whether a Given String is Palindrome or Not

Here i am writing two different programs two check whether an input string is palindrome or not. The first program checks the string manually character by chatter from both ends. The other program make use of a string function strrev() (under string.h) to reverse the string. Therefore, the second program is more compact.

Program 1:

#include<stdio.h>

#include<string.h>

void main()

{

char a[20];

int i, len;

printf("\nEnter the string:\n");

gets(a);

len = strlen(a);

for (i = 0; i < len / 2; i++)

{

if (a[i] != a[len - i - 1])

break;

}

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:

C and C++ Program to Display Armstrong Numbers upto 1000

This post about C and C++ programs 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 of each digit. So, in the program, we should have a code to determine number 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.

C Program:

#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:


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

Program to Understand Bitwise Operators

This program is to understand working of bitwise operators


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

}

C Program to Calculate Determinant of Matrix of Order Upto 3

In this post i am sharing a C program to calculate determinant of matrices of order upto 3 (3x3).

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

}

C and C++ Program to Display Pascal's Triangle

This post gives C and C++ programs to display given number of rows of pascal's triangle. Thenuser can enter the number of rows of pascal triangle to be displayed.

C Program:

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

}