Here I begin with solutions to the questions given in The For Loop #1.
#Pro1 : Prime Numbers between 1 to 100.
for(int j= 2;j<=100;j++) /* Process is same as for check a prime number with some minor changes*/
{
int flag = 0;
for(int i = 2;i<j;i++)
{
if(j % i == 0)
{
flag = 1;
break;
}
}
if(flag == 0)
printf("%d ",j );
}
int n = 10; /* Say I want to print first 10 numbers in series */
int a = 0;
int b = 1;
int sum = 0;
printf("%d %d ", a, b);
for(int i = 0;i <10;i++)
{
sum = a + b; /* Here we find what will be the next number */
a = b; /* Than we update a by b*/
b = sum; /*And than b by sum to get the next number*/
printf("%d ", sum);
}
/*Process is same as fibonacci series except here I use three variables a, b and c. */
int n = 10; /* Say I want to print first 10 numbers in series */
int a = 0;
int b = 1;
int c = 1;
int sum = 0;
printf("%d %d %d", a, b, c);
for(int i = 0;i <10;i++)
{
sum = a+b+c;
a = b;
b = c;
c = sum;
printf("%d ", sum);
}
for(int i = 1;i<=10;i++)
{
int fact = 1;
for(int j = 1;j<=i;j++)
{
fact = fact * j;
}
printf("% d ", fact);
}
{
if(j % i == 0)
{
flag = 1;
break;
}
}
if(flag == 0)
printf("%d ",j );
}
#Pro2 : Fibonacci Series.
int n = 10; /* Say I want to print first 10 numbers in series */
int a = 0;
int b = 1;
int sum = 0;
printf("%d %d ", a, b);
for(int i = 0;i <10;i++)
{
sum = a + b; /* Here we find what will be the next number */
a = b; /* Than we update a by b*/
b = sum; /*And than b by sum to get the next number*/
printf("%d ", sum);
}
#Pro3 : Tribonacci Series.
/*Process is same as fibonacci series except here I use three variables a, b and c. */int n = 10; /* Say I want to print first 10 numbers in series */
int a = 0;
int b = 1;
int c = 1;
int sum = 0;
printf("%d %d %d", a, b, c);
for(int i = 0;i <10;i++)
{
sum = a+b+c;
a = b;
b = c;
c = sum;
printf("%d ", sum);
}
#Pro 4 : Factorials of all numbers between 1 to 10
/* Here I have used one more for loop so that We may find factorial of all numbers */for(int i = 1;i<=10;i++)
{
int fact = 1;
for(int j = 1;j<=i;j++)
{
fact = fact * j;
}
printf("% d ", fact);
}
the blog is very useful
ReplyDelete