# Third Week Examples: More Loops, Console Input, Case Construction

Following D. Milicic Second, Third and Fourth Week Examples.

## While Loop

```/************************************************
Treibergs                            Jan 24, 2006
Third Week Example 1: While Loop
hello3.c
************************************************/

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

int
main(void)
{
int i = 1;

while (i <= 10)
{
printf("%d. Hello!\n",i);
i=i+1;
}

return EXIT_SUCCESS;
}

```

## Do - While Loops

```/************************************************
Treibergs                            Jan 24, 2006
Third Week Example 2: Do - While Loop
hello4.c
************************************************/

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

int
main(void)
{
int i = 1;

do
{
printf ("%d. Hello!\n",i);
i=i+1;
}
while (i <= 10);

return EXIT_SUCCESS;
}
```

## Console Input

```/************************************************
Treibergs                            Jan 24, 2006
Third Week Example 3: Console Input Example
evenodd.c
************************************************/

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

int
main(void)
{

int n;

printf ("Input an integer> ");
scanf ("%d", &n);

if (n % 2 != 1)
printf("%d is even.\n",n);

else
printf("%d is odd.\n",n);

return EXIT_SUCCESS;
}
```

```/************************************************
Treibergs                            Jan 24, 2006
Third Week Example 4: Interavtive Summation of
the Alternating Harmonic Series
sum7.c
************************************************/

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

int
main(void)
{

int i,n;
float sum;

sum = 0.0;

printf ("How many terms to sum> ");
scanf ("%d", &n);

for (i = 1; i <= n ; i=i+1)
{
if (i % 2 == 0)
sum = sum - 1.0/i ;

else
sum = sum + 1.0/i;
}

printf("The sum of %d terms is equal to %f\n", n, sum);

return EXIT_SUCCESS;
}

```

## Case Construction

```/************************************************
Treibergs                            Jan 24, 2006
Third Week Example 5: Case Construction
season.c
************************************************/

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

int
main(void)
{
int n;

printf ("Enter n (month 1--12): ");
scanf ("%d",&n);

switch(n)
{
case 1 : case 2 :  case 3 : printf("Winter\n");
break;

case 4 : case 5 : case 6 : printf("Spring\n");
break;

case 7 : case 8 : case 9 : printf("Summer\n");
break;

case 10 : case 11: case 12 : printf("Fall\n");
break;
}

return EXIT_SUCCESS;
}

```

```/************************************************
Treibergs                            Jan 24, 2006
Third Week Example 6: Case Construction to Sum
1/1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ...
sum8.c
************************************************/

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

int
main(void)
{

int i, n;
float sum;

n = 10000;
i = 1;
sum = 0.0;

while (i <= n)
{
switch( i%4 )
{
case 0: case 2:
break ;

case 1:
sum = sum + 1.0/i;
break;

case 3:
sum = sum - 1.0/i;
break;
}
i = i+1;
}

printf("The sum of the series is %f.\n", sum);

return EXIT_SUCCESS;
}

```

```/*  Treibergs      1-25-06

Program to evaluate the Taylor polynomials of
exp(x)  centered at  a

today.c   */

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

int
main(void)
{
int  i, n;
double  x,  a,  u,  term,   sum;

printf( " Taylors polynomials for exp(x)\n\n");
printf(" Enter the center  a  :");
scanf("%lf", &a);
printf(" Enter x :");
scanf("%lf",&x);

n = 20;
sum = 0.0;
term = exp(a);
u = x - a;

i = 0;

printf(" n\t\t term \t\t n th Taylor polynomial evaluated at  x\n");

while( i <=  n)
{
sum = sum + term;
printf(" %4d %21.15f  %21.15f \n", i, term, sum);
i = i + 1;
term = term*u/i;
}

printf(" \t Theoretical sum  =  %21.15f \n", exp(x));

return EXIT_SUCCESS;
}

```