Showing posts with label Functions. Show all posts
Showing posts with label Functions. Show all posts

Friday, September 23, 2011

WAP to swap two numbers using call by reference method


/*
  Name: WAP to swap two numbers using call by reference method
  Author: Parveen Malik
  Date: 23/09/11 11:43
*/


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

void swap(int*,int*);


int main()
{
    int x,y;
    printf("Enter the value of x and y \n");
    scanf("%d%d",&x,&y);
    printf("Before swapping\nx = %d\ny = %d\n",x,y);
    swap(&x,&y);
    printf("After swapping\nx = %d\ny = %d",x,y);
    getch();
    return 0;
}


void swap (int *a, int *b)
{
     int temp;
     temp=*a;
     *a=*b;
     *b=temp;
     }


OUTPUT : 


     

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 find factorial of a given number without using recursion


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


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


long factorial(int);
void main(void)
{
     int num;
     long fact=1;
     printf("Enter a number to calculate its factorial : ");
     scanf("%d",&num);
     printf("%d = %ld \n",num,factorial(num));
     getch();
     }
     long factorial(int n)
     {
          int count;
          long result=1;
          for(count=1;count<=n;count++)
          result=result*count;
          return (result);
          }


OUTPUT:



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 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 :