Showing posts with label if else. Show all posts
Showing posts with label if else. Show all posts

Friday, September 23, 2011

WAP to find factorial of a given number using recursion


/*
  Name: WAP to find factorial of a given number using recursion
  Author: Parveen Malik
  Date: 23/09/11 10:25
*/


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


long factorial(int);


main()
{
   int num;
   long f;

   printf("Enter a number to find its factorial :");
   scanf("%d",&num); 

   if(num<0)
      printf("Negative numbers are not allowed");
   else
   {
      f = factorial(num);
      printf("%d!=%ld",num,f);
   }
   getch();
   return(0);
}

long factorial(int n)
{
   if(n==0)
      return(1);
   else
      return(n*factorial(n-1));
}

OUTPUT:


WAP to check if it is a leap year


/*
  Name: WAP to check Leap Year
  Author: Parveen Malik
  Date: 23/09/11 09:39
*/


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


int main()
{
int year;
printf("Enter a year to check if it is a leap year \n");
scanf("%d",&year);


if(year%400==0)
printf("%d is a leap year .\n",year);
else if(year%100==0)
printf("%d is not a leap year.\n",year);
else if(year%4==0)
printf("%d is a leap year.\n",year);
else
printf("%d is not a leap year.\n",year);
getch();
return 0;
}


OUTPUT : 



WAP to check whether entered number is prime or not



/*
  Name: WAP to check whether entered number is prime or not
  Author: Parveen Malik
  Date: 23/09/11 02:28
*/


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


main()
{
   int n, c;


   printf("Enter a number\n");
   scanf("%d", &n);


   if ( n == 2 )
      printf("Prime number.\n");
   else
   {
       for ( c = 3 ; c <= n - 1 ; c++ )
       {
           if ( n %c == 0 )
              break;
       }
       if ( c != n )
          printf("Not prime.\n");
       else
          printf("Prime number.\n");
   }
   getch();
   return 0;
}


OUTPUT : 





WAP to accessing command line arguments


/*
  Name: WAP to accessing command line arguments 
  Author: Parveen Malik
  Date: 23/09/11 02:09
*/


#include <stdio.h>
#include <conio.h>
#define plural_text(n) &"s"[(1 == (n))]
#define plural_text2(n) &"es"[(1 == (n)) << 1]


main(int argc, char *argv[])
{
      int i, n = argc - 1;


      printf("You passed %d argument%s on the command line.",
            n, plural_text(n));


      if (argc > 1)
      {
            puts(" They are:");
            for (i = 1; i < argc; ++i)
            {
                  printf("\nArgument #%d:\n  Text: \"%s\"\n  Value: %d\n",
                        i, argv[i], atoi(argv[i]));
            }
      }
      else  putchar('\n');
      getch();
      return 0;
}


OUTPUT :



WAP to find first n numbers of the fibonacci series


/*
  Name: WAP to find first n numbers of the fibonacci series
  Copyright: Paul Griffiths 2000
  Author: Parveen Malik
  Date: 23/09/11 01:56
  Description: Finds the first 'n' numbers in the Fibonacci sequence.
  'n' must be supplied as the sole command line argument,
  and must be between 3 and 47.
*/


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


int ParseCmdLine(int argc, char *argv[]);


int main(int argc, char * argv[]) {
    unsigned long f1 = 1, f2 = 1;
    int temp, i = 3;
    int n = ParseCmdLine(argc, argv);


    printf("First %d numbers in the Fibonacci sequence:\n\n", n);


    printf("%11lu%11lu", f1, f2);


    for ( i = 3; i <= n; ++i ) {
        temp = f2;
        f2 += f1;
        f1 = temp;


        printf("%11lu", f2);


        if ( i % 5 == 0 )
            putchar('\n');
    }
    putchar('\n');
    return EXIT_SUCCESS;
}




/*  Returns the integer specified on the command line  */


int ParseCmdLine(int argc, char *argv[]) {
    int n;
    char * endptr;


    if ( argc < 2 ) {
        fprintf(stderr, "You must supply an argument\n");
        exit(EXIT_FAILURE);
    }
    else if ( argc > 2 ) {
        fprintf(stderr, "You must only supply one argument\n");
        exit(EXIT_FAILURE);
    }


    n = strtol(argv[1], &endptr, 0);
    if ( *endptr ) {
        fprintf(stderr, "You must supply a whole number as an argument\n");
        exit(EXIT_FAILURE);
    }


    if ( n < 3 ) {
        fprintf(stderr, "You must supply a number greater than 2\n");
        exit(EXIT_FAILURE);
    }
    else if ( n > 47 ) {
        fprintf(stderr, "You must supply a number less than 48\n");
        exit(EXIT_FAILURE);
    }


    return n;


}

WAP to find first 1000 prime numbers


/*
  Name: WAP to find first 1000 prime numbers 
  Copyright:  Copyright Paul Griffiths 2000
  Author:  Parveen Malik
  Date: 23/09/11 01:37


*/


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




int prime(int number, int * primes);


int main(void) {
    int primes[998] = { 3, 0 };
    int n = 5, i;
    int count = 0, found;


    printf("%8d%8d%8d", 1, 2, 3);               /*  Print first 3 primes     */




    /*  Find the next 997  */


    while ( count < 997 ) {
        i = 0;
        found = 1;




        /*  Test if number divides by any of the primes already found  */


        while ( primes[i] ) {
            if ( (n % primes[i++]) == 0 ) {   /*  If it does...              */
                found = 0;                    /*  ...then it isn't prime...  */
                break;                        /*  ...so stop looking         */
            }
        }


        if ( found ) {
            printf("%8d", n);                /*  If it's prime, print it...  */
            primes[i] =  n;                  /*  ...and add it to the list   */
            ++count;
        
        
            /*  Start a new line every 8 primes found  */
        
            if ( ((count + 3) % 8) == 0 )
                putchar('\n');
        }


        n += 2;     /*  There's no point testing even numbers, so skip them  */
    }


    putchar('\n');
getch();
    return EXIT_SUCCESS;
}


OUTPUT : 


Sunday, September 18, 2011

WAP to print all Armstrong numbers between 1 to 1000


/*
  Name: WAP to print all Armstrong numbers between 1 to 1000
  Author: Parveen Malik
  Date: 18/09/11 10:45
*/


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


int main()
{
int no, temp, rem, sum;


printf("Armstrong numbers between 1 and 1000 are:\n");
for(no=1; no<=1000; no++)
{
temp=no;
sum=0;
while(temp>0)
{
rem=temp%10;
sum=sum+(rem*rem*rem);
temp=temp/10;
}
if(no==sum)
printf("\n%d", no);
}
getch( );
return 0;
}
    
OUTPUT: 



WAP to check whether the entered number is positive,negative or zero


/*
  Name: WAP to check whether the entered number is positive,negative or zero
  Author: Parveen Malik 
  Date: 18/09/11 09:52
*/


#include<stdio.h>
#include<conio.h>
 int main()
 {
      long num;
      printf("\n\n\nEnter any number : ");
      scanf("%ld",&num);
      if(num>0)
      printf("\n%d is a positive number ",num);
      else if(num<0)
      printf("\n%d is a negative number ",num);
      else
      printf("\n%d is zero ",num);
      main();
      return 0;
      }


OUTPUT:


      

WAP to displays the position or index in the string S where the string T begins, or -1 if S doesn't contain T.


/*
  Name: Write a C program to displays the position or index in the string S where the string T begins, or -1 if S doesn't contain T. */
/*
  Author: Parveen Malik
  Date: 18/09/11 08:58
*/


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


int main(void)
{
    char str[10],tr[10];
    int i,j,pos;
    int flag=0;
    int count=0;
    printf("Enter the main string : ");
    gets(str);
    printf("Enter the substring : ");
    gets(tr);
    
    for(i=0;str[i]!='\0';i++)
    {
                             if(str[i]==str[0])
                             {
                                               pos=i;
                                               for(j=0;tr[j]!='\0';j++)
                                               {
                                                                       if(str[i]==tr[j])
                                                                       count++;
                                                                       i++;
                                                                       }
                                               flag=1;
                                               break;
                                               }
                                               }
                                               if(flag==1&&count==strlen(tr))
                                               printf("\nPostion :%d, and T is a substring of S",pos+1);
                                               else if(flag==1&&count<strlen(tr))
                                               printf("\nPosition : %d, and T is not a substring of S",pos+1);
                                               else
                                               printf("\nIndex : -1");
                                               getch();
                                               }

OUTPUT: 

WAP to print binary triangle



/*
  Name: WAP to print binary triangle 
  Author: Parveen Malik
  Date: 18/09/11 05:40
*/


#include<stdio.h>
int main()

    int i,j,row,mod,temp=1;
char inputstr[35];  /* make longer than reasonable input will expected */
                   /* error checking should still be employed */
                   /* to clear buffer overflow, if input is too long */
                   
while ( temp==1)
{
      row=0;
      while(row<=0)
      {
                   printf("\n Enter the number of rows you want : ");
                   fflush(stdout); /* printf didn't have newline, need to flush */
                   fgets(inputstr,34,stdin);
                             /* error checking should go here for buffer overflow */
                             
       row = atoi (inputstr);
       if(row<=0)
       printf("invalid input, try again \n");
       }
      for ( i=0;i<=row-1;i++)
      {  
          mod=i%2;
          for(j=0;j<=i;j++)
          {
                           printf("%d",mod);
                           if(mod==0)
                           mod=1;
                           else 
                           mod=0;
                           }
                           printf("\n");
                          
                                                  }
                                                     }
                                                     
                                                     return 0;
                                                     }



OUTPUT: 


WAP to find the smallest of 10 numbers using for loop


/*
  Name: WAP to find the smallest of 10 numbers using for loop 
  Author: Parveen Malik
 Date : 18/09/11 03:21


*/


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


int main(void)
{
    int a[15],min,i;
    printf("Enter any ten numbers \n");
    for(i=0;i<10;i++)
    {
                     scanf("%d",&a[i]);
                     if(i==0)
                      min=a[i];
                     
    if( a[i]<min)
    min=a[i];
}
printf("\nMinimum : %d",min);


getch();
return 0;
}

OUTPUT : 


WAP to find the largest of 10 numbers using for loop


/*
  Name: WAP to find the largest of 10 numbers using for loop 
  Author: Parveen Malik
  Date: 18/09/11 02:58 
*/


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


int main(void)
{
    int a[15],max,i;
    printf("Enter any ten numbers \n");
    for(i=0;i<10;i++)
    {
                     scanf("%d",&a[i]);
                     if(i==0)
                      max=a[i];
                     
    if( a[i]>max)
    max=a[i];
}
printf("\nMaximum : %d",max);


getch();
return 0;
}


OUTPUT : 



WAP to print square star pattern


/*
  Name: WAP to print square star pattern 
  Author: Parveen Malik
  Date: 18/09/11 02:12
*/


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


static void print(int length, int width)
{
        int j;
        int i;


        length--;
        width--;
        for(j = 0; j <= width; j++) {
                for(i = 0; i <= length; i++) {
                        if(!i || !j || j == width || i == length)
                                putchar('s');
                        else
                                putchar('*');
                }
                putchar('\n');
        }
}


int main(void)
{
        print(6, 4);
        getch();
        return 0;
}

OUTPUT : 

WAP to multiply two matrix


/*
  Name: WAP to Multiply two Matrix
  Author: Parveen Malik
  Date: 18/09/11 01:50 
*/


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


int main()
{
    float a[10][10], b[10][10],c[10][10];
    int m,n,p,q,i,j,k;
    printf("Enter the size of Matrix A as m,n : ");
    scanf("%d%d",&m,&n);
    printf("Enter the size of Matrix B as p,q : ");
    scanf("%d%d",&p,&q);
    
    if(n!=p)
    {
            printf("\n Matrix Multipication AxB is not feasible\n");
            exit(1);
            }
            printf("\n Enter the %d elements of Matrix A row-wise \n",m*n);
            
            for(i=0;i<m;i++)
            {
                            for(j=0;j<n;j++)
                            {
                                            scanf("%f",&a[i][j]);
                                            }
                                            }
            printf("\n Enter the %d elements of Matrix B row-wise \n",p*q);
            for(i=0;i<p;i++)
            {
                            for(j=0;j<q;j++)
                            {
                                            scanf("%f",&b[i][j]);
                                            }
                                            }
            for(i=0;i<m;i++)
            {
                            for(j=0;j<q;j++)
                            {
                                            c[i][j]=0;
                                            for(k=0;k<n;k++)
                                            {
            c[i][j]+=a[i][k]*b[k][j];
                                            }
                                            }
                                            }
    printf("\n Product AxB is : \n\n\n");
    for(i=0;i<m;i++)
{
    for(j=0;j<q;j++)
    {
                    
            printf("%5.2f\t",c[i][j]);
            }
            printf("\n");
            }
            
            getch();
            return 0;
            }
            
            OUTPUT : 


                                     

WAP to generate prime numbers


/*
  Name: WAP to generate the prime numbers 
  Author: Parveen Malik
  Date: 18/09/11 01:14
*/


#include<stdio.h>
#include<conio.h>
int main(void)
{
    int i,num,x,flag;
    printf("Enter the value upto which prime numbers is to be generated : ");
    scanf("%d",&num);
    for(i=1;i<=num;i++)
    {
                       x=2;
                       flag=0;
                       while(x<=i/2)
                       {
                                    if(i%x==0)
                                    {
                                              flag=1;
                                              break;
                                              }
                                              x++;
                                              }
                                              if(flag==0)
                                              printf("%5d",i);
                                              }
                                              getch();
                                              return 0;
                                              }


OUTPUT : 



Wednesday, January 5, 2011

Program to find out roots of a quadratic equation

/*  Name: Program to find out roots of a quadratic equation

Author: Parveen Malik

Date: 05/01/11 21:00*/


#include<stdio.h>

#include<conio.h>

#include<math.h>
void main()

{

int a,b,c;

float disc; // Discriminant

float root1,root2;

printf("Enter the value of a,b and c respectively\n");

printf("a = ");

scanf("%d",&a);

printf("b = ");

scanf("%d",&b);

printf("c = ");

scanf("%d",&c);

disc=b*b-4*a*c;

root1=-b+sqrt(disc)/2*a;

root2=-b-sqrt(disc)/2*a;

if(disc==0)

{

printf("Roots are real and equal\n");

printf("First Root :%.2f = Second Root: %.2f\n",root1,root2);

}

else if(disc<0)

{

printf("Roots are imaginary");

printf("First root = %.2f\n",root1);

printf("Second root = %.2f\n",root2);

}

else

{

printf("Roots are real and distinct\n");

printf("First root = %.2f\n",root1);

printf("Second root = %.2f\n",root2);

}

getch();

}

[caption id="attachment_82" align="aligncenter" width="655" caption="OUTPUT"]Program to find out roots of a quadratic equation[/caption]