You are on page 1of 6

MACHINE PROBLEMS

4.1 Write a code for the following display using for loops.

a. 9 b. 117 c. 1
12 104 1
15 91 2
18 78 4
21 65 7
24 52 11
27 39 16
30 26 22
33 13 29
36 0 37

4.2 Design a code for the following display using while loops.

a. 14 b. 2 c. 1
13 6 1
12 12 2
11 20 3
10 30 5
9 42 8
8 56 13
7 72 21
6 90 34
5 110 55

4.3 Create a code for the following display using do-while loops.

a. 100 b. 6 c. 64
82 16 32
66 25 16
52 30 8
40 33 9
30 40 27
22 46 81
16 51 243
12 55 729
10 58 2187
4.4 Build a code for the following display using nested loops.

a. ***** b. *
**** **
*** ***
** ****
* *****

c. 54321 d. 12345
5432 1234
543 123
54 12
5 1

4.5 Using nested loops make a program that will do the following displays.

a. 1 b. 2
2 3
3 4
2 3
4 4
6 5
3 4
6 5
9 6

4.6 Create a C program that will display “I will pass PRGLF” 10 times using for loops.

4.7 Design a program using C that input 20 integers and display the sum using for
loops.

4.8 Write a code that will loop by inputting any integer and displays whether the
integer is “POSITIVE” or “NEGATIVE”, and terminate the loop if the integer is
equal to zero using while loops.

4.9 Construct a C program that will terminate the loop if the entered password is
“CORRECT”, using do-while.

4.10 Create a program that will loop by inputting any character, and terminate the loop
if “PRGLF” are entered consecutively. (using do-while).

4.11 Build a program that will display all prime numbers from 1 to 1000 using looping.

4.12 Design a program that will input integer and display it in reverse using looping.
SOLUTIONS

4.1.a 4.1.b 4.1.c

int a; int b; int c,d;


main() main() main()
{ { {
for (a=9; a<=36; a=a+3) for (b=117; b>=0; b=b-13) for (c=1; c<=37; c=c+d++)
printf("%d\n",a); printf("%d\n",b); printf("%d\n",c);
getch(); getch(); getch();
} } }

4.2.a 4.2.b 4.2.c

int a; int b,c; int a=1,b=0;


main() main() main()
{ { {
a=14; b=2; while (a<=55)
while (a>=5) c=4; {
{ while (b<=110) printf("%d\n", a);
printf("%d\n",a); { a=a+b;
a=a-1; printf("%d\n",b); b=a-b;
} b=b+c; }
getch(); c=c+2; getch();
} } }
getch();
}
4.3.a 4.3.b 4.3.c

int n,t; int a,b; int i,j;


main() main() main()
{ { {
n=100; a=6; i=64;
t=18; b=10; do
do do {
{ { printf("%d\n", i);
printf("%d\n",n); printf("%d\n",a); i=i/2;
n=n-t; a=a+b; }
t=t-2; b=b-1; while (i>=8);
} } {
while (t>=0); while(a<=60); j=9;
getch(); getch(); do
} } {
printf("%d\n", j);
j=j*3;
}
while (j<=2187);
}
getch();
}

4.4.a 4.4b getch();


}
int a,b; int i,j,k;
main() main() 4.4c
{ {
for (a=1; a<=5; a++) for (i=1; i<=5; i++) int a,b;
{ { main()
for (b=5; b>=a; b--) printf("\n"); {
printf(" * ", a); for (j=5; j>=i; j--) for (a=1; a<=5; a++)
printf("\n"); printf(" "); {
} { for (b=5; b>=a; b--)
getch(); for (k=1; k<=i; k++) printf("%d",b);
} printf("\n");
printf("*"); }
} getch();
printf(" \n"); }
}
4.4d 4.5a 4.5b

int a,b; int i,j,k,h; int i,j,k;


main() main() main()
{ { {
for (a=5; a>=1; a--)
{ for (i=1;i<=3;i++) for (i=1; i<=3; i++)
for (b=1; b<=a; b++) printf("%d\n", i); {
printf("%d",b); getch(); for (j=1; j<=3; j++)
printf("\n"); } {
} k=i+j;
getch(); printf("%d\n", k);
} }

}
getch();
}

4.6 4.7 4.8


int a;
int a; int n,num,b; main()
main() main() {
{ { while(a!=0)
for (a=1; a<=10; a++) {
printf("\t\t\tI WILL PASS b=0; printf("Enter a number:
PRGLF\n\n"); for (n=1; n<=20; n++) ");
{ scanf("%d",&a);
getch(); printf("Enter number: if(a>0)
} "); printf("POSITIVE\n");
scanf("%d", &num); else if (a<0)
b=b+num; printf("NEGATIVE\n");
} else
printf("\nThe sum break;
is %d", b); }
getch(); getch();
} }
4.9 4.10 4.11

char pw[10],pw1[10]; char cha[10],cha1[10]; int i,j;


main() main() main()
{ { {
do do for(i=1;i<=1000;i++)
{ { {
printf("Enter printf("Enter for(j=2;j<=i-1;j++)
Password: "); Character: "); if(i%j==0)break;
scanf("%s", &pw); scanf("%s", if(i==j) printf("\t%d",i);
&cha); }
strcpy(pw1,"CORRECT"); getch();
} strcpy(cha1,"PRGLF"); }
while }
(strcmp(pw,pw1)!=0); while 4.12
getch(); (strcmp(cha,cha1)!=0);
} { int a,b;
do main()
{ {
printf("Enter printf("Enter a number:
Character: "); ");
scanf("%s", scanf("%d", &a);
&cha); while (a!=0)
{
strcpy(cha1,"PRGLF"); b=a%10;
} printf("%d" ,b);
while a=a/10;
(strcmp(cha,cha1)!=0); }
} getch();
getch(); }
}

You might also like