7.13 – Nested loops – programs to practice part 2

by subbu on October 19, 2013

Specification: Print the following output using a C program

Number triangle two halfs

#include<stdio.h>
int main()
{
int i,j;
for(j=1;j<=5;j++)
{
   for(i=1;i<=5-j;i++)
      printf("     ");    /*5 spaces */
   for(i=1;i<=j;i++)
       printf("%5d",i);
   for(i=j-1;i>=1;i--)
       printf("%5d",i);
printf("\n\n");
}
return 0;
}

Specification: Print the following output using a C program

number triangle two parts 2

#include<stdio.h>
int main()
{
int i,j;
for(j=1;j<=5;j++)
{
    for(i=1;i<=5-j;i++)
       printf("     ");        /*5 spaces */
    for(i=j;i>=1;i--)
       printf("%5d",i);
    for(i=2;i<=j;i++)
       printf("%5d",i);
    printf("\n\n");
}
return 0;
}

Specification: Print the following output using a C program

clip_image003

#include<stdio.h>
int main()
{
int i,j;
for(j=1;j<=5;j++)
{
    for(i=1;i<=5-j;i++)
        printf("     ");
    for(i=1;i<=j;i++)
        printf("%5d",i);
    for(i=j-1;i>=1;i--)
        printf("%5d",i);
    printf("\n\n");
}
for(j=4;j>=1;j--)
{
    for(i=1;i<=5-j;i++)
        printf("     ");
    for(i=1;i<=j;i++)
        printf("%5d",i);
    for(i=j-1;i>=1;i--)
        printf("%5d",i);
    printf("\n\n");
}
return 0;
}

Specification: Print the following output using a C program

clip_image004

#include<stdio.h>
int main()
{
int i,j;
for(j=1;j<=5;j++)
{
     for(i=1;i<=5-j;i++)
         printf("     ");
     for(i=j;i>=1;i--)
         printf("%5d",i);
     for(i=2;i<=j;i++)
         printf("%5d",i);
     printf("\n\n");
}
for(j=4;j>=1;j--)
{
     for(i=1;i<=5-j;i++)
         printf("     ");
     for(i=j;i>=1;i--)
         printf("%5d",i);
     for(i=2;i<=j;i++)
         printf("%5d",i);
     printf("\n\n");
}
return 0;
}

Specification: Print the following output using a C program

clip_image005

#include<stdio.h>
int main()
{
int i,j;
for(j=1;j<=5;j++)
{
    for(i=1;i<=5-j;i++)
        printf("     "); /* 5 spaces */
    for(i=1;i<=j;i++)
        printf("*    "); /* 4 spaces */
    for(i=1;i<=j-1;i++)
        printf("*    ");
    printf("\n\n");
}
for(j=4;j>=1;j--)
{
    for(i=1;i<=5-j;i++)
        printf("     "); /* 5 spaces */
    for(i=1;i<=j;i++)
        printf("*    "); /* 4 spaces */
    for(i=1;i<=j-1;i++)
        printf("*    ");
printf("\n\n");
}
return 0;
}

Specification: Print the following output using a C program

clip_image006
Logic:
Outer loop is used to run the inner loop for “j” number of times.
Inner loop runs the for loop “j” times and prints “j”
Program:

#include<stdio.h>
int main()
{
int i,j;
for(j=1;j<=5;j++)
{
    for(i=1;i<=j;i++)
       printf("%5d",j);
    printf("\n\n");
}
return 0;
}

Specification: Print the following output using a C program.

clip_image007

#include<stdio.h>
int main()
{
int i,j;
for(j=1;j<=5;j++)
{
    for(i=1;i<=5-j;i++)
        printf("     ");   /* 5 spaces */
    for(i=1;i<=j;i++)
        printf("%5d",j);
    for(i=1;i<=j-1;i++)
        printf("%5d",j);
    printf("\n\n");
}
for(j=4;j>=1;j--)
{
    for(i=1;i<=5-j;i++)
        printf("     ");  /* 5 spaces */
    for(i=1;i<=j;i++)
        printf("%5d",j);
    for(i=1;i<=j-1;i++)
        printf("%5d",j);
    printf("\n\n");
}
return 0;
}

Specification: Print the following output using a C program.

clip_image008

#include<stdio.h>
int main()
{
int i,j;
for(j=5;j>=1;j--)
{
   for(i=1;i<=j;i++)
       printf("%5d",i);
   for(i=1;i<=2*(5-j)-1;i++)
       printf("     ");   /* 5 spaces */
   for(i=j;i>=1;i--)
       if(i!=5)
          printf("%5d",i);
   printf("\n\n");
}
return 0;
}

Specification: Print the following output using a C program.

clip_image009

#include<stdio.h>
int main()
{
char i,j;
int k;
for(j='F';j>='A';j--)
{
   for(i='A';i<=j;i++)
       printf("%5c",i);
   for(k=1;k<=2*('F'-j)-1;k++)
       printf("     ");
   for(i=j;i>='A';i--)
       if(i!='F')
          printf("%5c",i);
   printf("\n\n");
}
return 0;
}

Specification: Print the prime numbers from 1 to the given limit

Show Logic

We have already developed the logic to find whether a number is a prime number or not.
clip_image010
Using outer loop we supply numbers from 1 to the specified range one by one, if the supplied number (n) is a prime number then it would be printed otherwise will try for the next number

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

Program:

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

Execution:
Enter the limit:15
2    3    5    7   11   13

Specification: Print the perfect numbers from 1 to the given limit.
Program:

#include<stdio.h>
int main()
{
int i,n,sum,limit;
printf("Enter the limit:");
scanf("%d",&limit);
for(n=1;n<=limit;n++)
{
   for(sum=0,i=1;i<n;i++)
      if(n%i==0)
          sum=sum+i;   /* sum of factors other than the given number */
   if(n==sum)
     printf("%5d",n);
}
return 0;
}

Execution:
Enter the limit: 1000
6        28      496

Specification: Print the Armstrong numbers from 1 to the given limit
Program:

#include<stdio.h>
int main()
{
int n,x,sum,limit;
printf("Enter the limit:");
scanf("%d",&limit);
for(n=1;n<=limit;n++)
{
      x=n;       /* to not disturb the value of n */
      for(sum=0;x!=0;x=x/10)   /* x would be zero after the end of loop */
          sum=sum+(x%10)*(x%10)*(x%10);
      if(sum==n)       /* Comparing with original number */
         printf("%5d",n);
}
return 0;
}

Execution:
Enter the limit: 1000
1        153     370     371     407

Specification: Print the palindrome numbers from 1 to the given limit.

Program:

#include<stdio.h>
int main()
{
int limit,n,x,rev;
printf("Enter the limit:");
scanf("%d",&limit);
for(n=1;n<=limit;n++)
{
   x=n;
   for(rev=0;x!=0;x=x/10)
        rev=rev*10+x%10;
   if(rev==n)
       printf("%5d",n);
}
return 0;
}

Execution:
Enter the limit: 100
1   2    3    4    5    6    7    8    9   11   22   33   44    55   66   77   88   99

Previous post:

Next post: