Pages

Tuesday 25 December 2012

PROPERTIES OF A NUMBER


/* Properties Of A Number */
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void main()
 {
  int n,c,sum=0,r,rev,flag=0,chk,i;
  printf("\n Enter the Number      :  ");
  scanf("%d",&n);
  chk=n;
 while(7)
 {
   printf("\n\n 1 Palindrome or Not\n 2 Amstrong or Not\n 3 Prime or Not  \n 4 Strange or Not \n 5 Perfect or Not \n 6 Exit  ");
   printf("\n\nEnter the choice  :  ");
   scanf("%d",&c);
   switch(c)
    {
     case 1:chk=n;
            rev=0;
            while(n>0)
             {
              r=n%10;
              rev=rev*10+r;
              n=n/10;
             }
            if(rev==chk)
             printf("\n\t Number is palindrome ");
            else
             printf("\n\t Number is not palindrome ");
             break;
    case 2:n=chk;
           rev=0;
           while(n!=0)
            {
             r=n%10;
             sum+=(r*r*r);
             n=n/10;
            }
             if(sum==chk)
             printf("\n\t Number is Amstrong");
            else
             printf("\n\t Number is not Amstrong");
             break;
     case 3:i=2;
            while(i<=n/2)
             {
              if(n%i==0)
               {
                flag=1;
                break;
                   }
              i++;
             }
            if(flag==1)
             printf("\n\t Number is not prime");
            else
             printf("\n\t Number is prime");
            break;
     case 4:n=chk;
            sum=0;
            i=0;
            while(n>0)
              {
               n=n/10;
               i++;
              }
            n=chk;
           while(n>0)
            {
             r=n%10;
             sum+=pow(r,i);
             n=n/10;
            }
             if(sum==chk)
             printf("\n\t Number is Strange");
            else
             printf("\n\t Number is not Strange ");
            break;
    case 5:n=chk;
           sum=0;
           for(i=0;i<=n/2;i++)
            {
             r=n%i;
             if(r==0)
              {
               sum+=i;
              }
             }
            if(sum==chk)
              printf("\n\t Number is perfect");
            else
              printf("\n\t Number is not perfect");
            break;
    case 6:exit(0);
    default:printf("Error in choice");
           break;
  }
 }
}

CALCULATOR USING SWITCH CASE



/* Calculator using Switch Case */
#include<stdio.h>
#include<stdlib.h>
void main()
 {
  int a,b,c;
  float s;
  printf("\nEnter two numbers:");
  scanf("%d%d",&a,&b);
  printf(" 1.Addition  \n 2.Subtraction \n  3.multiplication \n  4.Division  \n  5.Exit \n");
  printf("\nEnter the choice:");
  scanf("%d",&c);

     switch(c)
      {
        case 1:printf("The sum is %d",a+b);
                break;
        case 2:printf("The Subtraction is %d",a-b);
                break;
        case 3:printf("The multiplication is %d",a*b);
                break;
        case 4:s=a/b;
                printf("The Division is %f",s);
                break;
        case 5:exit(0);
        default:printf("Invalid Option");
                break;
      }
 }


FACTORIAL OF A NUMBER



/* Factorial Of A Number */
#include<stdio.h>
void main()
 {
  int n,f=1,i=1;
  printf("Enter the number:\n");
  scanf("%d",&n);
    while(i<=n)
     {
      f=f*i;
      i++;
     }
  printf("Factorial of the number is %d",f);
}


REVERSE OF A NUMBER



/* Reverse Of A Number */
#include<stdio.h>
#include<math.h>
void main()
 {
  int n,rev=0,sum=0,r;
  printf("Enter the number");
  scanf("%d",&n);
  while(n!=0)
   {
    r=n%10;
    rev=(rev*10)+r;
    sum=r+sum;
    n=n/10;
   }
  printf("Reverse is %d ",rev);
  printf("Sum of digit is %d",sum);
}




TEMPERATURE CONVERSION


/* Temperature Conversion */
#include<stdio.h>
void main()
 {
  int t,k;
  float f;
  printf("Enter the temperature which you want to covert:\n");
  scanf("%d",&t);
  k=t+273;
  f=(t+32)*5/9;
  printf("The fareheit scale is %f and kelvin scale is %d",f,k);
 }



SIMPLE CALCULATOR



/* simple calculator */
#include<stdio.h>
#include<string.h>
void main()
 {
  int a,b;
  printf("Enter two Numbers:");
  scanf("%d%d",&a,&b);
 /* printf("Enter the operation you need:\n");
  scanf("%c",op);*/


    printf("The sum is %d\n",a+b);

    printf("The Difference is %d\n",a-b);

    printf("The Division is %d\n",a/b);

    printf("The Multiplication is %d\n",a*b);

 }


Saturday 3 November 2012

PALINDROME NUMBERS




#include<stdio.h>

main()
{
   int n, reverse = 0, temp;

   printf("Enter a number to check if it is a palindrome or not\n");
   scanf("%d",&n);

   temp = n;

   while( temp != 0 )
   {
      reverse = reverse * 10;
      reverse = reverse + temp%10;
      temp = temp/10;
   }

   if ( n == reverse )
      printf("%d is a palindrome number.\n", n);
   else
      printf("%d is not a palindrome number.\n", n);

   return 0;
}


MATRIX ADDITION


/* Matrix Addition*/



#include <stdio.h>


main()
{
   int m, n, c, d, first[10][10], second[10][10], sum[10][10];

   printf("Enter the number of rows and columns of matrix ");
   scanf("%d%d", &m, &n);
   printf("Enter the elements of first matrix\n");

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         scanf("%d", &first[c][d]);

   printf("Enter the elements of second matrix\n");

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
            scanf("%d", &second[c][d]);

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         sum[c][d] = first[c][d] + second[c][d];

   printf("Sum of entered matrices:-\n");

   for ( c = 0 ; c < m ; c++ )
   {
      for ( d = 0 ; d < n ; d++ )
         printf("%d\t", sum[c][d]);

      printf("\n");
   }

   return 0;
}






Saturday 15 September 2012

QUADRATIC EQUATION

/* Quadratic Equation*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c,d;
float r1,r2,s;
printf("enter a,b,c");
scanf("%d%d%d",&a,&b,&c);
d=b*b-(4*a*c);
s=sqrt(d);
if(d>=0)
{
r1=(-b+s)/(2*a);
r2=(-b-s)/(2*a);
if(r1==r2)
{
printf("the roots are real and equal\n");
printf("the root is %5.3f\n",r1);
}
else
printf("the roots are real and distinct\n");
printf("the roots are %5.3f and %5.3f\n",r1,r2);
}
else
printf("imaginary roots\n");
}

SUM AND REVERSE OF A NUMBER

/*Sum And Reverse Of A Number*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0,rev=0,r;
printf("enter the number");
scanf("%d",&n);
while(n>0)
{
r=n%10;
rev=(rev*10)+r;
sum=sum+r;
n=n/10;
}
printf("sum is %d",sum);
printf("\n reverse is %d",rev);
getch();
}


FACTORIAL OF A NUMBER


/*Factorial Of A Number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,fact=1,i=1;
printf("enter number");
scanf("%d",&n);
while(i<=n)
{
fact=fact*i;
i++;
}
printf("factorial is %d",fact);
getch();
}

ARITHMETIC OPERATIONS

/*Arithmetic Operations*/ 

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
int ch;
printf("enter the numbers");
scanf("%d%d",&a,&b);
printf("enter your choice");
Printf("1.multiplication,2.division,3.addition,4.subtraction");
scanf("%d",&ch);
if(ch>0)
{
if(ch==1)
{
printf("product=%d",a*b);
}
else
if(ch==2)
{
printf("quotient=%d",a/b);
}
else
if(ch==3)
{
printf("sum=%d",a+b);
}
else
if(ch==4)
{
printf("diff=%d",a-b);
}
else
printf("Invalid choice");
getch();
}
}



AMICABLE NUMBERS



/*Amicable Numbers*/


#include<stdio.h>
#include<conio.h>
void main()
{
int i,r=0,sum=0,sum1=0,a,b,temp,temp1;
printf("Enter first number\n");
scanf("%d",&a);
printf("Enter second number\n");
scanf("%d",&b);
temp=a;
for(i=1;i<=a/2;i++)
{
r=a%i;
if(r==0)
sum+=i;
}
temp=b;
for(i=1;i<=b/2;i++)
{
r=b%i;
if(r==0
sum1+=i;
}
if((sum==temp1)&&(sum==temp))
printf("amicable");
else
printf("not amicable");
getch();
}



Friday 24 August 2012

CHECK BALANCED EXPRESSION USING STACK




/* check balanced  expression using stack */

#include < stdio.h >
#include < conio.h >
#define max 50

void main()
{
                char stk[max],exp[100];
                int top,i;              
                clrscr();
                top = -1;              
                printf("\nEnter an infix expression ");
                gets(exp);          
                for(i=0; exp[i] != '\0'; i++)
                {
                                if( exp[i]=='(' || exp[i] =='[' || exp[i] == '{' )
                                {
                                                top++;
                                                stk[top]= exp[i];
                                }
                                else
                                if ( exp[i] == ')' )
                                {
                                                if( stk[top] == '(' )
                                                top--;
                                }
                                else
                                {
                                                printf("Unbalanced exp");
                                                exit(0);
                                }
                                }
                                else
                                if ( exp[i] == ']' )
                                {
                                if( stk[top] == '[' )
                                                top--;
                                else
                                {
                                                printf("Unbalanced exp");
                                                exit(0);
                                }
                                }
                                else
                                if ( exp[i] == '}' )
                                {
                                if( stk[top] == '{' )
                                                top--;
                                else
                                {
                                                printf("Unbalanced exp");
                                                exit(0);
                                }
                                }
                } // for
                if( top == -1 )
                                printf("Exp is balanced");
                else
                                printf("Exp is not balanced");


AVERAGE OF N NUMBERS


/* Average Of N Numbers */



#include < stdio.h >
#include < conio.h >

void main()
{
                float a[20],sum,avg;
                int n,i;
                clrscr();
                printf("\nEnter n : ");
                scanf("%d", &n);
                printf("\nEnter %d numbers \n",n);
                for(i=0;i < n;i++)
                {
                   scanf("%f", &a[i]);
                }
                sum = 0;
                for(i=0;i < n;i++)
                {
                                sum += a[i];
                }
                avg = sum / n;
                printf("\nEntered %d numbers are \n",n);
                for(i=0;i < n;i++)
                {
                                printf("\n%.2f",a[i]);
                }
                printf("\nThe average of numbers is %f",avg);