New Android apps UniqueKey

Saturday, 7 March 2015

Merge 2 arrays and then sort this array.


/*a c program to sort the merg of array*/

#include<stdio.h>

main()
{
  float num1[100],num2[100],num[300];                              //array declaration
  int n1,n2,i,j,t;
  
  printf("\nenter the number of elements in 1st array\n");
  scanf("%d",&n1);                                                 //get the number of 1st array element

  printf("\nenter first array in ascending order\n");
  for(i=0;i<n1;i++)
  {
    scanf("%f",&num1[i]);                                          //to read the element of array 

  }
  
  printf("\nenter the number of elements in 2nd array\n");
  scanf("%d",&n2);                                                 //get the number of 2nd array element

  printf("\nenter second array in descending order\n");
  for(j=0;j<n2;j++)
  {
    scanf("%f",&num2[j]);                                          //to read the element of array
  }
  i=0;j=0;

  while(i<n1)
   {
     num[i]=num1[i];
     i++;
   }
 
  while(j<n2)
   {
      num[i]=num2[j];
     j++;
     i++;
   }

  for(i=0;i<n1+n2-1;i++)
   {
    for(j=0;j<n1+n2-i-1;j++)
    { 
      if(num[j]==num[j+1])
      j++;
      if(num[j]<num[j+1])
      {
        t=num[j];                                                  //use bubble sort to arrage array element
        num[j]=num[j+1];
        num[j+1]=t;
      }
    }
  }

  printf("\nthe sorted and merged array in decending order is :\n");
  for(i=0;i<n1+n2;i++)
  
  if(num[i]!=num[i+1])
  printf("%6.0f\n",num[i]);                                        //to print sorted and merg array in decending order
}

Computing equation using recursive function.


/*a c program to compute equation using a recursive function*/

#include<stdio.h>
#include<math.h>

long int power(int,int);               //declaration of function

main()
{
  int num,x;
  long int sum;
  
  printf("---------------------------------\n");

  printf("your equation is::\n");
  printf("4*x^5+3*x^4+2*x^3+x^2+x\n");      //to show the equation

  printf("--------------------------------\n");

  printf("enter the value of x\n");         //to enter the value of x
  scanf("%d",&x);   

  sum=4*power(x,5)+3*power(x,4)+2*power(x,3)+power(x,2)+x;         // using the function

  printf("-------------------------------\n");

  printf("result of given equation is :: %ld\n",sum);

  printf("--------------------------------\n");
}

long int power(int num1,int num2)          //definition of the function
{
  if(num2!=1)
  return(num1*power(num1,num2-1));      
}

Program to create number pyramid.


/*to make number pyramid*/

#include<stdio.h>
#include<math.h>

main()
{
   int num,t=0,s=1,j=0;
   int choice,num1,dig,sum=0;
   int n,i,pro;
 
   printf("1. FOR MAKE PYRAMID\n");
   printf("2. FOR FIND DIGITS SUM\n");
   printf("3. FOR MAKE MATHEMATICAL TABLE OF GIVEN NUMBER\n4. EXIT\n\n");
   
   do{
    
   printf("\nENTER YOUR CHOICE\n\n");
   scanf("%d",&choice);
   
   switch(choice)
  {
   case 1 : printf("enter the terms of pyramid\n");
            scanf("%d",&num);

            for(t=0;t<=num;t++)
            {
             for(j=0;j<=num-t;j++)
              {
                printf("  ");
              }
             for(s=1;s<=t;s++)
              {
                printf("   %d",s);
    
              }
             printf("   \n");     
            }
             printf("  ");
            break;
    case 2 : printf("enter the number\n");
             scanf("%d",&num1);
              
             while(num1!=0)
             {
               dig=num1%10;
               sum=sum+dig;
               num1=num1/10;
             }   
             printf("sum of indivisual digits=%d\n",sum);
             break;
    case 3 : printf("enter the number to make mathematical table\n");
             scanf("%d",&n);
             
             printf("table of number is \n\n");
            
             for(i=1;i<=10;i++)
            {
              pro=n*i;

              printf("%d\n",pro);
            }
            break;
            
    case 4 : break;
         
    default : printf("invalid option\ntry again\n");
             break;   
   }
  }while(choice!=4);
}

Find enter number pellendrom or not and find number is prime or not


/*program to find enter number pellendrom or not and find number is prime or not*/

#include<stdio.h>
#include<math.h>

main()
{
  int num,i,j,k,rev=0,n,c=0;

  printf("enter the number\n");
  scanf("%d",&num);
  n=num;
  while(num>0)
  {
   i=num%10;
   rev=rev*10+i;
   num=num/10;
  }
   
  if(rev==n)
  {
   printf("%d is pellendrom number\n\n",n);
  }
  else
  {
  printf("%d is not a pellendrom number\n",n);
  }
  for(j=2;j<=n/2;j++)
  {
    if(n%j==0)
    {
      c=1;
      break;
    }
  }
   if((c==1)||(n==1))
   printf("%d is not a prime number\n\n",n);
   else
   printf("%d is a prime number\n\n",n);
   
   


}

Check whether year is a leap year or not


/*program to check the year leap or not*/

#include<stdio.h>
#include<math.h>

main()
{
  int year;
 
  printf("enter the year which you want to check\n");
  scanf("%d",&year);

  if(year%400==0)
   {
       printf("%d is leap year\n",year);
   }  
  else
   { 
     if(year%4==0&&year%100!=0)
     {
       printf("%d is leap year\n",year);
     }
     else
     {
       printf("%d is not leap year\n",year);
     }
   }  
}