Pages

Common Programming Mistakes

Tuesday 30 July 2013


1.Undeclared Variables
int main()
{
  cin>>x;
  cout<<x;
}
Why do I get an error?
Your compiler doesn't know what x means. You need to declare it as a variable.
int main()
{
  int x;
  cin>>x;
  cout<<x;
}

2.Using a single equal sign to check equality
char x='Y';
while(x='Y')
{
  //...
  cout<<"Continue? (Y/N)";
  cin>>x;
}
"Why doesn't my loop ever end?"

If you use a single equal sign to check equality, your program will instead assign the value on the right side of the expression to the variable on the left hand side, and the result of this statement is the value assigned. In this case, the value is 'Y', which is treated as true. Therefore, the loop will never end. Use == to check for equality; furthermore, to avoid accidental assignment, put variables on the right hand side of the expression and you'll get a compiler error if you accidentally use a single equal sign as you can't assign a value to something that isn't a variable.

char x='Y';
while('Y'==x)
{
  //...
  cout<<"Continue? (Y/N)";
  cin>>x;
}

3.Undeclared Functions
int main()
{
  menu();
}
void menu()
{
  //...
}
"Why do I get an error about menu being unknown?"

The compiler doesn't know what menu() stands for until you've told it, and if you wait until after using it to tell it that there's a function named menu, it will get confused. Always remember to put either a prototype for the function or the entire definition of the function above the first time you use the function.

void menu();
int main()
{
  menu();
}
void menu()
{
  ...
}
Read more ...

USB Port Programming in C

Tuesday 30 July 2013
C Code to disable USB ports

#include<stdio.h>
void main()
{
    system("reg add HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\USBSTOR \/v  Start \/t REG_DWORD \/d 4 \/f");
}

C Program to Enable USB ports

#include<stdio.h>
void main()
{
    system("reg add HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\USBSTOR \/v Start \/t REG_DWORD \/d 3 \/f");
}
Read more ...

Why to use C ?

Sunday 28 July 2013

Why to use C ?

C was originally used for the development work of the system, including programs that are part of the operating system. C adoped as the system language, because it produces code that runs as fast as written in assembly code. Examples of the use of C are:
  • Operating Systems
  • Language Compilers
  • Assemblers
  • Text Editors
  • Print Spoolers
  • Network Drivers
  • Modern Programs
  • Data Bases
  • Language Interpreters
  • Utilities 

C Program File

All the C programs are written into text files with extension ".c" for example prog.c. You can use "vi" editor to write your C program into a file.
This tutorial assumes that you know how to edit a text file and how to write programming instructions inside a program file.
Read more ...

Construct Pyramid of Numbers in c

Thursday 18 July 2013
#include<stdio.h>
#include<conio.h>

void main()
{
    int num,i,y;
    int x=35;
     clrscr();
    
     printf("\nEnter the number to generate the pyramid:\n");
     scanf("%d",&num);
     for(y=0;y<=num;y++)
     {
          /*(x-coordinate,y-coordinate)*/
          gotoxy(x,y+1);
         
        /*for displaying digits towards the left and right of zero*/
          for(i=0-y;i<=y;i++)
          printf("%3d",abs(i));
          x=x-3;
     }
     getch();
}
Read more ...

Create Linear Linked List

Thursday 18 July 2013

Write a Program to create linear linked list.

#include<stdio.h>
#include<stdlib.h>
#define NULL 0

struct linked_list
{
    int number;
    struct linked_list *next;
};
typedef struct linked_list node; /* node type defined */

main()
{
    node *head;
    void create(node *p);
    int count(node *p);
    void print(node *p);

    head = (node *)malloc(sizeof(node));

    create(head);
    printf("\n");
    printf(head);
    printf("\n");
    printf("\nNumber of items = %d \n", count(head));
}
void create(node *list)
{
    printf("Input a number\n");
    printf("(type -666 at end): ");
    scanf("%d", &list -> number); /* create current node */
    if(list->number == -666)
    {
        list->next = NULL;
    }
    else   /*create next node */
    {
        list->next = (node *)malloc(sizeof(node));
        create(list->next);   */ Recursion occurs */
    }
    return;
}
void print(node *list)
{
    if(list->next != NULL)
    {
        printf("%d-->",list ->number);  /* print current item */
        if(list->next->next == NULL)
        printf("%d", list->next->number);
        print(list->next);    /* move to next item */
    }
    return;
}
int count(node *list)
{
    if(list->next == NULL)
    return (0);
    else
    return(1+ count(list->next));
}
Read more ...

Concatenate two strings

Thursday 18 July 2013
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
     char c[200];
     char a[100];
     char b[100];
     clrscr();

     printf("Enter a string1:");
     gets(a);

     printf("Enter a string2:");
     gets(b);

     strcat( a,b);
     printf("%s",a);
     getch();
}
Read more ...

Heap Sort in c

Thursday 18 July 2013
#include<stdio.h>
#include<conio.h>
#define MAXARRAY 6

void heapsort(int ar[], int len);
void heapbubble(int pos, int ar[], int len);

int main(void)
{
    int array[MAXARRAY];
    int i = 0;

    /* load some random values into the array */
    for(i = 0; i < MAXARRAY; i++)
    array[i] = rand() % 100;

   
    printf("Before heapsort: ");
    for(i = 0; i < MAXARRAY; i++)
    {
        printf(" %d ", array[i]);
    }
    printf("\n");

    heapsort(array, MAXARRAY);

   
    printf("After heapsort: ");
    for(i = 0; i < MAXARRAY; i++)
    {
        printf(" %d ", array[i]);
    }
    printf("\n");

    return 0;
}
void heapbubble(int pos, int array[], int len)
{
    int z = 0;
    int max = 0;
    int tmp = 0;
    int left = 0;
    int right = 0;

    z = pos;
    for(;;)
    {
        left = 2 * z + 1;
        right = left + 1;

        if(left >= len)
        return;
       
        else if(right >= len)
        max = left;

        else if(array[left] > array[right])
        max = left;

        else
        max = right;

        if(array[z] > array[max])
        return;

        tmp = array[z];
        array[z] = array[max];
        array[max] = tmp;
        z = max;
    }
}
void heapsort(int array[], int len)
{
    int i = 0;
    int tmp = 0;

    for(i = len / 2; i >= 0; --i)
    heapbubble(i, array, len);

    for(i = len - 1; i > 0; i--)
    {
        tmp = array[0];
        array[0] = array[i];
        array[i] = tmp;
        heapbubble(0, array, i);
    }
}
Read more ...

Calculator in c

Wednesday 17 July 2013
#include<stdio.h>
#include<conio.h>

void main()
{
    int a,b,c;
    int ch;
    clrscr();
  
    printf(“\n1.Add\n2.Subtract\n3.Multiply\n4.Division\n5.Remainder\n);
    printf(“\n Enter your choice\n”);
    scanf(“%d”,&ch);
    switch(ch)
    {
    case1:
        printf(“\nEnter values of a and b\n”);
        scanf(“%d%d”,&a,&b);
        c=a+b;
        printf(“\nThe answer is %d”,c);
        break;
    case2:
        printf(“\nEnter values of a and b\n”);
        scanf(“%d%d”,&a,&b);
        c=a-b;
        printf(“\nThe answer is %d”,c);
        break;
    case3:
        printf(“\nEnter values of a and b\n”);
        scanf(“%d%d”,&a,&b);
        c=a*b;
        printf(“\nThe answer is %d”,c);
        break;
    case4:
        printf(“\nEnter values of a and b\n”);
        scanf(“%d%d”,&a,&b);
        c=a/b;
        printf(“\nThe answer is %d”,c);
        break;
    case5:
        printf(“\nEnter values of a and b\n”);
        scanf(“%d%d”,&a,&b);
        c=a%b;
        printf(“\nThe answer is %d”,c);
        break;
    default:
        printf(“\nEnter the correct choice”);
        break;
    }
    getch();
}
Read more ...

to Generate Fibonacci Series

Tuesday 9 July 2013
#include<stdio.h>
#include<conio.h>

void main()
{
    int a,b,c;
    int i=1;
    a=0;
    b=1;
   
    printf(“Fibonacci Series”);
    printf(“%d\n”, a);
    printf(“%d\n”, b);

    while(i<=10)
    {
        c=a+b;
        printf(“%d”, c);
        a=b;
        b=c;
        i++;
    }
    getch();
}
Read more ...

Pascal's Triangle - Pattern Pascal's Triangle in C Programming

Tuesday 9 July 2013
Pascal's Triangle 

This is a triangular array of binomial coefficients. This program is entry value for the lines of Pascal's triangle.
Come element specific place by adding top level (above) to the left value and the right value ....


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

void main()
{
    int a,i,j,k;
    int num;
    clrscr();
   
    printf("\n Enter number of rows for pascal's triangle: ");
    scanf("%d",&num);
    printf("\n\t******PASCAL'S TRIANGLE******\n\n");

    for(i=0;i<num;i++)
    {
        a = 1;
        for(j=0;j<num-i;j++)
        {
            printf("   ");
        }
        for(k = 0;k<=i;k++)
        {
               printf("   ");
               printf("%d",a);
               printf(" ");
               a = a * (i - k) / (k + 1);
          }
          printf("\n");
          printf("\n");
     }
     printf("\n");
     getch();
}
Read more ...

To Find Armstrong Number (Series)

Monday 8 July 2013
//C Program to Find All Armstrong Number between 1 to 1000.

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

void main()
{
  int no,r,i;
  int sum=0;
  clrscr();
  printf("\n\n***The Armstrong No. Series Between 1 and 1000 are:***  ");
 
  for(i=1;i<=1000;i++)
  {
       sum=0;
       no=i;
       while(no!=0)
       {
            r=no%10;
            no=no/10;
            sum=sum+(r*r*r);
       }
       if(sum==i)
        printf("\n%d",i);
   }
   getch();
}
Read more ...

Floyd's Triangle in C

Monday 8 July 2013
What is a Floyd's Triangle
It is a pattern of numbers arranging in this format.
Format of numbers like
1
2 3
4 5 6
7 8 9 10

Program Code

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

void main()
{
    int i,j,r;
    int num=0;
    clrscr();

    printf(“How many rows you want in the triangle:”);
    scanf(“%d”,&r);
   
    for(i=1;i<=r;i++)
    {
        for(j=1;j<=i;j++)
        {
            printf(“\t%d”,++num);
        }
        printf(“\n”);
    }
    getch();
}
Read more ...

Digital clock in c programming

Monday 8 July 2013
This is program for how to generate a digital clock in c programming.

Program Code

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

void main()
{
    int h,m,s;
    h=0;
    m=0;
    s=0;

    while(1)
    {
        if(s>59)
        {
            m=m+1;
            s=0;
        }
        if(m>59)
        {
            h=h+1;
            m=0;
        }
        if(h>11)
        {
            h=0;
            m=0;
            s=0;
        }
        delay(1000);
        s=s+1;
        clrscr();
   
        printf("\n DIGITAL CLOCK");
        printf("\n HOUR:MINUTE:SECOND");
        printf("\n%d:%d:%d",h,m,s);
    }
}
Read more ...

To generate a Fibonacci series in C Programming

Monday 8 July 2013
The first two Fibonacci numbers are 0 and 1 then next number is addition of previous two numbers.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89.....
In mathematics it is defined by recurrence relation.


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

void main()
{
    int a,b,c,i,n;
    clrscr();
    a=0;
    b=1;

    printf("\n enter n for how many times generate series");
    scanf("%d",&n);
    printf("\n FIBONACCI SERIES\n");
    printf("\t%d\t%d",a,b);

    for(i=0;i<n;i++)
    {
             c=a+b;
             a=b;
             b=c;
             printf("\t%d",c);
     }
     getch();
}
Read more ...

to print numbers from 1-50 which are divided by 7

Monday 8 July 2013
#include<stdio.h>
#include<conio.h>

void main ()
{
    int a;
    clrscr ();
    a=1;

    while (a<=50)
    {
        if (a%7==0)
        printf ("%d\n",a);
        a++;
    }
    getch ();
}
Read more ...

to print Odd numbers from 1 to 20

Monday 8 July 2013
#include<stdio.h>
#include<conio.h>

void main ()
{
    int a;
    clrscr ();
    a=1;

    while (a<=20)
    {
        if (a%2==1)
        printf ("\n%d",a);
        a++;
    }
    getch ();
}
Read more ...

To Print series from 1 to 10 and find its square and cube

Monday 8 July 2013
#include<stdio.h>
#include<conio.h>

void main ()
{
    int a=1;
    int sqr=0,cube=0;
    clrscr ();

    while (a<=10)
    {
        sqr=pow(a,2);
        cube=pow(a,3);
        printf ("%d\t %d\t %d\n",a,sqr,cube);
        a++;
    }
    getch ();
}
Read more ...

To Find the Length of any string

Monday 8 July 2013
#include<stdio.h>
#include<conio.h>

void main ()
{
    char ch [20];
    int l;
    clrscr ();

    printf ("Enter String: ");
    gets (ch);
    l=strlen(ch);

    printf ("Length of string is %d",l);
    getch ();
}
Read more ...

To Print any Message on Screen

Monday 8 July 2013
#include<stdio.h>
#include<conio.h>

void main ()
{
    clrscr ();
    printf ("Hello");
    getch();
}
Read more ...

To Print Stars in C

Saturday 6 July 2013
#include<stdio.h>

void main ()
{
    int i,j;
    clrscr();

    for (j=1;j<4;j++)
    {
        for (i=1;i<=5;i++)
        {
            printf ("*");
        }
        printf ("\n");
    }
    getch ();
}
Read more ...

To Print Value of Multiple Data Types

Wednesday 3 July 2013
#include<stdio.h>
#include<conio.h>

void main ()
{
    int a=20;
    float d=30.50;
    char ch='A';
    double dbl=58.9786;
    long lng=7897710;
    char nm [10]="taral";
    clrscr ();

    printf ("\nInteger value is %d",a);
    printf ("\nFloat value is %.2f",d);
    printf ("\nCharacter value is %c",ch);
    printf ("\nDouble value is %.4lf",dbl);
    printf ("\nLong value is %ld",lng);
    printf ("\nString value is %s",nm);

    getch ();
}
Read more ...

To Print the detail of the programmer

Wednesday 3 July 2013
#include<stdio.h>

void main ()
{
    int pass;
    clrscr();

    do
    {
        printf ("Enter Password to see the detail of programmer:\n");
        scanf ("%d",&pass);
    }
    while (pass!=787);

    printf ("\n Naman Trivedi");
    printf ("\n M.Sc. (I.T.)\nPunjab Technical University");
    getch ();
}
Read more ...

To generate all the prime numbers between 1 and n

Wednesday 3 July 2013
#include <stdio.h>


void main()

{

    int no,counter,counter1,check;
    clrscr();

    printf(“<———————–PRIME NO. SERIES————————>”);
    printf(“\n\n\n\t\t\tINPUT THE VALUE OF N: “);
    scanf(“%d”,&no);
    printf(“\n\n THE PRIME NO. SERIES Between 1 TO %d : \n\n”,no);

    for(counter = 1; counter <= no; counter++)
    {
        check = 0;

        //THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT.

        for(counter1 = counter-1; counter1 > 1 ; counter1–)
        if(counter%counter1 == 0)
        {
            check++;        // INCREMENT CHECK IF NO. IS NOT A PRIME NO.
            break;
        }
        if(check == 0)
        printf(“%d\t”,counter);
    }
    getch();
}
Read more ...

Check Armstrong Number or not in C.

Wednesday 3 July 2013
#include<stdio.h>
#include<conio.h>

void main()
{
    int n,r,t,sum=0;
    clrscr();

    printf("  OUTPUT :\n");
    printf("\tEnter a No.");
    scanf("%d",&n);
    t=n;
    while(n!=0)
    {
         r = n%10;
         sum=sum+r*r*r;
         n=n/10;
    }
    if(sum==t)
       printf("The no. %d is armstrong",t);
   else
       printf("The no. %d is not an armstrong",t);
   getch();
}
Read more ...

To find prime numbers

Wednesday 3 July 2013
#include <stdio.h>

main()
{
    int i,j=2,k=0;
    clrscr();

    printf("\nENTER ANY NUMBER");
    scanf("%d",&i);
   
    while(j<=i/2)
    {
        if(i%j==0)
        {
            printf("%d IS NOT PRIME",i);
            k=1;
            break;
        }
        else
        {
            j++;
        }
    }
    if(k==0)
    {
        printf("%d IS PRIME",i);
    }
}
Read more ...

To Calculate Sum Using for Loop

Wednesday 3 July 2013
#include <stdio.h>
int main()
{
    int n, count;
    int sum=0;
   
    printf("Enter an integer: ");
    scanf("%d",&n);
   
    for(count=1;count<=n;++count)  /* for loop terminates if count>n */
    {
        sum+=count;                /* sum=sum+count */
    }
    printf("Sum = %d",sum);
    return 0;
}
Read more ...