Tuesday, March 28, 2017

Arduino and C programming

What is Arduino?

Arduino is an open-source electronics platform. It’s very easy-to-use combination of hardware and software. Arduino circuit boards are able to read the inputs from various sensors. Output of the board can be used for activating a motor, turning LED on/off and publishing something on network.

For more details about Arduino visit: https://www.arduino.cc


Arduino and C programming

Arduino programs can be divided in three main parts: structure, values (variables and constants), and functions. Most of the syntax of structure, variable/constants and functions is very similar to syntax of C programming language. Study few sample syntax of Arduino given below which is very similar to C syntax:

Control Structures

if, if...else, for, switch case, while, do... while, break, continue, return, goto

Arithmetic Operators

= (assignment operator), +  (addition), - (subtraction), * (multiplication), / (division), % (modulo)

Comparison Operators

== (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to)

Boolean Operators

&& (and), || (or), ! (not)

Compound Operators

++ (increment), -- (decrement), += (compound addition), -= (compound subtraction), *= (compound multiplication), /= (compound division)

Data Types

void, boolean, char, unsigned char, byte, int, unsigned int, long, float, double

Group Activity by Students...

Group Activity by Students


Saturday, March 25, 2017

C program to append data to text file.

This C program will add message entered by user into the data.txt file. Note that, present content will remain as it is, and new content will be appended at the bottom of data.

#include <stdio.h>
#include <string.h>
int main()
{
   FILE *fp;
   char str[80];
 
   fp = fopen("data.txt", "a");
 
   printf("Enter your message:");
   gets(str);
 
   fprintf(fp, "%s",str);
 
   printf("Your message is appended in data.txt file.");
   fclose(fp);
   //File validation is to be added..
   return 0;
}


Output of program

Enter your message:How are you?
Your message is appended in data.txt file.

Original content of data.txt file is: Hello student.

After execution of program, content of data.txt file: Hello student. How are you?

Friday, March 24, 2017

Program to read and print test.txt file.

This C program will read content from a test.txt file and print it on a screen.

#include<stdio.h>
int main(){

 FILE *fp, *fopen();
 char str1[80], ch;

 fp = fopen("test.txt","r");
 ch = fgetc(fp);

 while(ch != EOF)
 {
  printf("%c",ch);  
  ch=fgetc(fp);
 }
 fclose(fp);
 return 0;
}

Output of the program

Hello Student,
This content is from test.txt file.

Thursday, March 23, 2017

Reliance Jio DTH Offer

Dear Readers,

Reliance JIO is one of the excellent product in demand and use today. Most of the people in India are now talking about Reliance JIO. It has been observed that, now people are eagerly waiting for next product from JIO i.e. 

Reliance Jio DTH

Based on past experience of Reliance Jio Sim Welcome offer, this time all are waiting for 

Reliance Jio DTH Welcome offer

One of popular media posted news about Reliance Jio DTH STB. As per news posted, the service is expected to be launched in March or April 2017. Read more details at 
http://telecom.economictimes.indiatimes.com/news/leaked-picture-shows-reliance-jios-round-set-top-box-report/56931718

Keep watching more interesting news at Important News page of the cprogrampracticals.blogspot.com blog.
* * * * *

Monday, March 13, 2017

Check Prime number using C function.

A prime number is a positive integer which is divisible only by 1 and itself. For example: 2, 3, 5, 7, 11, 13


#include <stdio.h>
int checkPrime(int number);

int main()
{
    int n, flag;

    printf("Enter a positive integer: ");
    scanf("%d",&n);
if(n <= 0)
{
printf("Enter positive number only.");
exit(0);
}
//Function call...
flag = checkPrime(n);    

    if (flag==1)
        printf("%d is a prime number.",n);
    else
        printf("%d is not a prime number.",n);
    return 0;
}
//Function to check prime number int checkPrime(int number)
{
int i, flag=1;
for(i=2; i<=number/2; ++i)
    {
        //checks nonprime number
        if(number%i==0)
        {
            flag=0;
            break;
        }
    }
return(flag);
}


Output of program

Run-1

Enter a positive integer: 13
13 is a prime number.

Run-2

Enter a positive integer: 10
10 is not a prime number.

Run-3

Enter a positive integer: -5
Enter positive number only.


C program to check prime number

A prime number is a positive integer which is divisible only by 1 and itself. Followings are examples of prime numbers: 2, 3, 5, 7, 11, 13,17...


#include <stdio.h>
int main()
{
    int n, i, flag = 0;

    printf("Enter a positive integer: ");
    scanf("%d",&n);
if(n <= 0)
{
printf("Enter positive number only.");
exit(0);
}
    for(i=2; i<=n/2; ++i)
    {
        //checks nonprime number
        if(n%i==0)
        {
            flag=1;
            break;
        }
    }

    if (flag==0)
        printf("%d is a prime number.",n);
    else
        printf("%d is not a prime number.",n);
    return 0;
}


Output of program

Run-1

Enter a positive integer: 13
13 is a prime number.

Run-2

Enter a positive integer: 10
10 is not a prime number.

Run-3

Enter a positive integer: -5
Enter positive number only.


Saturday, March 11, 2017

What is Socket?

Introduction to Socket Programming

  • A Socket is used for communication between two devices.
  • Socket can be defined using IP Address and Port number.
  • Socket programming is possible using C as well as Java.
  • Socket connection is supported by all platforms/OS. (Windows, Unix, Mac, etc.).
  • It is used in a client server application over network.

Sample Applications based on Socket Programming:

  • Echo Client-Server Socket Application
  • Chat Application
  • Math Client-Server Socket Application
  • Simulation of ARP using Socket
  • IP Class Finder using Socket


Sunday, March 5, 2017

Difference between Structure and Union

Structure: structure in C language is a collection of variables with a single name. These variables can be of different types. 

Union: It is a special data type in C which allows us to store different data types in the same memory location.


 Structure

 Union
struct keyword is used to define Structure in C.

union keyword is used to define Union in C. 
Members of structure do not share memory. 

Members of union shares the memory. 

 Members of structure can be accessed at any time individually.

At a time only one member of union can be accessed.
Syntax:

struct structure_name
{
  datatype var1;
  datatype var2;
  ---
  ---
  datatype varn;

}struct_variable_name;
Syntax:

union union_name
{
  datatype var1;
  datatype var2;  ---
  ---
  datatype varn;

}union_variable_name; 


Friday, March 3, 2017

Flowchart - Advantages and Disadvantages

Some of the important advantages and disadvantages related to the Flowchart are given below:

Flowchart Advantages
  • Flowcharts are easier to understand compare to Algorithms and Pseudo code.
  • It helps us to understand Logic of given problem.
  • It is very easy to draw flowchart in any word processing software like MS Word.
  • Using only very few symbol, complex problem can be represented in flowchart.
  • Software like RAPTOR can be used to check correctness of flowchart drawn in computers.
  • Flowcharts are one of the good way of documenting programs.
  • It helps us in debugging process. 
Flowchart Disadvantages
  • Manual tracing is needed to check correctness of flowchart drawn on paper.
  • Simple modification in problem logic may leads to complete redraw of flowchart.
  • Showing many branches and looping in flowchart is difficult.
  • In case of complex program/algorithm, flowchart becomes very complex and clumsy.
  • Modification of flowchart is sometimes time consuming.


You may like to visit following top 10 Flowchart Examples:





Thursday, March 2, 2017

AES Encryption Decryption

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.KeyGenerator;

public class AESEncryptionDecryption {
   
    public static void main(String[] args) {
      try{
          Cipher cipher = Cipher.getInstance("AES");
          KeyGenerator kg = KeyGenerator.getInstance("AES");
          Key key = kg.generateKey();

          cipher.init(Cipher.ENCRYPT_MODE, key);
          CipherInputStream cipt=new CipherInputStream(new FileInputStream(new File("D:\\PlainTextInput.txt")), cipher);
          FileOutputStream fip=new FileOutputStream(new File("D:\\EncryptedText.txt"));

          int i;
          while((i=cipt.read())!=-1)
          {
             fip.write(i);
          }

          cipher.init(Cipher.DECRYPT_MODE, key);
          CipherInputStream ciptt=new CipherInputStream(new FileInputStream(new File("D:\\EncryptedText.txt")), cipher);
          FileOutputStream fop=new FileOutputStream(new File("D:\\DecryptedText.txt"));

          int j;
          while((j=ciptt.read())!=-1)
          {
             fop.write(j);
          }

        }
        catch(Exception e) {
           e.printStackTrace();
        }
      System.out.println("Encryption and Decryption of plain text file performed successfully.");
    }
}

Output of the program:

Encryption and Decryption of plain text file performed successfully.

Content of PlainTextInput.txt file: HelloStudent

Content of EncryptedText.txt file: ùóà ¬x9f¯—©c aá

Content of DecryptedText.txt file: HelloStudent 

Note: You need to create PlainTextInput.txt file in D:\ drive of computer. Execution of program will create EncryptedText.txt and DecryptedText.txt file in D:\ drive.

Array of Pointers

To store many pointer variables, one can use array of pointers.

#include <stdio.h>
int main () {

   int  number[5], i, *ptr[5];

   for(i=0; i<5; i++)
   {
     printf("Enter number[%d]:",i);
     scanf("%d",&number[i]);
   }
   /* Loop to assign the address of integer into pointer array */
   for ( i = 0; i < 5; i++) {
      ptr[i] = &number[i]; 
   }
   printf("Value printed using Pointer array.\n");
   for ( i = 0; i < 5; i++) {
      printf("Value of number[%d] = %d\n", i, *ptr[i] );
   }
   return 0;
}

Output of program:

Enter number[0]:2
Enter number[1]:4
Enter number[2]:6
Enter number[3]:8
Enter number[4]:10
Value printed using Pointer array.
Value of number[0] = 2
Value of number[1] = 4
Value of number[2] = 6
Value of number[3] = 8
Value of number[4] = 10

Wednesday, March 1, 2017

Dynamic memory allocation

Dynamic memory allocation means, programmer can manually handle memory space during runtime of programMalloc( ) function allocates requested size of bytes and returns a pointer to first byte of allocated memory.

Following program will demonstrate use of malloc() function for allocating memory at run time.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int *ptr;

    //Following statement allocates 2 int size memory space
    ptr = (int*)malloc(2*sizeof(int));  
    
    if(ptr == NULL)                     
    {
        printf("Error during memory allocation.");
        exit(0);
    }

    printf("Enter your number1: ");
    scanf("%d", ptr);
    
    printf("Enter your number2: ");
    scanf("%d", ptr+1);
   
    printf("Your number1 = %d\n", *ptr);
    printf("Your number2 = %d", *(ptr+1));
    
    free(ptr); //allotted memory will be cleared.

    return 0;
}

Output of the program:

Enter your number1: 101
Enter your number2: 202
Your number1 = 101
Your number2 = 202


* * * * *