Showing posts with label Detect. Show all posts
Showing posts with label Detect. Show all posts

C Program to Detection of a Prime Number

How to write a C Program to Detection of a Prime Number in C Programming Language ?

Write a program to determine whether a number is prime or not. 

Note :A prime number is one, which is divisible only by 1 or itself. 

  1. #include<stdio.h>
  2. main()
  3. {
  4. int num, i;
  5.  
  6. printf ("Enter a number:");
  7. scanf ("%d", &num);
  8.  
  9. i=2;
  10. while (i<=num - 1)
  11.  
  12. {
  13. if (num % i ==0)
  14.  
  15. {
  16. printf ("Not a prime number");
  17. break;
  18. }
  19.  
  20. i++;
  21. }
  22.  
  23. if (== num)
  24.  
  25. printf ("Prime Number");
  26. }

C Program for error detecting code using CRC-CCITT (16bit)

How to write a C Program for error detecting code using CRC-CCITT (16bit) in C Programming Language ?


Solution:

  1. #include<stdio.h>
  2. char m[50],g[50],r[50],q[50],temp[50];
  3. void caltrans(int);
  4. void crc(int);
  5. void calram();
  6. void shiftl();
  7. int main()
  8. {
  9. int n,i=0;
  10. char ch,flag=0;
  11. printf("Enter the frame bits:");
  12. while((ch=getc(stdin))!='\n')
  13. m[i++]=ch;
  14. n=i;
  15. for(i=0;i<16;i++)
  16. m[n++]='0';
  17. m[n]='\0';
  18. printf("Message after appending 16 zeros:%s",m);
  19. for(i=0;i<=16;i++)
  20. g[i]='0';
  21. g[0]=g[4]=g[11]=g[16]='1';g[17]='\0';
  22. printf("\ngenerator:%s\n",g);
  23. crc(n);
  24. printf("\n\nquotient:%s",q);
  25. caltrans(n);
  26. printf("\ntransmitted frame:%s",m);
  27. printf("\nEnter transmitted freme:");
  28. scanf("\n%s",m);
  29. printf("CRC checking\n");
  30. crc(n);
  31. printf("\n\nlast remainder:%s",r);
  32. for(i=0;i<16;i++)
  33. if(r[i]!='0')
  34. flag=1;
  35. else
  36. continue;
  37. if(flag==1)
  38. printf("Error during transmission");
  39. else
  40. printf("\n\nReceived freme is correct");
  41. }
  42. void crc(int n)
  43. {
  44. int i,j;
  45. for(i=0;i<n;i++)
  46. temp[i]=m[i];
  47. for(i=0;i<16;i++)
  48. r[i]=m[i];
  49. printf("\nintermediate remainder\n");
  50. for(i=0;i<n-16;i++)
  51. {
  52. if(r[0]=='1')
  53. {
  54. q[i]='1';
  55. calram();
  56. }
  57. else
  58. {
  59. q[i]='0';
  60. shiftl();
  61. }
  62. r[16]=m[17+i];
  63. r[17]='\0';
  64. printf("\nremainder %d:%s",i+1,r);
  65. for(j=0;j<=17;j++)
  66. temp[j]=r[j];
  67. }
  68. q[n-16]='\0';
  69. }
  70. void calram()
  71. {
  72. int i,j;
  73. for(i=1;i<=16;i++)
  74. r[i-1]=((int)temp[i]-48)^((int)g[i]-48)+48;
  75. }
  76. void shiftl()
  77. {
  78. int i;
  79. for(i=1;i<=16;i++)
  80. r[i-1]=r[i];
  81. }
  82. void caltrans(int n)
  83. {
  84. int i,k=0;
  85. for(i=n-16;i<n;i++)
  86. m[i]=((int)m[i]-48)^((int)r[k++]-48)+48;
  87. m[i]='\0';
  88. }

OUTPUT:-

[root@localhost nwcn]# vi 1.c
[root@localhost nwcn]# cc 1.c
[root@localhost nwcn]# ./a.out
Enter the binary data
1011
The msg before adding checksum:
10110000000000000000
The checksum calculated:
1011000101101011
The code word is:10111011000101101011
Enter the transmitted code word
10111011000101101011
Received msg:10111011000101101011
The checksum is:0000000000000000
No error in the msg
[root@localhost nwcn]# cc 1.c
[root@localhost nwcn]# ./a.out
Enter the binary data
1101
The msg before adding checksum:
11010000000000000000
The checksum calculated:
1101000110101101
The code word is:11011101000110101101
Enter the transmitted code word
10111101000110101101
Received msg:10111101000110101101
The checksum is:0110000011000110
Error in the msg

C Program Code for position detection

How to write a C Program Code for Position Detection in C Programming Language ?


Solution:

/* This is an example for the Adafruit MMA8451 Accel breakout board*/


#include <Wire.h>
#include<Time.h>
#include <Adafruit_MMA8451.h>
#include <Adafruit_Sensor.h>

Adafruit_MMA8451 mma = Adafruit_MMA8451();
double x=0,y=0,z=0,ux=0,uy=0,uz=0,ax=0,ay=0,az=0;

void setup(void) {
  Serial.begin(9600);
 
  Serial.print("Adafruit MMA8451 test!");
 

  if (! mma.begin()) {
    Serial.print("Couldnt start");
    while (1);
  }
  Serial.print("MMA8451 found!");
 
  mma.setRange(MMA8451_RANGE_2_G);
 
  Serial.print("Range = "); Serial.print(2 << mma.getRange());
  Serial.print("G");
 
}

void loop() {
  // Read the 'raw' data in 14-bit counts
 
  mma.read();
// Serial.print("X:\t"); Serial.print(mma.x);
  //Serial.print("\tY:\t"); Serial.print(mma.y);
  //Serial.print("\tZ:\t"); Serial.print(mma.z);
  //Serial.println();

  /* Get a new sensor event */
  sensors_event_t event;
  mma.getEvent(&event);

  /* Display the results (acceleration is measured in m/s^2) */
  x=((ux+ ((ax)*(second()-1)))*(second()-1) + 0.5 * (ax) * ((second()-1) * (second()-1)));
  y=((uy+ ((ay)*(second()-1)))*(second()-1) + 0.5 * (ay) * ((second()-1) * (second()-1)));
  z=((uz+ ((az)*(second()-1)))*(second()-1) + 0.5 * (az) * ((second()-1) * (second()-1)));

  ux=(ux+ ((ax)*(second()-1)));
  uy=(uy+ ((ay)*(second()-1)));
  uz=(uz+ ((az)*(second()-1)));

  ax=event.acceleration.x;
  ay=event.acceleration.y;
  az=event.acceleration.z;

 
  Serial.print(x); Serial.print(",");
  Serial.print(y); Serial.print(",");
  Serial.print(z); Serial.print(",");

//  Serial.print(event.acceleration.y); Serial.print(".");
//  Serial.print(event.acceleration.z); Serial.print(".");
  Serial.print(second()); Serial.print("#");
 

 
  /* Get the orientation of the sensor */
  uint8_t o = mma.getOrientation();
 
  switch (o) {
    case MMA8451_PL_PUF:
    //  Serial.println("Portrait Up Front");
      break;
    case MMA8451_PL_PUB:
      //Serial.println("Portrait Up Back");
      break;  
    case MMA8451_PL_PDF:
   //   Serial.println("Portrait Down Front");
      break;
    case MMA8451_PL_PDB:
     // Serial.println("Portrait Down Back");
      break;
    case MMA8451_PL_LRF:
     // Serial.println("Landscape Right Front");
      break;
    case MMA8451_PL_LRB:
      //Serial.println("Landscape Right Back");
      break;
    case MMA8451_PL_LLF:
      //Serial.println("Landscape Left Front");
      break;
    case MMA8451_PL_LLB:
      //Serial.println("Landscape Left Back");
      break;
    }
  Serial.println();
  delay(1000);
 
}

C Program to Detect int Underflow-Overflow using if-else

How to write a C Program to detect integer overflow-underflow in C Programming Language ?


Solution:
/*Program detect overflow-underflow in C*?
#include<stdio.h>
int main()
{
    unsigned long long int a,b,i;
    while(scanf("%llu",&a)!=EOF)
    {
        if(a<8)
        {
            printf("Underflow!\n");
        }
        else if(a<14)
        {
        b=1;
        for(i=1;i<=a;i++)
        {
            b=b*i;

        }
                       printf("%llu\n",b);
        }
        else
        {
            printf("Overflow!\n");
        }


    }
    return 0;
}