6. Loop
6. Loop
Tutorial – 6
Subject : PPS
Class : B. Tech. Sem.I (EC)
for(i=5;i>=1;i--)
{
for(j=0;j< i;j++)
printf("%c ",(charVal+j));
printf("\n");
}
}
(5) #include <stdio.h>
void main()
{
int cnt=1;
while(cnt>=10)
{
printf("%d,",cnt);
cnt+=1;
}
printf("\nAfter loop cnt=%d",cnt);
printf("\n");
}
(6) #include <stdio.h>
#define TRUE 1
int main()
{
int loop=10;
while(printf("Hello ") && loop--);
}
(7) #include <stdio.h>
int main()
{
int i=1;
while(i<=10 && 1++)
printf("Hello");
}
(8) int main()
{
int max = 5;
inti = 0;
for(;;)
{
i++;
if(i> max)
break;
printf("i = %d\n",i);
}
return 0;
}
(9) #include <stdlib.h>
#include <stdio.h>
enum {false, true};
int main()
{
int i = 1;
do
{
printf("%d\n", i);
i++;
if (i < 15)
continue;
} while (false);
getchar();
return 0;
}
Q.4 Do as directed.
(1) Given a number, write a program using while loop to reverse the digits of the number.
(2) Write a program to compute the sum of the digits of a given number.
(3) The numbers in the sequence1 1 2 3 5 8 13 21 …………………….are called
Fibonacci numbers. Write a program using do while loop to calculate and print the
first m Fibonacci numbers.
(4) Write a program to print the following outputs using for loops
a) 1 b) *
22 **
333 ***
(5) Write a program to compute the value of Euler’s number that is used as the base of
natural logarithms. Use the following formula.
e= 1+ 1/1! +1 /2! + 1/3+……………. 1/n!
(6) Write a program to print all integers that are not divisible by either 2 or 3 and lie
between 1 and 100. The program should also account for the number of such integers
and print
the result.
* * *