Showing posts with label programming burger. Show all posts
Showing posts with label programming burger. Show all posts

Thursday, 27 October 2016

C Program to Print A Pattern of Prime Numbers


C Program to Print A Pattern of Prime Numbers

Pattern to be printed looks like following, containing only prime numbers in regular sequence:



C program : 

#include<stdio.h>
#include<conio.h>
int nextprime(int);
int isprime(int);
void main()
{
 int i,p=1,n,j;
 clrscr();
 printf("Enter the no. of lines to be printed: ");
 scanf("%d",&n);
 if(n==0)
 printf("\nINVALID INPUT");
 printf("\n%4d\n",p);
 if(n!=1)
 {
  for(i=2;i<=n;i++)
  {
   for(j=1;j<=i;j++)
   {
     p=nextprime(p);
     printf("%4d",p);
   }
   printf("\n");
  }
 }
 printf(" \n Visit www.EngineersBurger.com for more intresting stuff ! \n -  Jay Akbari");
 getch();
}

int nextprime(int s)
{
 s=s+1;
 while(!isprime(s) )
 {
  s=s+1;
 }
 return s;
}

int isprime(int s)
{
 int i;
 for(i=2;i<s;i++)
 {
  if(s%i==0)
  return 0;
 }
 return 1;
}



Download .C file of this program : CLICK HERE
 

Saturday, 15 October 2016

C program | Use of Pointers, functions & Unusual use of Switch Case

C program | Use of Pointers, functions, Unusual use of Switch Case

 

What is this c program about? 

This program scans number of words from user in range of 1 to 10.  Then user will enter those many number of words in form of a string one by one, each word should be scanned in a new line. Only after scanning all the words from user, each word is processed. Processing of word is done as >> Letters at even places are printed first followed by 2 blank spaces and then letters at odd places of the word are printed at last for each word. 

Length of the word entered by the user should be long enough. This program can take a word of 97 characters long.
  •    sample input:
   2               (number of words scanned)
   Hello         (1st word entered by user)
   Vishal        (2nd word entered by user)
  •    output :
   Hlo  el       (output of Hello word)
   Vsa  ihl      (output of Vishal)


C program: 

 

#include<stdio.h>
#include<conio.h>
char* processing(char* );
void main()
{

 int i,n;
 char a1[100];
 char *p1,*p2,*p3,*p4,*p5,*p6,*p7,*p8,*p9,*p10;
 clrscr();

 printf("Enter no. of words, mimimum -> 1 maximum -> 10 \n ");
 scanf("%d",&n);
 printf("\n Start entering the words below, :\n");


 switch(1)
 {
  case 1:
   scanf("%s",a1);
   p1=a1;
   printf("\n");
   p1=processing(p1);
   if(n==1)
      break;

   case 2:
   scanf("%s",a1);
   p2=a1;
   printf("\n");
   p2= processing(p2);
   if(n==2)
     break;

  case 3:
   scanf("%s",a1);
   p3=a1;
   printf("\n");
   p3= processing(p3);
   if(n==3)
     break;

  case 4:
   scanf("%s",a1);
   p4=a1;
   printf("\n");
   p4= processing(p4);
   if(n==4)
     break;

  case 5:
   scanf("%s",a1);
   p5=a1;
   printf("\n");
   p5= processing(p5);
   if(n==5)
     break;

  case 6:
   scanf("%s",a1);
   p6=a1;
   printf("\n");
   p6= processing(p6);
   if(n==6)
     break;

   case 7:
   scanf("%s",a1);
   p7=a1;
   printf("\n");
   p7= processing(p7);
   if(n==7)
       break;

  case 8:
   scanf("%s",a1);
   p8=a1;
   printf("\n");
   p8= processing(p8);
   if(n==8)
     break;

  case 9:
   scanf("%s",a1);
   p9=a1;
   printf("\n");
   p9= processing(p9);
   if(n==9)
     break;

  case 10:
   scanf("%s",a1);
   p10=a1;
   printf("\n");
   p10= processing(p10);
   if(n==10)
     break;

  default: printf("Error");
 }

 switch(1)
 {
  case 1: printf("\n %s",p1);
   if(n==1)
     break;

  case 2: printf("\n %s",p2);
   if(n==2)
     break;

  case 3: printf("\n %s",p3);
   if(n==3)
     break;

  case 4: printf("\n %s",p4);
   if(n==4)
     break;

  case 5: printf("\n %s",p5);
   if(n==5)
     break;

  case 6: printf("\n %s",p6);
   if(n==6)
     break;

  case 7: printf("\n %s",p7);
   if(n==7)
     break;

  case 8: printf("\n %s",p8);
   if(n==8)
     break;

  case 9: printf("\n %s",p9);
   if(n==9)
     break;

  case 10: printf("\n %s",p10);
   if(n==10)
     break;

 }
 printf("\n For more intresting programs, visit >> www.EngineersBurger.com \n - Jay Akbari ");
 getch();
}

char* processing(char *a)
{ int i, n,j=0;
 char* temp ;
 n=strlen(a);
       // printf("\n string lenght is %d", n);
 temp= (char*) malloc(n*sizeof(char) + 3);

 for( i=0; *(a+i)!='\0'; i+=2)
 {
  *(temp+j)=*(a+i);
  j++;
  if(*(a+i+1)=='\0')
   break;
 }
 *(temp+j)=' ';
 *(temp+j+1)=' ';
 j++;

 for( i=1; *(a+i)!='\0'; i+=2)
 {
  *(temp+j)=*(a+i);
  j++;
  if(*(a+i+1)=='\0')
   break;
 }
 *(temp+j)= '\0';

 return(temp);
}

Download the .C file : CLICK HERE



Monday, 10 October 2016

HEXADECIMAL to OCTAL number system conversion in C

 

HEXADECIMAL to OCTAL number system conversion in C

Following program converts a hexadecimal number scanned from user into octal number system and prints the same. The only constraint is this program only converts hexadecimal numbers without any floating points. 

Download C program file to run on your device at the end of this post.
C Program:
 
 
 
 
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<alloc.h>

char* hextobin(char*, int);
char* bintooct(char*,int);
void convert1(char*, char*);
void convert2(char*, char*);
void convert3(char*, char*);
void processing(int,int,char*,char*);
void main()
{
 char hex[100],oct[100],binary[100];
 int n,n2;
 char*ptr;
 char*ptr2;

 clrscr();

 printf("\n Enter HEXADECIMAL NUMBER without any floating point :");
 scanf("%s",hex);
 n= strlen(hex);
 printf("\n length is %d",n);

 ptr=binary;
 ptr=hextobin(hex,n);
 n2= strlen(ptr);

 printf("\n hexadecimal %s is %s in binary.\n",hex,ptr);

 ptr2=oct;
 ptr2= bintooct(ptr,n2);
 printf("\n Binary %s is %s in octal. \n",ptr,ptr2);
 printf( " for more programs visit www.EngineersBurger.com\n -Jay Akbari ");
 getch();

}
//to convert hexadecimal to binary.
char* hextobin(char* hex, int n)
{
 int i,j,k,count=0;
 char* bin;
 char temp[5];
 bin= (char*) malloc(n*4*sizeof(char)+1);

 for(i=0; hex[i]!='\0';i++)
 {
  switch(hex[i])
  {
   case '0': strcpy(temp,"0000");
      break;

   case '1': strcpy(temp,"0001");
     break;

   case '2': strcpy(temp,"0010");
      break;

   case '3': strcpy(temp,"0011");
      break;

   case '4':strcpy(temp,"0100");
     break;

   case '5':strcpy(temp,"0101");
      break;

   case '6':strcpy(temp,"0110");
      break;

   case '7':strcpy(temp,"0111");
      break;

   case '8':strcpy(temp,"1000");
      break;

   case'9' :strcpy(temp,"1001");
      break;

   case'a' :strcpy(temp,"1010");
      break;

   case 'b' : strcpy(temp,"1011");
      break;

   case 'c': strcpy(temp,"1100");
      break;

   case 'd': strcpy(temp,"1101");
      break;

   case 'e': strcpy(temp,"1110");
      break;

   case 'f': strcpy(temp,"1111");
      break;
  }

 k=count;
 for(j=0; temp[j]!='\0';j++)
 {
  *(bin+k)=temp[j];
  k++;
 }

  count=k;

    }

    *(bin+k)='\0';
    return (bin);

}

//to convert binary to octal
char* bintooct(char*ptr,int n)
{   char*temp;

    if( (n%3)==0)
    {
 temp=(char*)malloc( n/3*sizeof(char)+1 );
 convert1(ptr,temp);
 return (temp);
    }

    else if ( (n%3)==1 )
    {
 temp=(char*) malloc(n/3*sizeof(char)+2 );
 convert2(ptr,temp);
 return (temp);
    }

    else //n%3==2
    {
 temp=(char*) malloc(n/3*sizeof(char)+2);
 convert3(ptr,temp);
 return (temp);
    }

}

void convert1(char*ptr,char*temp)
{
    int i,j;

    for(i=0,j=0; *(ptr+i)!='\0';i+=3,j++)
    {
 processing(i,j,ptr,temp);

    }
     *(temp+j)='\0';

}

void convert2(char* ptr,char* temp)
{
   int i,j;

   //checking left most binary is 1 or zero.
 if( *(ptr)=='0')       //eg:0111
  *(temp)='0';
 else
  *(temp)='1';  //eg:1111

   for(i=1,j=1; *(ptr+i)!= '\0';i+=3,j++)
    {

 processing(i,j,ptr,temp);

     }
     *(temp+j)='\0';
}

void convert3(char*ptr, char* temp)
{

   int i,j;

   //checkingthe first two on left most binary is 1 or zero.
 if( *(ptr)=='0'&& *(ptr+1)=='0')       //eg:00 111 111
  *(temp)='0';

 else if (*(ptr)=='0' && *(ptr+1)=='1')
  *(temp)='1';  //eg:01 111 111

 else if( *ptr=='1' && *(ptr+1)=='0' )
  *(temp) ='2';
 else //star of ptr and ptr+1 both are 1
  *(temp)='3';

   for(i=2,j=1; *(ptr+i)!= '\0';i+=3,j++)
    {

  processing(i,j,ptr,temp);

    }
     *(temp+j)='\0';

}
//this function is block of code which is common to above 3 functions.
void processing(int i,int j,char* ptr,char* temp)
{
 if ( *(ptr+i)=='0' && *(ptr+i+1)=='0' && *(ptr+i+2)=='0')
  *(temp+j)='0';

       else if ( *(ptr+i)=='0' && *(ptr+i+1)=='0' && *(ptr+i+2)=='1')
  *(temp+j)='1';

 else if ( *(ptr+i)=='0' && *(ptr+i+1)=='1' && *(ptr+i+2)=='0')
  *(temp+j)='2';

 else if ( *(ptr+i)=='0' && *(ptr+i+1)=='1' && *(ptr+i+2)=='1')
  *(temp+j)='3';

 else if ( *(ptr+i)=='1' && *(ptr+i+1)=='0' && *(ptr+i+2)=='0')
  *(temp+j)='4';

 else if ( *(ptr+i)=='1' && *(ptr+i+1)=='0' && *(ptr+i+2)=='1')
  *(temp+j)='5';

 else if ( *(ptr+i)=='1' && *(ptr+i+1)=='1' && *(ptr+i+2)=='0')
  *(temp+j)='6';

 else // ( *(ptr+i)=='1' && *(ptr+i+1)=='1' && *(ptr+i+2)=='1')
  *(temp+j)='7';
}

Download C program file: CLICK HERE

Have A Wonderful Day !

--------------------------------------------------------------------

Word Of The Day | Learn And Use A New Word Everyday!

--------------------------------------------------------------------

"Insidious"

Working or spreading in a hidden and usually injurious way

"glaucoma is an insidious disease"

Copyright © 2016 Engineers Burger - All Rights Reserved. Digital Craftmanship - Nikhil Jha and Jay Akbari.
MyBloggerLab.com