Delicious Digg Facebook Favorites More Stumbleupon Twitter
Showing posts with label Example Programs in 'C'. Show all posts
Showing posts with label Example Programs in 'C'. Show all posts

Monday, 8 October 2012

check addresses are correct or not


    /* cosider 3 variables and find the addresses ,first two variables
        addresses difference is equal to another two variables  
        addresses  difference. */

#include<stdio.h>
main()
{
int x=10,y=20,z=30;
clrscr();
printf("address of x=%d\n",&x);
printf("value of x=%d\n",x);
printf("address of y=%d\n",&y);
printf("value of y=%d\n",y);
printf("address of z=%d\n",&z);
printf("value of z=%d\n",z);
getch();
}

output :-
address of x= -12
value of x= 10
address of y= -14                       
value of y= 20
address of z= -16
value of z= 30



Friday, 21 September 2012

program for calculator



              #include<stdio.h>
              main()
             {
                int a,b;
                char op;
                clrscr();
                printf("enter the operator(+,-,*,/)\n:");
                scanf("%c",&op);
                printf("enter the values of a,b:\n");
                scanf("%d%d",&a,&b);
                if(op == '+')
                printf("\n%d+%d=%d",a,b,a+b);
                else if(op == '-')
                printf("\n%d-%d=%d",a,b,a-b);
                else if(ch == '*')
                printf("\n%d*%d=%d",a,b,a*b);
                else if(ch == '/')
                printf("\n%d/%d=%d",a,b,a/b);
                else
                printf("sorry invalid operator");
                getch();
              }             

                              
                  
                  

Program to Transpose a matrix


   /* Program to Transpose a matrix */

                             

                    #include<stdio.h>
                    main()
                   {
                      int j,k,a[3][3],b[3][3];
                      clrscr();
                      printf("\nEnter the elements for matrix :");
                      for(j=0;j<3;j++)
                      {
                          for(k=0;k<3;k++)
                         {
                             printf("\nenter elements for a[%d][%d]:",j,k);
                             scanf("%d",&a[j][k]);
                          }
                       }
                       for(j=0;j<3;j++)
                       {
                              for(k=0;k<3;k++)
                              b[j][k]=a[k][j];
                       

                                   
                       printf("\nthe transpose matrix is ..:\n");
                       for(j=0;j<3;j++)
                      {
                           for(k=0;k<3;k++)
                           printf("\t%d",b[j][k]);
                           printf("\n");
      
                }
                      getch();

                  }

   

Thursday, 20 September 2012

sum of two matrices



                                      /* sum of two matrices */

            #include<stdio.h>
            main()
            {
                   int j,k,a[3][3],b[3][3],c[3][3];
                   clrscr();
                   printf("\nEnter the elements of matrix :");
                   for(j=0;j<3;j++)
                  {
                          for(k=0;k<3;k++)
                          {
                              printf("\nenter elements for a[%d][%d]:",j,k);
                              scanf("%d",&a[j][k]);
                           }
                  }
                   for(j=0;j<3;j++)
                  {
                          for(k=0;k<3;k++)
                         {
                             printf("\nenter elements for b[%d][%d]:",j,k);
                             scanf("%d",&b[j][k]);
                         }
                  }
                  printf("\nthe matrix is ..:\n");
                  for(j=0;j<3;j++)
                  {
                          for(k=0;k<3;k++)
                         {
                          c[j][k]=a[j][k]+b[j][k];
                          printf("\t%d",d[j][k]);
                             
                         }
                          printf("\n");
                  }
                  getch();
 
      }

Monday, 10 September 2012

Temperature Conversion



               
                   Temperature Conversion     

   #include<stdio.h>

    main()

   {

       Char temp_type;

           float  temp,fahren,Celsius;

           printf(“\nenter temperature:”);

           scanf(“%f”,&temp);

           printf(“\nenter conversion type f or c:”);

           scanf(“%c”,&temp_type);

           if(temp_type == ‘c’)

           {

                fahren = (5.0/9.0) * (temp-32.00);

                printf(“\nthe equivalent Celsius = %f”,fahren);

            }

            else

            {

                 Celsius  = (9.0 * temp)/5.0 + 32;

                 Printf(“\nthe equivalent Fahrenheit = %f”,Celsius);

            }

   }

Calculating conditional Taxes


                    
                            Calculating conditional Taxes

        #include <stdio.h>

        main()

       {

               float taxable,taxes;

              clrscr();

              printf(“enter the income:”);

              scanf(“%f”,&taxable);

              if(taxable <= 20000.00)

             taxes = 0.02 * taxable;

             else

             taxes = 0.025 * (taxable – 20000.00)+400.00;

             printf(“\n\nthe taxes are %0.2f”,taxes);

             getch();

        }



 

Friday, 7 September 2012

program for increment & decrement operators


    Program for Increment & Decrement Operators

   Increment operators are divided into 2 types(++).
   *  post increment.
       a++;
       The original value of 'a' is assigned  to 'a' first,increment
       (+1) to the next operation.
   *  pre increment.
       ++a;
       The incremented value(+1) is assigned first,after perform the 
       operation.

      example1 :- 
              
         #include<stdio.h>
        main()
       {
          int a =6;
          clrscr();
          printf("y=%d",y++);
          getch();
       }

      example2 :-

        #include<stdio.h>
        main()
       {
             int x =10;
             clrscr();
  
             printf("value of x =%d",x++);
             printf("value of x =%d",--x);
             printf("value of x =%d",x--);
             printf("value of x =%d",++x);
             printf("value of x =%d",x++);
             getch();
        }


       example3 :-

       #include<stdio.h>
       main()
      {
       int x =6;
       clrscr();
       printf("%d%d%d",x++,++x,x--);
       getch();
      }
    
     example4 :-

      #include<stdio.h>
      main()
     {
          int i,j =4;
          clrscr();
          i =j++ * ++j * j--;
          printf("\ni=%d,j=%d",i,j);
          getch();
     }



program for arithmetic operators in single step


      Program for arithmetic operators in single step

      #include<stdio.h>
        main()
     {
         int a,b;
         clrscr();
         printf("enter a,b values :%d%d",&a,&b);
         printf("add=%d\n,sub=%d\n,mul=%d\n,div=%d",a+b,
                                                                            a-b,a*b,a/b);
        getch();
     }

      output :-
                     enter a,b values  : 2 4
                     add= 6
                     sub= -2
                     mul= 8
                     div= 0
                     

  

program for arithmetic operators


           Program for Arithmetic Operators


           #include<stdio.h>
              main()
             {
                int a,b,c;
                clrscr();
                printf("enter a,b values=%d%d",&a,&b);
                c=a+b;
                printf("c=%d",c);
                c=a-b;
                printf("c=%d",c);
                c=a*b;
                printf("c=%d",c);
                c=a/b;
                printf("c=%d",c);
                getch();
             }
             output :-
                            
                          enter a,b values= 2 4
                          c=6
                          c=-2
                          c=8
                          c= 0


   
 
 

Tuesday, 4 September 2012

swapped 2 numbers without using 3 variable


       Swapped 2 numbers without using 3 variable

       #include<stdio.h>
       main()
       {
             int a=10,b=20;
             printf("\noriginal values of a=%d,b=%d",a,b);
             a=a^b;
             b=a^b;
             a=a^b;
             printf("\nswapped values of a=%d,b=%d",a,b);
             getch();
       }
     
       output :-

                 original values of a=10,b=20
                 swapped values of a=20,b=10

                 



Monday, 3 September 2012

switch statement program


                        Switch statement program


          #include<stdio.h>
          main()
          {
                  int a,b,c,ch=1;
                  clrscr();
                  printf("enter a,b values\n:");
                  scanf(%d%d",&a,&b);
                  printf("[1]addition\n");
                           printf("[2]subtraction\n");
                  printf("[3]multiplication\n");
                  printf("[4]division\n");
                  printf("[5]exit");
                  printf("enter your choice\n");
                  scanf("%d",&ch);
                  if(ch<=5)
                  {
                  switch(ch)
                  {
                     case 1:
                               c=a+b;
                               printf("c=%d",c);
                               break;
                     case 2:
                               c=a-b;
                               printf("c=%d",c);
                               break;
                     case 3:
                               c=a*b;
                               printf("c=%d",c);
                               break;
                     case 4:
                               c=a/b;
                               printf("c=%d",c);
                               break;
                     case 5:
                               exit;
                   }
                   }
                   else
                   printf("not valid");
                   getch();
            }

        output1 :-
                  enter a,b values: 4 7
                  [1]addition
                           [2]subtraction
                  [3]multiplication
                  [4]division
                  [5]exit
                  enter your choice 1
                  c = 11


      output2 :-
                  enter a,b values: 41 97
                  [1]addition
                           [2]subtraction
                  [3]multiplication
                  [4]division
                  [5]exit
                  enter your choice 22

                   not valid

                          
   




                                
                           
                 

Thursday, 30 August 2012

Reverse floyds triangle


         Reverse floyds triangle :-


       #include<stdio.h>
       main()
      {

       int i,j,k;
       i=7;
       for(j=3;j>=0;j--)
      {
       for(k=0;k<=j;k++)
      {
       printf("%d ",(i+k));
      }
       i=i-j;
       printf("\n");
      }
      getch();

      }

Program to print multiplication table


     Program to print multiplication table :-

#include<stdio.h>
main()
{
 int r,i,j,k; 
 printf("Enter the number range: ");
 scanf("%d",&r);
 for(i=1;i<=r;i++)
{
 for(j=1;j<=10;j++)         
 printf("%d*%d=%d ",i,j,i*j);
 printf("\n");
 }
  return 0;
}

Palindrome number


    Palindrome number:-

 
  #include <stdio.h>
  main()
 {
   int n, n1, rev = 0, rem;
   printf("Enter any number: \n");
   scanf("%d", &n);
   n1 = n;
   while (n > 0)
 {
   rem = n % 10;
   rev = rev * 10 + rem;
   n = n / 10; }
   if (n1 == rev)
 {
   printf("Given number is a palindromic number");
 }
   else
{
 printf("Given number is not a palindromic number");
 }
 return 0;
}

Check prime number up to n


     Check prime number up to n:-

#include<stdio.h>
main()

int num,i,count,n;
printf("Enter max range: ");
scanf("%d",&n);
for(num = 1;num<=n;num++)

count = 0;
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
count++;
break;
}
}
if(count==0 && num!= 1)  
printf("%d ",num);
}
return 0;
}

Check prime number up to 100



   Check prime number up to 100 :-

#include<stdio.h>
main()
{
int n,i,count;
for(n = 1;n<=100;n++)
{
count = 0;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
count++;break;
}
if(count==0 && n!= 1)
printf("%d ",n);
}
return 0;
}
}

program for prime number



        prime number:-


#include<stdio.h>
int main()
{
int n,i,count=0;
printf("Enter a number: ");
scanf("%d",&n);
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
count++;
break;
}
}
if(count==0 && n!= 1)
printf("%d is a prime number",n);
else
printf("%d is not a prime number",n);
return 0;
}

output:-
                Enter a number: 7
                7 is a prime number

program for fibonacci series


  Program for Fibonacci series

#include<stdio.h>
main()
{
   int a,b,c,i;
   clrscr();
   a=0;
   b=1;
   printf("fibonacci series is\n:%d\t%d\t",a,b);
   for(i=0;i<5;i++)
   {
           c=a+b;
           a=b;
           b=c;
           printf("\t%d",c);
   }
   getch();
 }


output :-  0 1 1 2 3 5 8



 

Tuesday, 28 August 2012

a program for "for loop"



              A Program for "for loop"

      Syntax :- 

                     {
                        for( initialization;condition;incrementation/
                                               decrementation)
                        statements;
                     }
     Example :-


                      #include<stdio.h>
                     main()
                     {
                        int i;
                        clrscr();
                        for(i=1;i<=10;i++)
                        printf("%d\n",i)
                        getch();
                     }
     
       output:-
                      1
                      2
                      3
                      4
                      5
                      6
                      7
                      8
                      9
                      10

a program for do-while loop



               A program for do-while loop

                syntax :-

                                        {
                                      initialization;
                                      do
                                      statements;
                                      incrementation/decrementation;
                                      while(condition);
                                   }
              
               Example :-

                                  #include<stdio.h>
                                  main()
                                  {

                                       int i=10;
                                       do
                                       {
                                           printf("%d\n",i);
                                            i=i+1;
                                                    (or)
                                       i++;
                                        }
     
                                        while(i<=15);
                                         return 0;
                                                 getch();                                 
                                 }
                                                     
             output:-
                           10
                           11
                           12
                           13
                           14
                           15