New Android apps UniqueKey

Showing posts with label C language. Show all posts
Showing posts with label C language. Show all posts

Friday, 15 May 2015

Prime numbers between 2 numbers


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

main()
{
 int n1,n2,i,j,flag;
 
 printf("Enter 2 numbers\n");
 scanf("%d %d",&n1,&n2);
 
 for(i=n1+1 ; i<n2 ; i++)
 {
  flag = 0;
  
  for(j=2 ; j<=sqrt(i) ; j++)
  {
   if(i%j==0){
    flag=1;
    break;
   }
  }
  if(flag==0)
  printf("%d\t",i);
 }
}

Tuesday, 12 May 2015

Double Linked List Program

This is a program to show how double linked list works.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
 
struct node
{
     struct node *previous;
     int data;
     struct node *next;
}*head, *last; 

void insert_begning(int value);
int delete_from_end();
void display();

int main()
{
    int value, i, loc;
    head=NULL;
    printf("Select the choice of operation on link list");
    printf("\n1.) insert at begning\n2.)delete from end \n3.) display\n4.exit\n");

    while(1)
    {
          printf("\n\nenter the choice\n");
          scanf("%d",&i);
          switch(i)
          {
            case 1:
                {
                 printf("enter the value you want to insert in node ");
                 scanf("%d",&value);
                 insert_begning(value);
                 display();
                 break;
                }
            case 2:
                   delete_from_end();
                   display();
                   break;
            case 3 :
                   display();
                   break;
            case 4 :
                 {
                      exit(0);
                      break;
                 }
          }
    }
    printf("\n\n%d",last->data);
    display();
    getch();
}

void insert_begning(int value)
{
    struct node *var,*temp;
     var=(struct node *)malloc(sizeof(struct node));
     var->data=value;
     if(head==NULL)
     {
         head=var;
         head->previous=NULL;
         head->next=NULL;
         last=head;
     }
     else
     {
         temp=var;
         temp->previous=NULL;
         temp->next=head;
         head->previous=temp;
         head=temp;
     }
} 

int delete_from_end()
{
      struct node *temp;
      temp=last;
      if(temp->previous==NULL)
      {
           free(temp);
           head=NULL;
           last=NULL;
           return 0;
      }
      printf("\nData deleted from list is %d \n",last->data);
      last=temp->previous;
      last->next=NULL;
      free(temp);
      return 0;
} 

void display()
{
     struct node *temp;
     temp=head;
     if(temp==NULL)
      {
         printf("List is Empty");
      }
     while(temp!=NULL)
     {
          printf("-> %d ",temp->data);
          temp=temp->next;
     }
} 

Saturday, 21 March 2015

Simple Linked list Program

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

struct node
{
 int data;
 struct node *link;                 //to creating self reference pointer
};

int display(struct node *,int);
void insertion(struct node *,int);
void deletion(struct node *,int);

struct node *head;
int count=0;

main() 
{
   int i,choice,inst=0,del,chk1=1,chk=1,num;
  
   do
   {
   printf("\nMENU:\n\n 1.Insertion\n 2.Deletion \n 3.Display\n 4.Exit\n\nenter your choice :     ");
   scanf("%d",&choice);
   
   switch(choice)
   {
  case 1:
          if(count==0)
          { 
           printf("\nThis is First Node\n"); 
          head=(struct node *)malloc(sizeof(struct node));
           insertion(head,inst);
          }
         else
         {
           printf("Where do you want to insert data\n 1. In starting \n2. End\n");
           scanf("%d",&inst);
           insertion(head,inst);
         }   
         break;
         
  case 2:
          printf("from where do you want to delete\n 1. In starting 2. End\n");
          scanf("%d",&del);
          deletion(head,del);
          break;
          
  case 3:
         chk1=display(head,count);
         if(chk1==1)
         printf("NULL\n");
         break;
         
  case 4:
          chk=0;
          break;
     
    default : printf("invalid option selected\ntry again\n\n");        
   }
 }
 while(choice!=4);
  }
  
void insertion(struct node *list,int num)
{ 
  struct node *new1;
  int num1;
  
  printf("Enter the number\n");
  scanf("%d",&num1);
  
  if(count==0&&num==0)
  {
    head->data=num1;
 head->link=NULL;
  }
  else if(num==1&&count>0)
  {
 new1=(struct node *)malloc(sizeof(struct node));
 new1->data=num1;
 new1->link=head;
 head=new1;
  }
  else if(num==2&&count>0)
  {
 for( ; ;)
 {
   if(list->link!=NULL)
      list=list->link;
   else
  {
    new1=(struct node *)malloc(sizeof(struct node));
    new1->data=num1;
    list->link=new1;
    new1->link=NULL;
    break;
  }
 }
  }
  
 count++;
 
}

void deletion(struct node *list,int num)
{
  struct node *temp;
  
  if(num==1&&count>0)
 {
   head=list->link;
   free(list);
 }
  else if(num==2&&count>1)
 {
   for( ; ;)
    {
  if(list->link->link!=NULL)
     list=list->link;
  else
   {
     free(list->link);
     list->link=NULL;
   break;
    }
   }
 }
 else if(count==1)
   free(head);
 else if(count<=0)
 {
    printf("List is empty\n\n"); 
    count--; 
 }
 count--;
 
}

int display(struct node *list,int num)
{ 
  int chk=1;
  
  if(num>0)
   {
  printf("%d->",list->data);
  num--;
  display(list->link,num);
   }
  else if(count<=0)
 {
   chk=0;
   printf("list is empty\n\n");
 }
 return(chk);
}

Saturday, 7 March 2015

Armstrong number


/*check number is Armstrong number or not*/

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

main()
{
  int num,i,j,temp,sum=0,dig,n;

  printf("enter the number to check\n");
  scanf("%d",&num);

  for(temp=num;temp>0;)
  {
     dig=temp%10;
     sum=sum+(dig*dig*dig);
     temp=temp/10;
  }
  if(num==sum)
  {
   printf("number is a armstrong number\n");
  }
  else
   printf("number is not a armstrong number\n");

}

Print all the characters using ASCII value.


/*print alphabet*/

#include<stdio.h>

main()
{
  int n[122],i,j,num;
   
  for(i=0;i<=121;i++)
  {
    n[i]='0'+i;
    printf("%d  %c\t",n[i],n[i]);
    
  }
}

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

Sunday, 22 February 2015

Removing white space from a string.


#include<stdio.h>

main()
{
    char str[150];
    int i,j;
    
    printf("Enter a string: ");
    gets(str);
    
    for(i=0; str[i]!='\0'; i++)
    {
        while (!((str[i]>='a'&&str[i]<='z') || (str[i]>='A'&&str[i]<='Z' || str[i]=='\0')))
        {
            for(j=i;str[j]!='\0';++j)
            {
                str[j]=str[j+1];
            }
            str[j]='\0';
        }
    }
    printf("Output String: ");
    puts(str);
}

Find no. of vowels , consonants , digits and white spaces in a string.


#include<stdio.h>

main()
{
    char str[150];
    int i,v,c,ch,d,s,o;
    
    o=v=c=ch=d=s=0;
    
    printf("Enter a string:\n");
    gets(str);
    
    for(i=0;str[i]!='\0';i++)
    {
        if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u' || str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]=='O' || str[i]=='U')
            ++v;
        else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
            ++c;
        else if(str[i]>='0'&&c<='9')
            ++d;
        else if (str[i]==' ')
            ++s;
    }
    printf("Vowels: %d",v);
    printf("\nConsonants: %d",c);
    printf("\nDigits: %d",d);
    printf("\nWhite spaces: %d",s);
}

No. of particular character in a string.


#include <stdio.h>

main()
{
   char c[100],ch;
   int i,count=0;
   
   printf("Enter a string: ");
   gets(c);
   printf("Enter a character to find frequency: ");
   scanf("%c",&ch);
   
   for(i=0;c[i]!='\0';i++)
   {
       if(ch==c[i])
           count++;
   }
   printf("Frequency of %c = %d", ch, count);
}

Prime numbers b/w given interval


/* A C program to display all prime numbers between given interval. */

#include <stdio.h>

main()
{
  int n1, n2, i, j, c;
  
  printf("Enter two numbers :   ");
  
  scanf("%d %d", &n1, &n2);
  printf("Prime numbers between %d and %d are: ", n1, n2);
  for(i=n1+1;i<n2;i++)
  {
      c=0;
      for(j=2;j<=i/2;j++)
      {
        if(i%j==0)
        {
          c=1;
          break;
        }
      }
      if(c==0)
        printf("%d ",i);
  }
}

Prime Number or not


/* A C program to check whether a number is prime or not. */

#include<stdio.h>

main()
{
  int n, i, c=0;
  
  printf("Enter the number to check whether this number is prime or not :  ");
  scanf("%d",&n);
  
  for(i=2;i<=n/2;i++)
  {
      if(n%i==0)
      {
          c=1;
          break;
      }
  }
  if (c==0)
      printf("%d is a prime number.",n);
  else
      printf("%d is not a prime number.",n);
}

Friday, 20 February 2015

Fibonacci series recursive


#include<stdio.h>

void fibonacci(int);

main()
{
    int k,n;
    long int i=0,j=1,f;

    printf("Enter the range of the Fibonacci series: ");
    scanf("%d",&n);

    printf("Fibonacci Series: ");
    printf("%d %d ",0,1);
    fibonacci(n);
}

void fibonacci(int n)
{

   static long int first=0,second=1,sum;

    if(n>0)
 {
         sum = first + second;
         first = second;
         second = sum;
         printf("%ld ",sum);
         fibonacci(n-1);
    }

}

Fibonacci Series


#include<stdio.h>

main()
{
    int k,r;
    int i=1,j=1,f;

    printf("Enter the number range :  ");
    scanf("%d",&r);

    printf("fibonacci series :\n");
    printf("%d\n%d\n",i,j);

    for(k=2;k<r;k++)
 {
         f=i+j;
         i=j;
         j=f;
         printf("%d\n",j);
    }
}

Reverse string


#include<stdio.h>

char* reverse(char[]);

int main()
{

    char str[100],*rev;

    printf("Enter  any string: ");
    scanf("%s",str);

    rev = reverse(str);

    printf("Reversed string is: %s",rev);
}

char* reverse(char str[])
{

    static int i=0;
    static char rev[100];

    if(*str)
 {
         reverse(str+1);
         rev[i++] = *str;
    }

    return rev;
}

Reverse the string without library function


#include<stdio.h>
#include<string.h>

main()
{
 char str[50];
 int i,j,len,temp;
 
 printf("enter the string\n");
 gets(str);
 
 len=strlen(str);
 
 for(i=0,j=len-1;i<=len/2;i++,j--)
 {
  temp=str[i];
  str[i]=str[j];
  str[j]=temp;
 }
 
 printf("reversed string is : %s\n",str);
 
}

Reverse the string


#include<stdio.h>
#include<string.h>

main()
{
 char str[50];
 
 printf("enter the string\n");
 gets(str);
 
 strrev(str);
 
 printf("reversed string is : %s\n",str);
 
}