Wednesday, 29 January 2014

Explain strlen() function.

strlen() function-It counts number of characters in a string.
Syntax
l=strlen(string)   ,where l reads the count of characters

Write a program to read a string through keyboard.Determine length of the string.

#include<stdio.h>
#include<conio.h>
main()
{
    char string[10];
    int string,l;
    printf("Enter the string:");
    gets(string);
    l=strlen(string);
    printf("Entered name is %s and its length is %d",string,l);
}
Output
Enter the string:
Rohini
Entered string is Rohini and its length is 6

Explain string with different formats.

String
Group of characters,digits,symbols enclosed within quotation marks are called strings.The C compiler inserts NULL(\0) character automatically at end of the string.
Different formats of initialization of string
char name[]="INDIA";
or
char name[]={'I','N','D','I,'A','\0'};
or
char name[]={{I},{N},{D},{I},{A}};

Write a program to print "INDIA" by using different formats of initialization of array.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
   {
    char a1[]={'I','N','D','I''A','\0'};
    char a2[]="INDIA";
    char a3[6]={{I},{N},{D},{I},{A}};
    clrscr();
    printf("\n Array1=%s",a1);
    printf("\n Array2=%s",a2);
    printf("\n Array3=%s",a3);
   }
Output
Array1=INDIA
Array2=INDIA
Array3=INDIA

Write a program to display string 'PRABHAKAR 'using different formats.
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[]="PRABHAKAR";
clrscr();
printf("  %s  \n ",a);
printf(" %.5s \n ",a);
printf(" %.8s \n ",a");
printf(" %.15s \n ",a);
printf(" %-10.4s  \n",a);
printf(" %11s \n ",a);
}
Output
PRABHAKAR
PRABH
PRABHAKA
PRABHAKAR
PRAB
PRABHAKAR

Print elements of character array given with while loop (help of Null character).

#include<stdio.h>
#include<conio.h>
main()
{
    char a[]="Have a nice day";
    int i=0;
    clrscr();
    printf("Elements of the array:");
    printf("\n");
    while(a[i]!='\0')
        {
        printf("  %c ",a[i]);
        i++;   
        }

}
Output
Elements of the array:
Have a nice day

Explain two dimensional array with example.Write a program to print a matrix and its transpose.

Two dimensional array-It is a collection of a number of one-dimensional arrays.Each row can be thought of as a one-dimensional array.
It is thought as a rectangular display of elements with rows and columns.
        column1        column2     column3
row1        x[0][0]        x[0][1]        x[0][2]
row2        x[1][0]        x[1][1]        x[1][2]
row3        x[2][0]        x[2][1]        x[2][2]

Write a program to display elements of two dimensional array.
#include<stdio.h>
#include<conio.h>
main()
{
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
printf("Elements of an array");
for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            {
            printf(" \t %d ",a[i][j]);
            }
        printf(" \n ")
           
    }
   Output
   1 2 3
   4 5 6
   7 8 9

Write a program to enter elements of a matix with order 3x3 and print the matrix.


#include<stdio.h>
#include<conio.h>
main()
{
int a[3][3];
printf("Enter elements of an array");
for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            {
            scanf(" \t %d ",&a[i][j]);
            }
        printf("\n")
    }
for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            {
            printf(" \t %d",a[i][j]);
            }
        printf("\n")
    }
       Output
    Enter elements of an array
       1 2 3
       4 5 6
       7 8 9
Write a program to enter elements of a matix with order 3x3 and display its transpose.

#include<stdio.h>
#include<conio.h>
main()
{
int a[3][3];
printf("Enter elements of an array");
for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            {
            scanf(" \t %d ",&a[i][j]);
            }
        printf("\n")
    }
for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            {
            printf(" \t %d",a[i][j]);
            }
        printf("\n")
    }
printf("Transpose of matrix");
for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            {
            printf(" \t %d",a[j][i]);
            }
        printf("\n")
    }
}
    Output
Enter elements of an array
    1 2 3
    4 5 6
    7 8 9
Transpose of matrix
    1 4 7
    2 5 8
    3 6 9

Explain array with one dimensional array example

Array-It is a set of unique variables with same data type located in continous memory locations.

Declaration of array
int a[5];
Here set of 5 elements with integer data type is stored in the array a.

initialization of array
int a[5]={1,2,3,4,5}

Important point-
(a)Array elements are called by array names.
array element 1 called by array name a[0],array element 2 called by array name a[1],array element 3 called by array name a[2],
array element 4 called by array name a[3],array element 5 called by array name a[4]
(b)Any particular element of an array can be modified seperately without disturbing other elements.
Example-
int a[5]={1,2,3,8,5}
To carry out this task the statement a[3]=4 can be used to change element 8 to 4 from array a[5].
(c)Starting memory location of array a assumed 2000 then next element located in 2002 because each integer element requires 2 bytes.
So character-1 byte,integer-2bytes,float-4 bytes,long-4 bytes,double-8 bytes.

Character array-They are called as strings.Here NULL('\0') character is automatically added at the end.
NULL acts as an end of the character array when compiler reads it .

Write a program to display elements of one dimensional character array with address.
#include<stdio.h>
#include<conio.h>
main()
{
int i=0;
char name[6]={'A','R','R','A','Y'};
printf("Character and memory location");
while(name[i]!='\0')
    {
        printf("    \n  %c \t %u   ",name[i],&name[i]);
        i++;
    }
}
Output
Character and memory location
A    4054
R    4055
R    4056
A    4057
Y    4058

Wednesday, 22 January 2014

Explain each decision statements with example.

It checks the condition,then executes its sub block.

if statement-It checks the condition,if condition is true then executes its sub block.
Otherwise when condition is false,the compiler skips lines within the if block.
It is enclosed in curly braces.
Use curly braces even with a single statement.

syntax
if(condition is true)
{
statement;
}

Write an example for if statement checking if entered number is less than 10.
#include<stdio.h>
#include<conio.h>
main()
 {
  clrscr();
  int a;
  printf("Enter the number:");
  scanf("%d",&a);
  if(a<10)
   {
    printf("Entered number is less than 10");
    else
    printf("Entered number is not less than 10");
   }
}

Output

Enter the number:5

Entered number is less than 10

if else statement-It has two blocks.if condition is true first block is executed otherwise if condition is false else block is executed.

syntax
if(condition is true)
  {
   statement1;
  }
 else
  {
  statement2;
  }

Write example for if else statement.Read values of a,b,c Add and check after addition if it is in the range of 100 & 200 or not.
 

#include<stdio.h>
#include<conio.h>
main()
 {
  clrscr();
  int a,b,c;
  printf("Enter the numbers a b c:");
  scanf("%d %d %d",&a.&b,&c);
  printf("\n a=%d b=%d c=%d",a,b,c)
  d=a+b+c;
  if(d>=100 & d<=200)
    {
    printf("\n Sum is in between 100 and 200");
    else
    printf("\n Sum is not in between 100 and 200");
    }
 }

Output
Enter the numbers a b c: 50 50 20
a=50 b=50 c=20
Sum is in between 100 and 200

nested if statement-If condiion is true then first block is executed.Otherwise in else block condition is checked with the if statement..

syntax
if(condition is true)
  {
   statement1;
  }
   else if (condition is true)
              {
              statement2;
              }
            else
              {
              statement3;
              }

Write an example for nested if else statement where find largest among 3 numbers.

#include<stdio.h>
#include<conio.h>
main()
 {
  clrscr();
  int a,b,c;
  printf("Enter the numbers a b c:");
  scanf("%d %d %d",&a.&b,&c);
  printf("\n a=%d b=%d c=%d",a,b,c)
 
  if(a>b)
    {
      if(a>c)
          {
          printf("\n Largest number is %d",a);
          }
          else
          {
          printf("\n  Largest number is %d",c ");
          }

    }
  else
    {
       if(c>b)
           {
           printf("\n Largest number is %d",c);
           }
           else
           {
          printf("\n  Largest number is %d",b ");
           }

   }
 
Output

Enter the numbers a b c:10 20 30
Largest number is 30

break statement-break skips from the loop in which it is defined and then goes automatically to first statement after loop.

continue statement-It is used for continuing in next iteration of loop statement and it skips the statement after this statement.

goto statement-Where label is the position where the control is to be transfered.

syntax
goto label;

where label name must start with any character

Write an example of goto statement checking the number whether even or odd.
   
    #include<stdio.h>
    #include<conio.h>   
    main()
     {
    int a;
    clrscr();    
    printf("Enter the number:");
    scanf("%d %d",&a);
      if(a%2==0)
             {
             goto even:
             }
               else
             {
             goto odd;
             }
    even:
    printf("\n %d is even");
    return;
    odd:
    printf("\n %d is odd");
    }

       Output
     
 Enter the number:4
       4 is even

Switch statement-Used to make a choice from a number of options.It need only one argument of any data type,which is checked with many case options.
   
    syntax
    switch(variable)
        {
        case constant a:
        statement;
        break;
        case constant b:
        statement;
        break;
        default:
        statement;
        }
Write an example for switch statement showing functions like addition ,subtraction
,multiplication,division,remainder.
    #include<stdio.h>
    #include<conio.h>
    main()
     {
          clrscr();
          int a,b,c,ch;
        printf("0 \n TERMINATED BY CHOICE");
          printf("1 \n FOR ADDITION");
          printf("2 \n FOR SUBTRACTION");
          printf("3 \n FOR MULTIPLICATION");
          printf("4 \n FOR DIVISION");
          printf("5 \n FOR REMAINDER");
          printf("6 \n FOR EXIT");
          printf("\n ENTER YOUR CHOICE")
        scanf("%d",&ch);
            if(ch<=6 && ch>=0)
                {
                    printf("Enter 2 numbers:");
                    scanf("%d %d",&a,&b);
                }
        switch(ch)
        {
       
        case 1:
        c=a+b;
        printf("ADDITION:%d",c);
        break;
       
        case 2:
        c=a-b;
        printf("SUBTRACTION:%d",c);
        break;

        case 3:
        c=a*b;
        printf("MULTIPLICATION:%d",c);

        case 4:
        c=a/b;
        printf("DIVISION:%d",c);
        break;
       
        case 5
        c=a+b;
        printf("REMAINDER:%d",c);
        break;

        case 0:
        printf("\n TERMINATED BY CHOICE");
        exit();
        break;
        default:
        printf("Invalid choice");
           }
    }
       

Thursday, 16 January 2014

Explain increment operator with example.

The operator ++ adds one to its operand.
x=x+1 can be written as x++ or ++x
If ++ is used as prefix then value of variable will be increased first.
If ++ is used as suffix then value of variable will be increased later.

Write a program to show effect of increment operator as a prefix.

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int x=10,y=10,z;
z=x* ++y;  /* y is increased first then used for multiplication */
printf("Value of z = %d",z);

output
Value of z=110


Write a program to show effect of increment operator as a suffix.

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int x=10,y=10,z;
z=x*y++;  /* First multiplication then y is increased*/
printf("Value of z = %d",z);

output
Value of z=101


Write a program to show '&' operator.

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int x=2;
float y=2;
printf("Address of x=%u,Address of y=%u",&x,&y);
}

output
Address of x=4066,Address of y=25096

Explain conditional operator with example.

Conditional operator
Here if given condition is true then exp1 is evaluated else exp2.

syntax
condition?(exp1) : (exp2);

Write a program to show conditional operator
#include<stdio.h>
#include<conio.h>
main
int x=2;
()
{
clrscr();
printf("Result=%d",1==2?4:5);
}

output
Result=5