Blog Moved

This website completely moved to new domain. For latest content, visit www.programmingposts.com

Search This Blog

12 May 2013

C PROGRAM TO PRINT NUMBERS 1 TO N WITHOUT USING LOOPS

C PROGRAM TO PRINT NUMBERS 1 TO N WITHOUT USING LOOPS


#include<stdio.h>
void print_numbers(int,int); //declaring function definition
main()
{
  int n;
  printf("\n *** C PROGRAMS BLOG ***");
  printf("\n >>> Program to print numbers 1 to n without using Loops <<<");
  printf("\n\n Enter the value of n: ");
  scanf("%d",&n); //taking value of n as input from user
  print_numbers(1,n); //calling the function
  getch();
}
void print_numbers(int i,int max)
{
 printf("%3d\t",i);
   if(i<max)
   {
    //calling function recursively
    print_numbers(++i,max);
   }
   return;
}

Sample Output :



6 May 2013

C PROGRAM TO PRINT THE ELEMENTS OF ARRAY IN REVERSE ORDER

C PROGRAM TO PRINT THE ELEMENTS OF ARRAY IN REVERSE ORDER

#include<stdio.h>
#define MAX 25 //maximum size of array is 25

main()
{
  int i,n,arr[MAX];

  printf(" *** C PROGRAMS BLOG ***");
  printf("\n >>> Program to Print the element of array in Reverse Order<<<");
  printf("\n\n Enter the size of array: ");
  scanf("%d",&n); //taking size of array as input from user
  printf("\n Enter the %d elements of array: \n",n);
  
  //taking input all the elements of array using for loop
  for(i=0;i<n;i++)
  {
   printf("\n arr[%d]:",i+1);
   scanf("%d",&arr[i]);    
  }
   printf("\n The Elements of Array in Reverse order are : \n");
   
    //Printing the elements of array in reverse order
   for(i=n-1;i>=0;i--)
   {
    printf(" %d ",arr[i]);   
   }
   getch();
}

Sample Output :



C PROGRAM TO FIND LARGEST AND SMALLEST NUMBER IN AN ARRAY

C PROGRAM TO FIND LARGEST AND SMALLEST NUMBER IN AN ARRAY

#include<stdio.h>
#define MAX 25 //maximum size of array is 25

main()
{
  int i,n,LargeNo,SmallNo,arr[MAX];

  printf(" *** C PROGRAMS BLOG ***");
  printf("\n >>> Program to find Largest element in array <<<");
  printf("\n\n Enter the size of array: ");
  scanf("%d",&n); //taking size of array as input from user
  printf("\n Enter the %d elements of array: \n",n);
  
  //taking input all the elements of array using for loop
  for(i=0;i<n;i++)
  {
   printf("\n arr[%d]:",i+1);
   scanf("%d",&arr[i]);    
  }
   LargeNo = arr[0];
   SmallNo = arr[0];
   for(i=0;i<n;i++)
   {
    //checking for Largest Number
    if(arr[i]>LargeNo)
    {
     LargeNo = arr[i];
    }
    //checking for Smallest Number
    if(arr[i]<SmallNo)
    {
     SmallNo = arr[i];
    }
   }
  //printing the largest and Smallst elements in array
  printf("\n The Largest Element in the Array is: %d ",LargeNo);
  printf("\n The Smallest Element in the Array is: %d ",SmallNo);
  getch();
}

Sample Output :

C PROGRAM TO FIND LARGEST / BIGGEST NUMBER IN ARRAY

C PROGRAM TO FIND LARGEST / BIGGEST NUMBER IN ARRAY

#include<stdio.h>
#define MAX 25 //maximum size of array is 25

main()
{
  int i,n,LargeNo,arr[MAX];

  printf("*** C PROGRAMS BLOG ***");
  printf("\n>>> Program to find Largest element in array <<<");
  printf("\n\n Enter the size of array: ");
  scanf("%d",&n); //taking size of array as input from user
  printf("\n Enter the %d elements of array: \n",n);
  
  //taking input all the elements of array using for loop
  for(i=0;i<n;i++)
  {
   printf("\n arr[%d]:",i+1);
   scanf("%d",&arr[i]);    
  }
   LargeNo = arr[0];
   for(i=0;i<n;i++)
   {
   	if(arr[i]>LargeNo)
   	{
   		LargeNo = arr[i];
   	}
   }
  //printing the largest element in array
  printf("\n The Largest Element in the Array is: %d ",LargeNo);
  getch();
}

Explanation : The above program is to find the largest element in the given array of elements. Here we are using declaring the maximum size of array as 25 using #define directive .

Sample Output :

22 Apr 2013

C PROGRAM TO FIND WHETHER STRING IS A PALINDROME

C PROGRAM TO FIND WHETHER STRING IS PALINDROME :

#include<stdio.h>
#include<string.h>
main()
{
 char str1[25],str2[25];
 printf("\n *** C Programs Blog ***");
    printf("\n >>> C Program to find whether string is Palindrome <<<");
    printf("\n\n Enter the string: ");
    scanf("%s",str1);
    strcpy(str2,str1); //copying str1 in str2
    strrev(str2);  //reversing str2
    if(strcmp(str1,str2) == 0) //checking whether str1 and atr2 are same
    { 
      printf("\n Entered string \" %s \" is a Palindrome",str1);
    }
    else
    {
     printf("\n Entered string \" %s \" is not a Palindrome",str1);
    }
    getch();   
}

Sample Output :

14 Apr 2013

C PROGRAM TO PRINT SOURCE PROGRAM NAME

#include<stdio.h>
main(int argc, char *argv[])
{
	char *p=argv[0];
	printf("\n *** C PROGRAMS BLOG ***");
	printf("\n >>> C PROGRAM TO PRINT NAME OF SOURCE PROGRAM <<< \n");
	printf("\n Program name is :  %s ",p);
	getch();
}

10 Mar 2013

C PROGRAM TO CHECK WHETHER GIVEN YEAR IS LEAP YEAR OR NOT


C PROGRAM TO CHECK WHETHER GIVEN YEAR IS LEAP YEAR OR NOT

#include<stdio.h>
void LeapYear(int);
void main()
{
    int year;
    printf("\n *** C PROGRAMS BLOG *** \n");
    printf("\n >>>> C PROGRAM TO CHECK WHETHER GIVEN YEAR LEAP YEAR OR NOT <<<< \n");
    printf("\n Enter the year : ");
    scanf("%d",&year);
    LeapYear(year);
    getch();
    return 0;
}
void LeapYear(int yr)
{
    int rem1,rem2;
    rem1 = yr%4 ; //rem1 = 0 for leap year
    rem2 = yr%100; //rem2! = 0 for leap year
    if((rem1 == 0) && (rem2!=0) || yr%400 == 0)
    {
        printf("\n The given year %d is Leap Year..",yr);
    }
    else
    {
        printf("\n The given year %d is Not Leap Year..",yr);
    }
    
}

Sample Output :



C PROGRAM TO FIND ROOTS OF QUADRATIC EQUATION


C PROGRAM TO FIND ROOTS OF QUADRATIC EQUATION

#include<stdio.h>
#include<math.h>
int main()
{
    float a,b,c,x,y,disc,p,q,r;
    printf("\n *** C PROGRAMS BLOG ***");
    printf("\n >>>> C PROGRAM TO FIND ROOTS OF QUADRATIC EQUATION <<<<");
    printf("\n Enter the values of a,b,c : ");
    scanf("%f %f %f",&a,&b,&c);
    disc=(b*b)-(4*a*c);
    if(disc == 0)
    {
        x=(-b)/(2*a);
        y=x;
        printf("\n Roots are Equal..");
        printf("\n x= %f , y= %f",x,y);
    }
     else if(disc < 0)
    {
        p=(-b)/(2*a);
        q=sqrt(-disc)/(2*a);
        printf("\n Roots are Complex and Imaginary..");
        printf("\n x= %f+%fi , y= %f-%fi",p,q,p,q);
    } 
     else if(disc > 0)
    {
        r=sqrt(disc);
        x=((-b)+r)/(2*a);
        y=((-b)-r)/(2*a);
        printf("\n Roots are Real Numbers..");
        printf("\n x= %f , y= %f",x,y);
    }
    else if(a==0)
    {
      x=(-c)/b;
      printf("\n Root is x = %f ",x);
    }
    getch();
    return 0;
}

Sample Output :


9 Mar 2013

C PROGRAM EXAMPLE FOR toupper() FUNCTION

C PROGRAM EXAMPLE FOR toupper() FUNCTION :


#include <stdio.h>
//#include<string.h>
int main() 
{
  int i;
  char str[20];
  printf("\n >>> C PROGRAM TO IMPLEMENT toupper() FUNCTION <<< \n");
  printf( "\n Enter the string in Lower or Mixed Case: ");
  //scanf("%s",&str); //scanf() wont allow spaces
  gets(str); //gets() allows apaces
  printf("\n The entered string in Upper Case is: ");
  for(i=0;i<=strlen(str);i++)
  {
      printf("%c",toupper(str[i]));
  }
  getch();
  return 0;
}

Output :

C PROGRAM EXAMPLE FOR tolower() FUNCTION


C PROGRAM EXAMPLE FOR tolower() FUNCTION

#include <stdio.h>
//#include<string.h>
int main() 
{
  int i;
  char str[20];
  printf("\n >>> C PROGRAM TO IMPLEMENT tolower() FUNCTION <<< \n");
  printf( "\n Enter the string in Caps or Mixed Case: ");
  //scanf("%s",&str); //wont allow spaces
  gets(str); //gets() allows apaces
  printf("\n The entered string in Lower Case is: ");
  for(i=0;i<=strlen(str);i++)
  {
      printf("%c",tolower(str[i]));
  }
  getch();
  return 0;
}

Output :

C PROGRAM TO PERFORM ARITHMETIC OPERATIONS USING SWITCH LOOP


C PROGRAM TO PERFORM ARITHMETIC OPERATIONS USING SWITCH LOOP :

#include<stdio.h>
void main()
{
    float value1,value2;
    char operator;
    printf("\n >>> PROGRAM TO PERFORM ARITHMETIC OPERATIONS USING SWITCH LOOP <<< \n");
    printf("\n Enter Your Expression : ");
    scanf("%f %c %f",&value1,&operator,&value2);
    switch(operator)
    {
        case '+' : printf("\n Addition Result  is :  %f \n",value1+value2);
            break;
        case '-' : printf("\n Subtraction Result  is : %f \n",value1-value2);
            break;
        case '*' : printf("\n Multiplication Result  is : %f \n",value1*value2);
            break;
        case '/' : printf("\n Division Result is : %f \n",value1/value2);
            break;
        default : printf("\n Unknown operator");
            break;
    }
    getch();
}

Output :


visit :  C Program to Show the Operations of Arithmetic Operators 

26 Jan 2013

C PROGRAM TO PERFORM MATRIX MULTIPLICATION


C PROGRAM FOR PRODUCT OF TWO MATRICES


#include<stdio.h>
#include<conio.h>
void main()
{
  //using 2D-ARRAYS
  int RowSize,ColSize;
  int Matrix1[5][5];
  int Matrix2[5][5];
  int ResultMatrix[5][5];
  int i, j;
  //clrscr();
  printf("\n >>> PROGRAM To PERFORM MULTIPLICATION OF TWO MATRICES <<<\n");
  printf("\n Enter the Size of a Matrix(For Example:3 3) : ");
  scanf("%d %d",&RowSize,&ColSize);

  if (RowSize > 5 || ColSize > 5)   //limiting the size of matrix
  {
    printf(" The Size Of Matrix should Be in Less Than 5 (limiting size of array)");
    printf("\n\n\t Press Enter key to exit....");
    getch();
    return;
   }

   else
   {
     //Initializing all the elements to zero
     for (i = 0; i < RowSize; i++)
     {
        for (j = 0; j < ColSize; j++)
        {
            Matrix1[i][j] = 0;
            Matrix2[i][j] = 0;
         }
      }
      //Reading elements of Matrix1
      printf("\n Enter the elements of Matrix1(%d * %d) \n", RowSize, ColSize);
      for (i = 0; i < RowSize; i++)
      {
        for (j = 0; j < ColSize; j++)
        {
           printf(" Matrix1[%d][%d] : ", i, j);
           scanf("%d",&Matrix1[i][j]);
        }
       }

       //Reading elements of Matrix2
       printf("\n Enter the elements of Matrix2(%d * %d) \n", RowSize, ColSize);
       for (i = 0; i < RowSize; i++)
       {
         for (j = 0; j < ColSize; j++)
         {
           printf(" Matrix2[%d][%d] : ", i, j);
           scanf("%d",&Matrix2[i][j]);
         }
        }

        //calculating ResultMatrix, by Multiplying the elements of Matrix1 and Matrix2
        for (i = 0; i < RowSize; i++)
        {
          for (j = 0; j < ColSize; j++)
          {
            ResultMatrix[i][j] = Matrix1[i][j] * Matrix2[i][j];
          }
        }

        //Printing Result Matrix
        printf("\n\n\t*** Result Matrix  ***\n\n\t");
        for (i = 0; i < RowSize; i++)
        {
           for (j = 0; j < ColSize; j++)
           {
             if (ResultMatrix[i][j] > 0 &&  ResultMatrix[i][j] < 10)
             {
               //prinnting number as 01,02,etc,.
                printf("0%d ",ResultMatrix[i][j] );
             }
             else
             {
               printf("%d ",ResultMatrix[i][j]);
             }

             if (j == ColSize - 1) { printf("\n\t"); }

            }
          }
        }

       printf("\n\n\t Press Enter key to exit....");
       getch();
 }

Output1:

Output2 :


For C# Program :

C# PROGRAM FOR PRODUCT OF MATRICES USING ARRAYS





C PROGRAM TO PERFORM SUBTRACTION ON MATRICES


C PROGRAM TO PERFORM SUBTRACTION ON TWO MATRICES


#include<stdio.h>
#include<conio.h>
void main()
{
  //using 2D-ARRAYS
  int RowSize,ColSize;
  int Matrix1[5][5];
  int Matrix2[5][5];
  int ResultMatrix[5][5];
  int i, j;
  printf("\n >>> PROGRAM To PERFORM SUBTRACTION ON TWO MATRICES <<<\n");
  printf("\n Enter the Size of a Matrix(For Example:3 3) : ");
  scanf("%d %d",&RowSize,&ColSize);

  if (RowSize > 5 || ColSize > 5)   //limiting the size of matrix
  {
    printf(" The Size Of Matrix should Be in Less Than 5 (limiting size of array)");
    printf("\n\n\t Press Enter key to exit....");
    getch();
    return;
   }

   else
   {
     //Initializing all the elements to zero
     for (i = 0; i < RowSize; i++)
     {
        for (j = 0; j < ColSize; j++)
        {
            Matrix1[i][j] = 0;
            Matrix2[i][j] = 0;
         }
      }
      //Reading elements of Matrix1
      printf("\n Enter the elements of Matrix1(%d * %d) \n", RowSize, ColSize);
      for (i = 0; i < RowSize; i++)
      {
        for (j = 0; j < ColSize; j++)
        {
           printf(" Matrix1[%d][%d] : ", i, j);
           scanf("%d",&Matrix1[i][j]);
        }
       }

       //Reading elements of Matrix2
       printf("\n Enter the elements of Matrix2(%d * %d) \n", RowSize, ColSize);
       for (i = 0; i < RowSize; i++)
       {
         for (j = 0; j < ColSize; j++)
         {
           printf(" Matrix2[%d][%d] : ", i, j);
           scanf("%d",&Matrix2[i][j]);
         }
        }

        //calculating ResultMatrix, by Subtracting Matrix2 from Matrix1
        for (i = 0; i < RowSize; i++)
        {
          for (j = 0; j < ColSize; j++)
          {
            ResultMatrix[i][j] = Matrix1[i][j] - Matrix2[i][j];
          }
        }

        //Printing Result Matrix
        printf("\n\n\t*** Result Matrix  ***\n\n\t");
        for (i = 0; i < RowSize; i++)
        {
           for (j = 0; j < ColSize; j++)
           {
             if (ResultMatrix[i][j] > 0 &&  ResultMatrix[i][j] < 10)
             {
               //prinnting number as 01,02,etc,.
                printf("0%d ",ResultMatrix[i][j] );
             }
             else
             {
               printf("%d ",ResultMatrix[i][j]);
             }

             if (j == ColSize - 1) { printf("\n\t"); }

            }
          }
        }

       printf("\n\n\t Press Enter key to exit....");
       getch();
 }

Output1 :
                    
Output2: 


For C# program :

C# PROGRAM TO PERFORM SUBTRACTION ON MATRICES USING ARRAYS