Thursday, December 29, 2011

Pyramid of numbers

/* A program to print pyramid of numbers */

#include <stdio.h>
int main()
{
    int n, c, k, start = 1, temp;
    printf("Enter number of rows:");
    scanf("%d",&n);
    temp = n;
    for ( c = 1; c <= n; c++ )
    {
        for ( k = temp; k > 1; k-- )
            printf(" ");
        temp--;
        for ( k = 1; k <= 2*c - 1; k++ )
        {
            if ( k <= c)
            {
                 printf("%d", start);
                 if ( k < c )
                 start++;
            }
            else
            {
                start--;
                printf("%d", start);
            }
        }

        start = 1;
        printf("\n");
    }

    return 0;
}


Print Fibonacci series


/* A program to Print Fibonacci Series */

#include<stdio.h>
int main()
{
   int n, term_1 = 0, term_2 = 1, next_term, c;
   printf("Enter the number of terms in Fibonacci series:");
   scanf("%d",&n);
   printf("\nFirst %d terms of fibonacci series are:\n",n);
   for ( c = 0 ; c < n ; c++ )
   {
      if ( c <= 1 )
         next_term = c;
      else
      {
         next_term = term_1 + term_2;
         term_1 = term_2;
         term_2 = next_term;
      }
      printf("%d\n",next_term);
   }
   return 0;
}


Thursday, December 22, 2011

Using Ternary Operator


/* A program to read a number and print -1 if it is negative, 0 if it is zero and 1 if it is positive */

#include<stdio.h>
int main()

{
int a,b;

printf("\n The Program will print :\n 1 if input number is +ve\n 0 if input number is 0\n -1 if input number is -ve\n");
printf("Enter a number:\n");
scanf("%d",&a);

b=a>0?1:(a<0?-1:0);
printf("\nResult = %d",b);

return 0;

}

Circle and a point


/*A program to check whether a given point lies inside the circle, on  the circle or outside the circle */

#include <stdio.h>
#include <math.h>

int main()

{
    int h,k,x,y,r;
    float a;
    printf("enter center (h,k) of a circle\n");
    scanf("%d%d",&h,&k);
    printf("\nenter radius\n");
    scanf("%d",&r);
    printf("\nenter a point to be checked\n");
    scanf("%d%d",&x,&y);
    a=sqrt(pow(x-h,2)+pow(y-k,2));
    if (a<r)
        printf("\npoint (%d, %d) lies inside the circle",x,y);
    else if (a==r)
        printf("\npoint (%d, %d) lies on the circle",x,y);
    else
        printf("\npoint (%d, %d) lies outside the circle",x,y);
    printf(" having centre (%d,%d) and radius %d\n",h,k,r);
    return 0;

}

Monday, December 19, 2011

Pass/fail check


/* A program to check whether a student is pass or fail */

#include <stdio.h>
int main()

{
    int thermo, drawing, chemistry, math, c,total;
    printf("\n Marks in:\n\n");
    printf(" thermo    = ");
    scanf("%d",&thermo);
    printf(" drawing   = ");
    scanf("%d",&drawing);
    printf(" chemistry = ");
    scanf("%d",&chemistry);
    printf(" math      = ");
    scanf("%d",&math);
    printf(" c         = ");
    scanf("%d",&c);
    total=math+thermo+chemistry+drawing+c;
    if(thermo<40 || chemistry<40 || drawing<40 || math<40 || c<40)
        printf("\n Result: FAIL");
    else
        printf("\n Result: PASS");
    printf("\n Percentage:%0.2f\n",total/5.0);
    printf("\n NOTE: Total for each subject is 100\n");
    return 0;
}

Sunday, December 18, 2011

Reversing a number


/*A program to print a given integer in reverse order and sum it with original */

#include<stdio.h>

int main()  /* use 'long' instead of 'int' if integer is a long integer */
{
    int n, reverse = 0,temp;
    printf("Enter a number to reverse\n");
    scanf("%d",&n);
    temp=n;
    while( n != 0 )
   {
      reverse = reverse * 10;
      reverse = reverse + n%10;
      n = n/10;
   }

    printf("Reverse of entered number is %d\n", reverse);
    printf("sum of original number %d and reversed number %d is %d\n",temp,reverse,temp+reverse);
    return 0;
}

Find smallest number


/* C Program to Find Smallest Number out of 4 Number Using Array */

#include <stdio.h>
#include <conio.h>
int main()
{
int a[4],i,low;
printf("Enter 4 Numbers\n\n");
for(i=0;i<4;i++)
scanf("%d",&a[i]);
low=a[0];
for(i=0;i<4;i++)
{
if(a[i]<low)
low=a[i];
}
printf("\nSmallest Number is=%d ",low);
getch();
return 0;
}

Check straight line condition


/* A program to check whether given three points fall on one straight line or not */


#include<stdio.h>


int main()

{
    int x1, x2, x3, y1, y2, y3 ;

printf("\nEnter the Co-ordinates of first point(x1,y1)");
scanf("%d%d",&x1, &y1);

printf("\nEnter the Co-ordinates of 2nd point(x2,y2)");
scanf("%d%d",&x2, &y2);

printf("\nEnter the Co-ordinates of 3rd point(x3,y3)");
scanf("%d%d",&x3, &y3);

if( (y2-y1)/(x2-x1)==(y3-y2)/(x3-x2) )
printf("The three points lie on straight line");

else
printf("The three points do not lie on straight line");

return 0;

}




Sum until zero is pressed

/* A program to numbers until zero is entered. Sum the number and display */
#include <stdio.h>
int main()

{
    int i, sum=0;
    printf("enter number\n");
    scanf("%d",&i);
    while(i!=0)
    {
        sum=sum+i;
        scanf("%d",&i);
    }
    printf("sum is %d",sum);
    return 0;

}

Armstrong Number


/* A program to print all three digit Armstrong number */ 

#include <stdio.h>
#include <math.h>
int main()

{
    int i, rem,arm=0,temp;
    printf("armstrong numbers are:\n");
    for(i=100;i<1000;i++)
    {
        temp=i;
        while(temp!=0)
        {
            rem=temp%10;
            arm=arm+pow(rem,3);
            temp=temp/10;
        }


        if(arm==i)
        printf("%d ",i);
        arm=0;
    }



    return 0;



}




Prime number check

/* A program to test whether a given number is prime or not */

#include <stdio.h>
int main()
{
    int a,i,rem,flag=0;
    printf("enter a number:");
    scanf("%d",&a);
    for(i=2;i<a/2;i++)
    {


        rem=a%i;
        if(rem==0)
        {
            printf("%d is not prime",a);
            flag=1;
            break;

        }
        else
        flag=0;
    }

        if (flag==0)
            printf("%d is prime",a);




return 0;

}

Create a pyramid of stars

/* A program to create a pyramid of stars */ 

#include<stdio.h>

int main()
{
   int row, c, n, temp;

   printf("Enter the number of rows in pyramid of stars you wish to see: ");
   scanf("%d",&n);

   temp = n;

   for ( row = 1 ; row <= n ; row++ )
   {
      for ( c = 1 ; c < temp ; c++ )
        printf(" ");

      temp--;

      for ( c = 1 ; c <= 2*row-1 ; c++ )
         printf("*");

      printf("\n");
   }

   return 0;
}

Check input character: whether it is alphabet or number or special character


#include <stdio.h>
#include <conio.h>
int main()

{
    char x;
    printf("\npress any key\n");
    x=getche();
    if(x>='a' && x<='z' || x>='A' && x<='Z')
        printf("\n%c is alphabet",x);
    else if(x>='0' && x<'9')
        printf("\n%c is number",x);
    else
        printf("\n%c is special character",x);

    return 0;
}

Enter two numbers and an arithmetic operator and do the respective operation


#include <stdio.h>
#include <conio.h>
int main()
{
    char i;
    int a,b;
    printf("enter two numbers\n");
    scanf("%d%d",&a,&b);
    printf("enter arithmetic operator '+' or '-' or '/' or'*'\n");
    i=getche();
    printf("\b");
    if(i=='+')
        printf("%d + %d = %d",a,b,a+b);
    else if(i=='-')
        printf("%d - %d = %d",a,b,a-b);
    else if(i=='*')
        printf("%d * %d = %d",a,b,a*b);
    else if(i=='/')
        printf("%d / %d = %f",a,b,(float)a/b);
    else
        printf("wrong arithmetic operator selected");
    return 0;
}

HCF(GCD) of two input numbers


#include <stdio.h>
int main()
{
    int a,b,c,d,temp,j,hcf;
    printf("enter two numbers\n");
    scanf("%d%d",&a,&b);
    if(a>b)
        temp=a;
    else
        temp=b;
        for (j=1; j<=temp; j++)
        {
            c = a%j;
            d = b%j;
            if(c==0 && d==0)
            hcf=j;
        }

    printf("\nLCM of %d and %d is %d\n",a,b,a*b/hcf);
    return 0;
}

LCM of two entered numbers


#include <stdio.h>
int main()
{
    int a,b,c,d,temp,j,hcf;
    printf("enter two numbers\n");
    scanf("%d%d",&a,&b);
    if(a>b)
        temp=a;
    else
        temp=b;
        for (j=1; j<=temp; j++)
        {
            c = a%j;
            d = b%j;
            if(c==0 && d==0)
            hcf=j;
        }

    printf("\nLCM of %d and %d is %d\n",a,b,a*b/hcf);
    return 0;
}