Wednesday, 1 May 2019

ch-3 Loop

loop

#include<stdio.h>
 main()
 {
int a,fact=1;;
printf("enter any number");
scanf("%d",&a);
for(int i=1;i<=a;i++)
fact=fact*i;
printf("%d",fact);

}

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


}

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

#include<stdio.h>
 main()
 {
 int  sum=0;
 for(int i=0;i<=5;i++)
 sum=sum+i;
 printf("%d",sum);


}

 #include<stdio.h>
 main()
 {
 int a,d=12345,m;
 m=d%10;
d=d/10;

printf("m=%d\nd=%d\n",m,d);
}

#include<stdio.h>
main()
{

 int d=5476,r,di,s=0;
  while(d!=0)
  {
  r=d%10 ;
  d=d/10;
  s=s+r ;

  }

printf("%d",s);
}

#include<stdio.h>
main()
{

char c='a';

for(int i=0;i<=4;i++)
{
for(int j=0;j<=i;j++)
{
 printf("%c\t",c);

 }
 printf("\n");

++c ;
}
}

pyramid
1.
#include <stdio.h>
int main()
{
int i, j, rows, k=0;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=1; i<=rows; i++, k=0)
{
  for(j=1; j<=rows-i; j++)
  {
printf("  ");
  }
  while(k != 2*i-1)
  {
printf("* ");
k++;
  }
  printf("\n");
}

return 0;
}

2.
#include <stdio.h>
int main()
{
int i, j, rows, k=0;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=5; i>=1; i--, k=0)
{
  for(j=1; j<=rows-i; j++)
  {
printf("  ");
  }
  while(k != 2*i-1)
  {
printf("* ");
k++;
  }
  printf("\n");
}

return 0;
}

3.  #include <stdio.h>
int main()
{
int i, j, rows, k=0;
printf("Enter number of rows: ");
scanf("%d",&rows);
  for(i=1; i<=rows; i++, k=0)
{
  for(j=1; j<=rows-i; j++)
  {
printf("  ");
  }
  while(k != 2*i-1)
  {
printf("* ");
k++;
  }
  printf("\n");
}


 for(i=rows; i>=1; i--, k=0)
{
  for(j=1; j<=rows-i; j++)
  {
printf("  ");
  }
  while(k != 2*i-1)
  {
printf("* ");
k++;
  }
  printf("\n");
}

return 0;
}


MCQ:
1. main()
 { 
int i=1; 
while() 

printf(“%d”,i++); 
if(i>10)
 break; 

}
a) The condition in while loop is must
b) There should be a semicolon in the while loop 
c) The while loop should be replaced by for loop 
d) No error

ans a) The condition in while loop is must Break statement can be used to exit a loop. TRUE 

main()
 {
 int i=1; 
while() 

printf(“%d”,i++);
 if(i>10)
 break;
 }
 }

a) The condition in while loop is must 
b) There should be a semicolon in the while loop
c) The while loop should be replaced by for loop
d) No error

A programming construct in which a set of statement in a computer program can be executed repeatedly.
 i. Loop statement
 ii. Conditional Statement
 iii. Block Statement
 iv. All of the above 

 i. Loop statement

1.8 What will be the output of following program? 
int main()
 { 
for(int c=1;c<5;++c); 
printf(“%d”,c); 

a) 1 
b) 5 
c) 6 
d) 12345

b) 5

How many times is a do while loop guaranteed to loop? 
a) 0
 b) Infinitely 
c) 1 
d) Variable

c) 1

The following code for(;;) represents as infinite loop. It can be terminated by
 A) Break 
B) Exit(0); 
C) Abort(); 
D) All of the mentioned. 

Which of the following is not an iterative statement? 
A) switch 
B) while 
C) do while 
D) for  

How many times is a do while loop guaranteed to loop? 
A) 0 
B) Infinitely 
C) 1 
D) Variable  

How many times the printf statement within the while loop will be executed?
 x=1; 
while(x=0) 
 printf("hello");
 A) 1 
B) 0 
C) infinite 
D) 2 

true/false

1. The following block of code sums up an input series of grades (terminated by -1) 
while (grade !=-1); 
total=total+grade; 
counter=counter+1; 
printf ”enter grade, -1 to end:”); 
scanf(“%d”,&grade);

Fill in the blanks:
1. ____infinite_____ loop can be terminated using break statement.

2. ____dowhile_____ is an exit control loop

3. In do…while loop, _semicolon_______ symbol is required after condition.
4. The _____(B) continue_________ statement is used to skip a part of the statement in a loop.



Question:

Draw a flow chart to print the factorials of numbers from 1 to n where n is entered by user.

Draw a flowchart to find the fibionacci series till term <=10000

Write a program to find the sum of the digits of a number

Write a C Program to display following pattern: 
 A 
 BB
 CCC 
 DDDD 
 EEEEE

Write a C program to find whether a number is palindrome or not?

 Write a program to print all the Krishnamurti number from 1 to n. Here, n is user dependent. A Krishnamurti number is a number whose sum of factorial of individual digits equals the number. For example, 145 = 1! + 4! + 5! = 1 + 24+ 120 = 145.

true/false


1.0 is an example of double constant and float constant. 

No comments:

Post a Comment

Index

ch-1 Introduction