Sunday 23 April 2023

Exercise 5-To demonstrate use of nested loops

SET A

1. Write a program to display all prime numbers between x and y.

#include<stdio.h>
#include<conio.h>
void main()
{
 int x,y,i,flag,j;
 clrscr();
 printf("\nEnter two Numbers:");
 scanf("%d%d",&x,&y);
 printf("\nPrime numbers between %d and %d is  ",x,y);
 for(i=x;i<y;i++)
    {
     flag=0;
     for(j=2;j<i;j++)
    {
     if(i%j==0)
        flag=1;
    }
     if(flag==0)
       printf("\n%d",i);
    }
 getch();
}

OUTPUT:

Enter two Numbers:1                                                            
20                                                                             
                                                                               
Prime numbers between 1 and 20 is                                              
1                                                                              
2                                                                              
3                                                                              
5                                                                              
7                                                                              
11
13
17
19
2. Write a program to display multiplication tables from ___ to ___ having n multiples each. The
output should be displayed in a tabular format. For example, the multiplication tables of 2 to 9
having 10 multiples each is shown below.
2 ´ 1 = 2 3 ´ 1 = 3 ………….9 ´ 1 = 9
2 ´ 2 = 4 3 ´ 2 = 6…………..9 ´ 2 = 18
…………. ………….
2 ´ 10 = 20 3 ´ 10 = 30………..9 ´ 10 = 90


#include<stdio.h>
#include<conio.h>
void main()
{
 int i,j,x,y;
 clrscr();
 printf("\nEnter two Numbers:");
 scanf("%d%d",&x,&y);

 for(i=1;i<=10;i++)
    {

     for(j=x;j<=y;j++)
    {
     printf("\t%dx%d=%d",j,i,j*i);
    }
     printf("\n");
    }



 getch();
}

OUTPUT:

Enter two Numbers:                                                             
2                                                                              
9                                                                              
        2x1=2   3x1=3   4x1=4   5x1=5   6x1=6   7x1=7   8x1=8   9x1=9          
        2x2=4   3x2=6   4x2=8   5x2=10  6x2=12  7x2=14  8x2=16  9x2=18         
        2x3=6   3x3=9   4x3=12  5x3=15  6x3=18  7x3=21  8x3=24  9x3=27         
        2x4=8   3x4=12  4x4=16  5x4=20  6x4=24  7x4=28  8x4=32  9x4=36         
        2x5=10  3x5=15  4x5=20  5x5=25  6x5=30  7x5=35  8x5=40  9x5=45         
        2x6=12  3x6=18  4x6=24  5x6=30  6x6=36  7x6=42  8x6=48  9x6=54         
        2x7=14  3x7=21  4x7=28  5x7=35  6x7=42  7x7=49  8x7=56  9x7=63         
        2x8=16  3x8=24  4x8=32  5x8=40  6x8=48  7x8=56  8x8=64  9x8=72         
        2x9=18  3x9=27  4x9=36  5x9=45  6x9=54  7x9=63  8x9=72  9x9=81         
        2x10=20 3x10=30 4x10=40 5x10=50 6x10=60 7x10=70 8x10=80 9x10=90        
                  3. Modify the sample program 1 to display n lines as follows (here n=4).
A B C D
E F G
H I
J
 

#include<stdio.h>
#include<conio.h>
void main()
{
 int n,i,j,a;
 clrscr();
 printf("\nEnter Number:");
 scanf("%d",&n);
 a=65;
 for(i=0;i<n;i++)
    {
     printf("\n");
     for(j=0;j<n-i;j++)
    {

     printf("\t%c",a);
     a=a+1;
    }
    }
 getch();
}

OUTPUT:

Enter Number:4                                                                 
                                                                               
        A       B       C       D                                              
        E       F       G                                                      
        H       I                                                              
        J                                                                      
                                                                               
SET B

1. Write a program to display all Armstrong numbers between 1 and 500. (An Armstrong
number is a number such that the sum of cube of digits = number itself Ex. 153 = 1*1*1 + 5*5*5
+ 3*3*3
 
        


#include<stdio.h>
#include<conio.h>
void main()
{
 int ans=0,i,d,x;
 clrscr();
 printf("\nArmstrong between 1 to 500 is:");
 for(i=1;i<=500;i++)
    {
     x=i;
     ans=0;
     while(x>0)
      {
       d=x%10;
       ans=ans+(d*d*d);
       x=x/10;
      }
     if(i==ans)
       printf("\n %d",i);
    }

 getch();
}

OUTPUT:

Armstrong between 1 to 500 is:                                                 
 1                                                                             
 153                                                                           
 370                                                                           
 371                                                                           
 407                                                                           

 3. Display all perfect numbers below 500. [A perfect number is a number, such that the sum of
its factors is equal to the number itself]. Example: 6 (1 + 2 + 3), 28 (1+2+4+7+14) 
   
                                                                    
                                                                               
  #include<stdio.h>
#include<conio.h>
void main()
{
 int n,fact,sum,i,d,j;
 clrscr();
 printf("\nPerfect No upto 500 is:");
 for(i=1;i<500;i++)
    {
     sum=0;
     for(j=1;j<i;j++)
    {
     if(i%j==0)
       {
        sum=sum+j;
       }
    }
     if(sum==i)
    printf("\n%d",i);
    }

 getch();
}

OUTPUT:


Perfect No upto 500 is:                                                        
6                                                                              
28                                                                             
496                                                                            
                                                                         

No comments:

Post a Comment

Questions

  Exercise 1 Set A . Apply all the three program development steps for the following examples.  1. Accept dimensions of a cylinder and p...