Saturday, 9 February 2013

C program for Reversing a string and palindrome checking with explanation

Step 1:

Open the IDE and type the coding given here.

Step 2:

CODING:

#include<stdio.h>
#include<string.h>
main()
{
    char str[80], rev[80];
    int i,j,len;
    printf("Reversing the string and checking for palindrome\n\n");
    printf("Enter string\n");
    gets(str);
    len=strlen(str);
    for(i=len-1,j=0;i>=0;i--,j++)
    rev[j]=str[i];
    rev[j]='\0';
    printf("Entered string is ");
    printf(str);
    printf("\n\n");
    printf("reversed string is ");
    printf(rev);
    printf("\n\n");
    if(strcmp(str, rev)==0)
    printf("\n\n%s is a palindrome", str);
    else
    printf("\n\n%s is not a palindrome", str);
    getch();
}

OUTPUT:


EXPLANATION:
str, revi,j,len are declared.
strlen() calculates the length of a string.

i  0  1  2  3
   r  o  s   e 
for loop reverses the string as:
j  0  1  2  3
   e  s  o  r
strcmp compares the two strings.

Friday, 8 February 2013

C program for Floyd's triangle with explanation

Step 1:
Open the IDE

Step 2:
Type the coding given below:

CODING:
 
#include<stdio.h>
#include<stdlib.h>
main()
{
int n, i,j,line;
printf("\n\n");
printf("PROGRAM FOR FLOYD'S TRIANGLE\n\n");
printf("enter the number..");
scanf("%d", &n);
line=1;
j=1;
while(line<=n)
{
    for(i=1;i<=line;i++)
    {
        printf("%d", j);
        printf("\t\t");
        j++;
    }
    line++;
    printf("\n");
}
getch();
}

OUTPUT:


EXPLANATION:
Floyd's triangle is a right-angled-triangular array of natural numbers, used in computer science education. It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15


First, the value of line, j, i are assigned as 1.

 Here I gave the n value as 5.
Step 1:
1. while(1<=5)true
2. for(i=1;1<=1; i++); True
3. print 1;
4. tab
5. j++ =2.
6. i=2;
7. For(2<=1)False
8. line++ = 2.

Step 2:
While(2<=5)
for(i=1; 1<=2;i++)
 print j. (2).
tab
j++ = (3)
i=2;
for(2<=2)True
print j(3)
tab
j++ = 4
i=3.
for(3<=2)False
Then line++ = 3.

Step 3:
while(3<=5)
for(i=1;1<=3;i++)
print j=4.
tab
j++ = 5.
i=2.
for(2<=3)
print j=5.
tab
j++ =  6.
i=3.
for(3<=3)
print j=6.
tab
j++ = 7
i=4.
for(4<=3)False
Then line++ = 4.
Same procedure until while condition becomes false.























































































Simple C program for fibonnacci series

Open the IDE and type the program.

CODING:
#include<stdio.h>
main()
{
    int f1=-1, f2=1;
    fib(f1,f2);
    getch();
}
fib(fib1,fib2)
{
int fib3, n,i;
printf("\nFIBONCCI SERIES USING FUNCTION");
printf("\n\nEnter the no.of terms")    ;
scanf("%d", &n);
printf("\n\n The fibonocci series is..\n");
for(i=0;i<n;i++)
{
    fib3=fib1+fib2;
    fib1=fib2;
    fib2=fib3;
    printf("\n%d", fib3);
}
}

OUTPUT:


EXPLANATION: 
 First f1=-1, f2=1 is assigned. Next it is passed to Fib(fib1, fib2). for loop is executed for n times.
Here, for example I gave 5 as n value. 

For i=0, fib1=-1, fib2=1 then fib3=fib1+fib2(-1+1=0). Then, fib1=1(ie. fib2).fib2=0(ie.fib3)

For i=1, fib3=1+0=1. Then fib1=0. fib2=1.

For i=2, fib3=0+1=1. Then fib1=1. fib2=1.

For i=3, fib3=1+1=2. Then fib1=1. fib2=2.

For i=4, fib3=2+1=3. Then fib1=2. fib2=3.

The output of fib3 is printed for n times.

C program for Converting centigrade value to fahrenheit

Step 1:
Open the IDE and File->New.

Step2:
Type the coding given here.

CODING:

#include<stdio.h>
main()
{
    float cg, fr;
    printf("\n CONVERTING CENTIGRADE TO FAHRENHEIT VALUE");
    printf("\n\nEnter the centigrade value");
    scanf("%f", &cg);
    fr=(9.0/5.0*cg+32.0);
    printf("\n\nThe centigrade value is %5.2f",cg );
    printf("\n\nThe fahrenheit value is %5.2f", fr);
    getch();
}



OUTPUT:



EXPLANATION:
  First declaring the variables. The formula is F = (9/5)*C + 32. stdio.h is used so that we use scanf and printf.


Program to find the area and circumference of a circle

[ We have learnt how to install the IDE and to do the programs already. If any doubts visit my first Program.]
                 TO FIND AREA AND CIRCUMFERENCE OF A CIRCLE

CODING:

#include<stdio.h>
main()
{
    float area, radius,circum;
    printf("\n");
    printf("Enter the radius: ");
    scanf("%f",&radius);
    circum = 2.0*22.0/7.0*radius;
    area = 22.0/7.0*radius*radius;
    printf("AREA AND CIRCUMFERENCE OF A CIRCLE");
    printf("\n*********************************");
    printf("\n\n");
    printf("The radius is %5.2f", radius);
    printf("\n\n");
    printf("The Area is %5.2f", area);
    printf("\n\n");
    printf("The Circumference is %5.2f", circum);
    printf("\n\n");
    getch();   
}

OUTPUT:


EXPLANATION:
   First, the variables has been declared as float type. we have to use scanf method to read the value and the data type has to be given as %f. Formula for area of a circle is
π×(radius)2 and for circumference is 2 π×r.

  getch() is a function which has its protype defined in conio.h header file.
it is basically used to take input a single character from keyboard and this char is not displayed at the screen.
it waits until itn gets a input that's why it can be used as a screen stopper.

C language Keywords

Learn these C keywords. 

auto       double     int          struct
break      else       long         switch
case       enum       register     typedef
char       extern     return       union
const      float      short        unsigned
continue   for        signed       void
default    goto       sizeof       volatile
do         if        
static       while

Thursday, 7 February 2013

A first simple program in c


Step 1:
    If you are using windows 7, please install the C-Free software to work with 'c' language.

[Note: C-Free is a professional C/C++ integrated development environment (IDE) that support multi-compilers.

Step 2:
Step 3:
Install the IDE and select File->New.






Step 4:
   Type the program.
   A simple program is given here.
Coding:
#include<stdio.h>
main()
{
    printf("\n\n HELLO WORLD \n");
}


Step 5:
  Save the file as hello.c.

Step 6:
Compile and run your simple program. Click Makes and Run button to do this.


Step 7:
  Your output will be:

Now you have learnt a very simple program in C language.


  

Tuesday, 5 February 2013

About Me

Hai friends! I am ShanthiPriya. Am just going to share what i have learnt.