Sunday, 30 December 2012

Number Palndrom Program in C

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

    int n, m, temp=0;
    clrscr();
    printf("Enter the Number");
    scanf("%d",&n);
    m=n;

    while(n>0)
    {
    temp=(temp*10)+n%10;
    n=n/10;
    }

    if(m==temp)
    printf("Palndrom");
    else
    printf("Not Palndrom");

    getch();
    }

Tuesday, 4 December 2012

Global & Local Decloration

    #include<stdio.h>
    #include<conio.h>     //Example for global declaration
    #define a 10

    main()
    {
     int b=20;      //expample for local declaration
     clrscr();

    printf("The Global Variable value is: %d\n",a);
    printf("The Local Variable value is: %d",b);

    getch();

    }

Out put is:

Prog for print int, long data types

#include<conio.h>
    #include<stdio.h>
    main()
    {
    int a;
    long b;
    clrscr();
    printf("Enter the int value: ");
    scanf("%d",&a);
    printf("The value is: %d",a);
    printf("\n Enter the Long int value: ");
    scanf("%ld",&b);
    printf("The value is: %ld",b);

    getch();
    }

Application of logic - planning and writing algorithms



Planning and writing algorithms
Throughout this course you will be required, in both labs and assessments, to write algorithms to solve a particular problem. When planning and writing the algorithm it is useful to consider the following points:
General algorithm design and structure
In general, before writing an algorithm, it is useful to spend a few minutes thinking about the problem and how you would go about solving it without a programming language. One question you can ask yourself is if I had to solve this problem with a pen, paper and pocket calculator how would I go about doing it? Some students find it useful to sketch a simple flow diagram during this process.
In all cases you should be clear about the exact sequence of commands that the algorithm has to perform. It must be possible to follow an algorithm step-by-step. The instructions must be given in the correct order and there must be some way of telling when to finish one instruction and begin another. This is particulary important for programs that involve repeating a process several times (see Session 3 on looping). Similarly, your algorithm must have a definite stop condition. Often this is a trivial thing like outputting a calculated value. However, some care has to be given to ensure that a program will stop.
Writing and checking the algorithm
When writing your algorithm it is a very good idea to compile the source code regularly, after each few lines of code have been added. In this way you are able to spot any compilation errors immediately. Another reason why this is good practise is that it forces your code to be saved regularly so that if your computer crashes you do not lose hours worth of editing.
As you develop the algorithm you may want to add simple printf statements to write out values along the way to check that your code is doing what you expect it to.
Other things to bear in mind whilst developing your algorithm include
  • Think carefully about the type of each variable, in particular, don't use double or float for variables that can only have integer values - you may run into rounding error problems. You should never use real numbers as for loop indices.
  • Think carefully about initialisation of variables. If you start adding numbers to an uninitialised variable you may well get a meaningless value. In lots of cases it makes sense to initialise to 0 but this is not always the case (consider, e.g. how you initialised for Exercise 3.2 which calculated a factorial).
  • Where appropriate, any data read in or written out must be checked if it makes sense. A good example of this is in the Day of week exercise which is part of this topic. You are asked to enter a date in the month and a month in the year. Clearly entering 55 then 9 for the 55th of September makes no sense. Your algorithm should always check for this type of error. If such checks are missing you will lose marks in an assessment.
Testing the final algorithm
Once you have finished writing your algorithm and you are confident it is working correctly you will want to check it is working OK. Sometimes an assessment may give you some example values to check. Otherwise, you can always modify your code to check values that you know.

c program for finding gcd (greatest common divisor)

#include<stdio.h>

int main(){

    int x,y,m,i;

    printf("Insert any two number: ");

    scanf("%d%d",&x,&y);
    if(x>y)
         m=y;
    else
         m=x;
    for(i=m;i>=1;i--){
         if(x%i==0&&y%i==0){
             printf("\nHCF of two number is : %d",i) ;
             break;
         }
    }
    return 0;
}