7.7 – loops – programs to practice Part-1

by subbu on October 9, 2013

Specification: Print the natural numbers from 1 to n (Specified limit)

Show Logic

printing 1 to n natural numbers
Here we take a loop driver variable i, which must be started from 1

i=1;

The value of i must be printed and incremented by 1 for every iteration

printf("\t%d",i);
i++;

The loop must run as long as the value of i is less than or equal to n. Where n is the limit given by the user

i<=n

Putting all together in a while loop

scanf("%d",&n);  /* limit to be given by the user */
i=1;
while(i<=n)
{
   printf("\t%d",i);
   i++;
}

The same loop can be written using for loop as

for(i=1;i<=n;i++)
   printf("\t%d",i);

The same loop can be written using null statement as

for(i=1;i<=n;printf("\t%d",i),i++);

Program:

#include<stdio.h>
int main()
{
int n,i;
printf("Enter the limit:");
scanf("%d",&n);
for(i=1;i<=n;i++)
  printf("\t%d",i);
return 0;
}

Execution:
Enter the limit: 4
1        2        3        4

Specification: Print the natural numbers from n to 1 (natural numbers in reverse order)

Show Logic

n to 1 natural numbers
Here we accept the limit from the user into n

scanf("%d",&n);

We take a driver variable i, must be started from n

i=n;

The value of i must be printed and decremented by 1 for every iteration

printf("\t%d",i);
      i--;

The loop must continue as long as the value of i is greater than or equal to 1

i>=1

Putting all together in a while loop

scanf("%d",&n);
i=n;
while(i>=1)
{
  printf("\t%d",i);
  i--;
}

The same loop can be written using for loop as

for(i=n;i>=1;i--)
      printf("\t%d",i);

The same loop can be written using null statement as

for(i=n;i>=1;printf("\t%d",i),i--);

Program:

#include<stdio.h>
int main()
{
int n,i;
printf("Enter the limit:");
scanf("%d",&n);
for(i=n;i>=1;i--)
printf("\t%d",i);
return 0;
}

Execution:
Enter the limit: 4
4        3        2        1

Specification: Print the even numbers from 1 to n

Show Logic

Here we use for(i=1;i<=n;i++) loop to generate 1 to n natural numbers

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

For every iteration, we check whether i is an even or not using the condition if(i%2==0).
If the condition is true then we print the value of i otherwise skips from printing and go to next iteration

for(i=1;i<=n;i++)
{
    if(i%2==0)
       printf("\t%d",i);
}

Program:

#include<stdio.h>
int main()
{
int n,i;
printf("Enter the limit:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
   if(i%2==0)
      printf("\t%d",i);
}
return 0;
}

Execution:
Enter the limit: 6
2        4        6

Specification: Print the sum of natural numbers from 1 to n

Show Logic

Here we use the loop for(i=1;i<=n;i++) to generate natural numbers from 1 to n

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

We add the value of i to result variable sum for every iteration. So that sum will have sum of natural numbers on completion of loop.
Make sure that sum must be initialized with zero otherwise sum is stored with the garbage value, adding any thing to the garbage will be garbage

for(sum=0,i=1;i<=n;i++)
   sum=sum+i;

The same loop can be written using arithmetic assigning operator as

for(sum=0,i=1;i<=n;i++)
     sum+=i;

The same loop can be written with null statement as

for(sum=0,i=1;i<=n;sum+=i,i++);

Program:

#include<stdio.h>
int main()
{
int n,i,sum;
printf("Enter the limit:");
scanf("%d",&n);
for(sum=0,i=1;i<=n;i++)
        sum=sum+i;
printf("Sum of natural numbers %d",sum);
return 0;
}

Execution:
Enter the limit:5
Sum of natural numbers 15

Specification: Print the sum of even and odd numbers from 1 to n

Show Logic

Here we use the loop for(i=1;i<=n;i++) to generate natural numbers from 1 to n

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

We check whether i is an even or odd using the condition if(i%2==0). If the condition is true then i is added to even sum (esum) otherwise added to odd sum (osum).
Both esum and osum are initialized with 0 at the beginning of loop.

esum=0;osum=0;
for(i=1;i<=n;i++)
{
   if(i%2==0)
       esum=esum+i;
   else
       osum=osum+i;
}

The same loop can be written as

for(esum=osum=0,i=1;i<=n;i++)
{
    if(i%2==0)
        esum+=i;
    else
        osum+=i;
}

Program:

#include<stdio.h>
int main()
{
int n,i,esum,osum;
printf("Enter the limit:");
scanf("%d",&n);
for(esum=osum=0,i=1;i<=n;i++)
{
     if(i%2==0)
        esum+=i;
     else
        osum+=i;
}
printf("Even numbers sum %d",esum);
printf("\nOdd numbers sum %d",osum);
return 0;
}

Execution:
Enter the limit:10
Even numbers sum 30
Odd numbers sum 25

Specification: Accept an integer and print its factorial.

Show Logic

factorial of a number
Here we use the loop for(i=1;i<=n;i++) to generate natural numbers from 1 to n

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

We multiply i to the result variable fact for every iteration. At the end of loop fact will have the factorial of number.
fact must be initialized with 1 but, not with 0 because multiplying any thing with 0 results 0.

fact=1;
for(i=1;i<=n;i++)
     fact=fact*i;

The same loop can be written with null statement as

for(fact=i=1;i<=n;fact*=i,i++);

Program:

#include<stdio.h>
int main()
{
long fact;
int n,i;
printf("Enter a number:");
scanf("%d",&n);
for(fact=i=1;i<=n;i++)
    fact=fact*i;
printf("Factorial of %d is %ld",n,fact);
return 0;
}

Execution:
Enter a number:8
Factorial of 8 is 40320

Specification: Accept the table number and print the table up to 20 terms

Show Logic

Let us take i as driver variable and tn to take the table number.
We generate 1 to 20 terms using a loop for(i=1;i<=20;i++)

for(i=1;i<=20;i++)
         i

We multiply the table number tn with the term i to produce result of each term and will continue for 20 terms using the loop.

for(i=1;i<=20;i++)
         tn*i

To print the table in proper format we need to print the table number, term and the product of table number and term for every iteration

for(i=1;i<=20;i++)
    printf("\n%dx%d=%d",tn,i,tn*i);

printing required table

Program:

#include<stdio.h>
int main()
{
int tn,i;
printf("Enter the table number:");
scanf("%d",&tn);
for(i=1;i<=20;i++)       /* To print table for 20 terms */
   printf("\n%dx%d=%d",tn,i,tn*i);
return 0;
}

Execution:
Enter the table number:7
7×1=7
7×2=14
7×3=21
———-

Specification: Accept the base, exponent and print the result of expression

Show Logic

If we want to calculate 3 power 8 then we multiply 3 for 8 times, that is we need to multiply base to the result variable for exponent number of times.
We run the loop for exponent number of times using the loop

for(i=1;i<=exp;i++)

We multiply the base to the result variable for exponent number of times using the loop. The result variable has the value of base power exponent on completion of the loop.

for(res=1,i=1;i<=exp;i++)
       res=res*base;

The same loop can be written using null statement as

for(res=i=1;i<=n;res*=base,i++);

Program:

#include<stdio.h>
int main()
{
unsigned long res;
int base,exp,i;
printf("Base:");
scanf("%d",&base);
printf("Exponent:");
scanf("%d",&exp);
for(res=1,i=1;i<=exp;i++)
        res=res*i;
printf("Result %lu",res);
return 0;
}

Execution:
Base:2
Exponent:16
Result 2004189184

Specification: Print the Fibonacci series up to n terms.

Show Logic

Fibonacci series starts from 0  1, the sum of last two terms would be the next term
printing fibonacci series up to n terms
Here a is assigned with 0, b is assigned with 1 and c is assigned with 0.
To print each term, we first print the value of c, a is assigned with b, b is assigned with c and c is assigned with a+b.

printf("%5d",c);
a=b;
b=c;
c=a+b;

To print n number of terms, we run the same code for n number of times using for(i=1;i<=n;i++) loop

a=0;b=1;c=0;
for(i=1;i<=n;i++)
{
    printf("%5d",c);
    a=b;
    b=c;
    c=a+b;
}

The same loop can be written using null statement as

for(a=0,b=1,c=0,i=1;i<=n;printf("%5d",c),a=b,b=c,c=a+b,i++);

Program:

#include<stdio.h>
int main()
{
int a,b,c;
int n,i;
printf("How many terms?");
scanf("%d",&n);
a=0;b=1;c=0;
for(i=1;i<=n;i++)
{
   printf("%5d",c);
   a=b;
   b=c;
   c=a+b;
}
return 0;
}

Execution:
How many terms?5
0    1    1    2    3

Previous post:

Next post: