Tuesday, 8 July 2014

A PROGRAM TO READ AND PRINT ELEMENTS IN A SINGLE DIMENSIONAL ARRAY

A PROGRAM TO READ AND PRINT ELEMENTS IN A SINGLE DIMENSIONAL ARRAY

#include<stdio.h>
#include<conio.h>
void main( )
{
int n,i,a[20];
clrscr ( );
printf ("enter number of terms in the array\n");
scanf ("%d",&n);
printf ("enter elements in the array\n");
for( i=0 ; i < n ; i++ )
{
scanf ("%d", &a[i] );
}
printf ("the elements are\n");
for ( i = 0 ; i < n ; i++)
{
printf ("%d" , a[i] );
}
getch( );
}

SINGLE- DIMENSIONAL ARRAY

SINGLE- DIMENSIONAL ARRAY

  • IT IS SOMETIMES CALLED A LIST.
  • IT IS A COLLECTION OF SIMILAR DATA TYPES.
  • IT IS THE SIMPLEST FORM OF AN ARRAY.
  • DECLARATION PART :-
Specify its name , size and data types of the item.

SYNTAX :-
        storage_class data_type array_name[size];

EXAMPLE:-
        int arr[ 20 ];
        float array[40];
        int X[5] ={ 2,3,4,5,6 };

NOTE- 
Array elements are accessed by their index value. The index of an array starts from zero.
int marks[4] = { 70,80,90,85 };
The value 70 lies in the index position zero and 85 lies in index position three ( n - 1 ).

Monday, 7 July 2014

ARRAY

ARRAY



WHAT IS AN ARRAY?
  • An array is a derived data type from fundamental data type.
  • It is a collection of variables of the same type that are referenced by a common name which are stored in a contiguous memory location.

DECLARING AN ARRY

  • Arrays are declared by specifying its data type,name and the number of elements the array holds between square brackets immediately following the array name.
Eg- int name [20];
    -  int student_marks[5][20];

TYPES- 1 DIMENSIONAL
            - N DIMENSIONAL

C PROGRAM TO REVERSE A 3 DIGIT NUMBER

REVERSING A 3 DIGIT NUMBER



#include<conio.h>
#include <stdio.h>
 
int main()
{
   int n, reverse = 0;
 clrscr ();
   printf("Enter a number to reverse\n");
   scanf("%d",&n);
 
   while (n != 0)
   {
      reverse = reverse * 10;
      reverse = reverse + n%10;
      n = n/10;
   }
 
   printf("Reverse of entered number is = %d\n", reverse);
 
   return 0;
}

FIBONACCI SERIES( using for loop )

FIBONACCI SERIES( using for loop )


#include<stdio.h>
#include<conio.h>
void main();
{
int n,first=0,second=1,next,c;
clrscr( );
printf("enter no. of terms");
scanf("%d",&n);
for(c=0;c<n;c++)
{
if(c<=1)
{
next=c;
}
else
{
next=first+second;
first=second;
second=next;
}
}
printf("%d",n);
getch();
}

Monday, 30 June 2014

APPLICATION OF DO- WHILE LOOP

APPLICATION OF DO- WHILE LOOP




#include <stdio.h>

 void main()
{
    int i = 10;

    do{
       printf("Hello %d\n", i );
       i = i -1;
    }
while ( i > 0 );
}


OUTPUT-

Hello 10
Hello 9
Hello 8
Hello 7
Hello 6
Hello 5
Hello 4
Hello 3
Hello 2
Hello 1

A SIMPLE WHILE LOOP APPLICATION USING BREAKS

SIMPLE WHILE LOOP APPLICATION USING BREAKS








#include <stdio.h>

main()
{
    int i = 10;

    while ( i > 0 )
    {
       printf("Hello %d\n", i );
       i = i -1;
       if( i == 6 )
       {
          break;
       }
    }
}





OUTPUT-         Hello 10
Hello 9
Hello 8
Hello 7