Learn C programming in 11 days

Day 4 loops

Simple loop

 

/*********************************************************/

/*  loop1.cpp - VC5.0                                    */

/*  Loop with for                                        */

/*  J Luo, 14-Mar-2001                                   */

/*********************************************************/

 

#include <iostream.h>

#include <stdio.h>

#include<math.h>

 

void main()

{

    int s,n;

    double t, pi;

    s=1;

    t=1;

    pi=0;

    n=1;

    while(fabs(t)>=1e-6)

    {

       pi=pi+t;

       s=-s;

       n=n+2;

       t=s*1.0/n;

    }

    pi=pi*4;

    printf("pi=%f\n",pi);

}

 

Using for loop

 

/*********************************************************/

/*  pi1.cpp - VC5.0                                      */

/*  Using for loop                                       */

/*  J Luo, 14-Mar-2001                                   */

/*********************************************************/

 

#include <iostream.h>

#include <stdio.h>

#include<math.h>

 

void main()

{

    int n, sign;

    float term, sum;

 

    sum=0;

    sign=1;

    for (n=1; n<=999; n=n+2)

    {

       term = (float)sign/n;

       sum = sum + term;

       sign = -sign;

    }

 

    printf("pi=%f\n",sum*4);

}

 

Another approach

             

/*********************************************************/

/*  pi1.cpp - VC5.0                                      */

/*  Using for loop                                       */

/*  J Luo, 14-Mar-2001                                   */

/*********************************************************/

 

#include <iostream.h>

#include <stdio.h>

#include<math.h>

 

void main()

{

    int n, sign;

    float term, sum;

 

    sum=0;

    sign=1;

    n = 0;

    while ( fabs(term) > 0.000001 )

    {

       term = sign/(float)pow(3,n)/(2*n+1);

       sum = sum + term;

       sign = -sign;

       n = n + 1;

    }

 

    printf("n=%d\n", n-1);

    printf("pi=%f\n", sum*2*sqrt(3));

}

 

Another equation

             

/*********************************************************/

/*  pi2.cpp - VC5.0                                      */

/*  Using for loop                                       */

/*  pi = 2sqrt(1-1/(3^1x3) + 1/(3^2x5) - 1/(3^3x7 + ...  */

/*  J Luo, 14-Mar-2001                                   */

/*********************************************************/

 

#include <iostream.h>

#include <stdio.h>

#include<math.h>

 

void main()

{

    int n, sign;

    int a, b;

    float term, sum;

 

    sign=1;

    a = 1;

    b = 1;

    n = 0;

    sum=0;

    do

    {

       term = sign/float(a*b);

       sum = sum + term;

       sign = -sign;

       a = a*3;

       b = b + 2;

       n = n + 1;

    } while ( fabs(term) > 0.000001 );

 

    printf("n=%d\n", n-1);

    printf("pi=%f\n", sum*2*sqrt(3));

}

 

Prime numbers

 

/*********************************************************/

/*  prime.cpp - VC6.0                                    */

/*  Check if it is a prime                               */

/*  With for loop                                        */

/*  J Luo, 14-Mar-2001                                   */

/*********************************************************/

 

#include <stdio.h>

 

void main()

{

    int m;

    int prime;

 

    printf("Enter an integer: ");

    scanf("%d", &m);

 

    prime = 1;

    for (int i=2; i<m; i++)

       if ( m%i==0 )

       {

           prime = 0;

           break;

       }

 

    if ( prime )

       printf("%d is a prime.\n", m);

    else

       printf("%d is NOT a prime.\n", m);

}

 

Using while loop

 

/*********************************************************/

/*  prime.cpp - VC6.0                                    */

/*  Check if it is a prime                               */

/*  With while loop                                      */

/*  J Luo, 14-Mar-2001                                   */

/*********************************************************/

 

#include <stdio.h>

 

void main()

{

    int m;

    int i;

    int prime;

 

    printf("Enter an integer: ");

    scanf("%d", &m);

 

    prime = 1;

    i = 2;

    while ( i < m )

    {

       if ( m%i == 0 )

       {

           prime = 0;

           break;

       }

       else

           i = i + 1;

    }

 

    if ( prime )

       printf("%d is a prime.\n", m);

    else

       printf("%d is NOT a prime.\n", m);

}

 

Using do while loop

 

/*********************************************************/

/*  prime.cpp - VC6.0                                    */

/*  Check if it is a prime                               */

/*  With do while loop                                   */

/*  J Luo, 14-Mar-2001                                   */

/*********************************************************/

 

#include <stdio.h>

#include <math.h>

 

void main()

{

    int m;

    int i, n;

    int prime;

 

    printf("Enter an integer: ");

    scanf("%d", &m);

 

    n = sqrt(m);

    prime = 1;

    i = 2;

    do 

    {

       if ( m%i == 0 )

       {

           prime = 0;

           break;

       }

       else

           i = i + 1;

    } while ( i < n );

 

    if ( prime )

       printf("%d is a prime.\n", m);

    else

       printf("%d is NOT a prime.\n", m);

}

 

Find primes between 100-200

 

/*********************************************************/

/*  prime100-200.cpp - VC6.0                             */

/*  Check if it is a prime                               */

/*  With do while loop                                   */

/*  J Luo, 14-Mar-2001                                   */

/*********************************************************/

 

#include <stdio.h>

#include <math.h>

 

void main()

{

    int m;

    int i, n;

    int prime;

 

    printf("primes between 101 and 199: \n");

   

    for (m=101; m<=199; m++)

    {

       n = (int) sqrt(m);

       prime = 1;

       for (i=2; i<n; i++)  

       {

           if ( m%i == 0 )

           {

              prime = 0;

              break;

           }

       }

 

       if ( prime ) printf("%d\t", m);

    }

    printf("\n");

}