Pages

To Perform Addition, Subtraction, Multiplication and Division of Two No.

Thursday 10 January 2013
Write a Program to perform addition, subtraction, multiplication and division of Two Numbers.

#include <stdio.h>

int main()
{
   int first, second;
   int addition, subtract, multiply;
   float divide;

   printf("Enter two integers\n");
   scanf("%d%d", &first, &second);

   addition = first + second;
   subtract = first - second;
   multiply = first * second;
   divide   = first / (float)second; 

   printf("Sum = %d\n", addition);
   printf("Difference = %d\n", subtract);
   printf("Multiplication = %d\n", multiply);
   printf("Division = %.3f\n", divide);
   return 0;
}
Read more ...

Add n Numbers

Thursday 10 January 2013
Write a Program Add n Numbers Without using an array.

#include <stdio.h>

int main()                                           
{
               int n;
               int sum = 0;
               int c, value;

               printf ("Enter the number of integers you want to add\n");
               scanf ("%d", &n);
               printf ("Enter %d integers\n",n);

               for (c = 1; c <= n; c++)
               {
                               scanf ("%d",&value);
                               sum = sum + value;
               }
               printf ("Sum of entered integers = %d\n",sum);
               return 0;
}
Read more ...

Read and Write Character Until End Of File

Thursday 10 January 2013
Write a Program Read and Write Character Until End of File.

#include <stdio.h>

int main(void)
{
            int a;
            for ( ; ; )
            {
                        a = getchar();
                        if (a == EOF)
                        break;
                        putchar(a);
            }
            return 0;
}

Read more ...

To Print The Reverse Number

Monday 7 January 2013
Write a program the outputs of number with the digits reversed.

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

int main()
{
    int intnumber;                           /* can try long for bigger range, int range is the limit*/
    int condition, remainder;
    int counter = 0, i = 0, j = 0;      /* counter to store the number of digit entered by user*/
    int counter1 = 0;                      /* counter1 is similar, used as for loop sentinel*/
    int reverseint[20];

    printf("Enter an integer number: ");
    scanf_s("%d", &intnumber);               /* read and store input in intnumber*/

    condition = intnumber;                       /* set the condition sentinel value to intnumber*/
    while (condition > 0)
    {
        condition = condition /10;
        counter = counter + 1;
            counter1 = counter1 + 1;         /* this counter for printing in reverse*/
    }
    counter = counter - 1;
    printf("The number in reverse: ");
    while (counter >= 0)
    {
        /* extract each of the decimal digits, need to cast to int*/
        /* to discard the fraction part*/
        /* pow(10, counter) used to determine the ...,10000, 1000, 100, 10, 1*/

        remainder = intnumber % (int) pow(10, counter);
        intnumber = intnumber/(int) pow(10,counter);
        reverseint[i] = intnumber;
        i++;
        intnumber = remainder;           /* update and repeat for the rest*/

        counter = counter - 1;
    }

    /* print the array element in reverse*/
  
    for (j=counter1-1; j >= 0; j--)
    printf("%d ", reverseint[j]);
    printf("\n");

    return 0;
}
Read more ...

Swap two no using pointers

Monday 7 January 2013

 Swap two numbers using pointers

 
#include <stdio.h>

int main()
{
   int x, y, *a, *b;
   int temp;

   printf ("Enter the value of x and y\n");
   scanf ("%d%d", &x, &y);
   printf ("Before Swapping\n x = %d\n y = %d\n", x, y);

   a = &x;
   b = &y;

   temp = *b;
   *b = *a;
   *a = temp;

   printf("After Swapping\n x = %d\n y = %d\n", x, y);
   return 0;
}
Read more ...

Swapping of two numbers in C

Monday 7 January 2013

Swapping of two numbers

#include <stdio.h> int main() { int x, y; int temp; printf ("Enter the value of x and y\n"); scanf ("%d%d", &x, &y); printf ("Before Swapping\n x = %d\n y = %d\n",x,y); temp = x; x = y; y = temp; printf ("After Swapping\n x = %d\n y = %d\n",x,y); return 0; }
Read more ...

Find Out The Largest Value

Monday 7 January 2013
 Write a program that determines the largest value among the integers.

#include <stdio.h>
#include <conio.h>

int main()
{
    int i, num[5], largest = 0;
    int stop = 0;

    while( stop != -1)
    {
        printf("Enter 5 integers separated by a space: ");
      
        for(i=0; i <=4; i++)
        scanf_s("%d", &num[i]);
        largest = num[0];   /* assign the 1st element to largest*/

        for(i=1; i<=4; i++)  /* compare the others and keep storing the largest*/
        if(num[i] > largest)
        largest = num[i];
        printf("The largest number among ");

        for(i=0; i <=4; i++)
        printf("%d ", num[i]);
        printf("is %d\n", largest);
        printf("\nMore data? -1 to stop, others to continue: ");
        scanf_s("%d", &stop);
    }
    return 0;
}
Read more ...

Find Out The Smallest Value

Monday 7 January 2013
 Write a program that determines the smallest value among the integers.

#include <stdio.h>
#include <conio.h>

int main()
{
    int i, num[5], smallest = 0;
    int stop = 0;

    while( stop != -1)
    {
        printf("Enter 5 integers separated by a space: ");
      
        for(i=0; i <=4; i++)
        scanf_s("%d", &num[i]);
        smallest = num[0];

        for(i=1; i<=4; i++)          /* compare the others and keep storing the smallest*/
        if(num[i] < smallest)
        smallest = num[i];
        printf("The smallest number among ");
      
        for(i=0; i <=4; i++)
        printf("%d ", num[i]);
        printf("is %d\n", smallest);   /* print the smallest number*/
        printf("\nMore data? -1 to stop, others to continue: ");
        scanf_s("%d", &stop);
    }
    return 0;
}
Read more ...

Add an integer between 0 to 999

Monday 7 January 2013
Write a program that reads an integer between 0 to 999 and adds all the digits in the integer.

#include <stdio.h>
#include <conio.h>

int main()
{
    int count = 0, num = 0;
    int reminder = 0, sum = 0;
    int stop = 0;

    while (stop != -1)
    {
        printf("Enter an integer: ");
        scanf_s("%d", &num);
      
        printf("\n After operation:\n");
        printf("reminder num\n");
        printf("--------- ---\n");
      
        while(num != 0)
        {
            reminder = num % 10;    /* get the reminder (digits) by dividing by 10*/
            sum = sum + reminder;  /* sum up the reminder*/
            num = num / 10;
            printf("%d %d\n", reminder, num); // let see current value of num and
                                                           // reminder...
        }
        printf("\n");
        printf("The sum of digits is %d\n", sum);   /* print the sum of the digits */
        sum = 0;
        printf("More? -1 to stop, other to continue: ");
        scanf_s("%d",&stop);
    }
    return 0;
}
Read more ...

Find out Even or Odd integer

Monday 7 January 2013
 Write a program that reads an integer and checks whether it is odd or even.

#include <stdio.h>
#include <conio.h>

int main()
{
   int num = 0;
   int reminder = 0;

   while(num != -1)
   {
      printf("Enter an integer (-1 to stop): ");
      scanf_s("%d", &num, sizeof(int));
    
      if(num != -1)
      {
         reminder = num % 2;
         if(reminder == 0)
             printf("%d is an even number.\n", num);
         else
         printf("%d is an odd number.\n", num);
       }
    }
    printf("%d is an odd number.\n", num);
    printf("You ask to stop! Thank you.\n");
  
    return 0;
}
Read more ...

Socket Programming in C

Thursday 3 January 2013

                            Socket Programming in c


How To Create the TCP/IP Client Server Model?
The classic client-server model, the client sends a request to the server, and the server to do some processing, will reply to the request is received (or back) returned to the client. The terms request and the answer may have a different meaning depends on the content and the method of operation.
In addition, the server may be broad to be classified according to the method of two types of them from the customer service request. Duplicate server can service only one customer places. If two or more clients in their requests at the same time messenger, one must wait until other customers received the service. On the other hand the concurrent server can service multiple clients simultaneously. Typically, this request received from a new server process a large number of fertility original procedure returns a listen to the new connection just the process of a large number of reproductive services received request.
Transmission Control Protocol (TCP) to the end of the placement of two end points between groups. Negotiations be carried out to establish a socket, and socket still open during the entire communications. Recipient admits each packet, wrapped resend the implementation of the agreement if the packet is missed or arrives bad. TCP allows one application as it sends the data as much desire and does not depend on IP packet size limit. TCP is responsible for the data is divided into packets, buffer isolated data, re-sending lost or broken wrapped, acknowledge the receipt, by telling the sender acceleration control of rates of data streams, or slow down, for application does not receive more than it can be easily manipulated.
The target host and port number is not sufficient to identify the recipient of a TCP connection. TCP connection becomes unique five unique elements:

-          IP address of the server
-          IP address of the client
-          Port number of the server
-          Port number of the client
-          Protocol (UDP, TCP/IP, etc…)

Where each of the requesting client is assigned a unique port number in view of the server port number is always the same. If any of these numbers are different, the outlet is different. A server so you can listen to a port, conversations with multiple clients at the same time. This TCP connection is more similar to a telephone connection than a letter; You need to not only know the telephone number (IP address), but because the phone may be a lot of people to share in that positioning, you want at the other end (the port number you need) with the name of the user of its conversations. Analogy a little further sent. If you do not hear other people say, simple requests ("What?") Will lead to the other end resend or repeated phrases. Well, the connection remains open until someone hangs up.

TCP CLIENT C PROGRAM:
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

int main()
{
      int sock, bytes_recieved;
      char send_data[1024],recv_data[1024];
      struct hostent *host;
      struct sockaddr_in server_addr;
      host = gethostbyname("127.0.0.1");
      if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
      {
                        perror("Socket");
                        exit(1);
      }
      server_addr.sin_family = AF_INET;  
      server_addr.sin_port = htons(5000);
      server_addr.sin_addr = *((struct in_addr *)host->h_addr);
      bzero(&(server_addr.sin_zero),8);
      if (connect(sock, (struct sockaddr *)&server_addr,
      sizeof(struct sockaddr)) == -1)
      {
           perror("Connect");
            exit(1);
      }
      while(1)
      {

           bytes_recieved=recv(sock,recv_data,1024,0);
           recv_data[bytes_recieved] = '\0';
          if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)
          {
           close(sock);
           break;
          }
          else
           printf("\nRecieved data = %s " , recv_data);
           printf("\nSEND (q or Q to quit) : ");
           gets(send_data);
 
          if (strcmp(send_data , "q") != 0 && strcmp(send_data , "Q") != 0)
           send(sock,send_data,strlen(send_data), 0);
          else
          {
           send(sock,send_data,strlen(send_data), 0);
           close(sock);
           break;
          }
        }
      return 0;

}


TCP SERVER C PROGRAM:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>                                                                                                

int main()
{
        int sock, connected, bytes_recieved , true = 1;
        char send_data [1024] , recv_data[1024];    
        struct sockaddr_in server_addr,client_addr; 
        int sin_size;
   
        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
       {
            perror("Socket");
            exit(1);
       }
       if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&true,sizeof(int)) == -1)
        {
            perror("Setsockopt");
            exit(1);
        }
    
        server_addr.sin_family = AF_INET;      
        server_addr.sin_port = htons(5000);  
        server_addr.sin_addr.s_addr = INADDR_ANY;
        bzero(&(server_addr.sin_zero),8);
        if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))
                                                                       == -1)
      {
            perror("Unable to bind");
            exit(1);
      }
        if (listen(sock, 5) == -1)
      {
            perror("Listen");
            exit(1);
       }
                           
        printf("\nTCPServer Waiting for client on port 5000");
        fflush(stdout);
        while(1)
        {
            sin_size = sizeof(struct sockaddr_in);
            connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size);
            printf("\n I got a connection from (%s , %d)",
                   inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
            while (1)
            {
              printf("\n SEND (q or Q to quit) : ");
              gets(send_data);
           
              if (strcmp(send_data , "q") == 0 || strcmp(send_data , "Q") == 0)
              {
                send(connected, send_data,strlen(send_data), 0);
                close(connected);
                break;
              }
           
             else
                 send(connected, send_data,strlen(send_data), 0);
              bytes_recieved = recv(connected,recv_data,1024,0);
              recv_data[bytes_recieved] = '\0';
              if (strcmp(recv_data , "q") == 0 || strcmp(recv_data , "Q") == 0)
              {
                close(connected);
                break;
              }
              else
              printf("\n RECIEVED DATA = %s " , recv_data);
              fflush(stdout);
            }
        }    
      close(sock);
      return 0;
}
Read more ...