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 :