Loop control instructions Flashcards

1
Q

what are different loop control instructions

A

for
while
do while

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

For loop

A

include<stdio.h></stdio.h>

for ( initialisation ; condition ; updation ){
//statement
}

int main(){
for (int i=1;i<=5;i+=1){
printf(“hello world\n”);
}
}

where i stands for iterator or counter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

i++ and ++i

A

i++ similar to i+=1 or i=i+1 increment the value after (post increment operator)
++i increment the value before using the value (pre increment operator)

similarly we have i– and –i (post and pre decrement)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

character increment -print characters from a to z using character increment

A

include<stdio.h></stdio.h>

int main(){
for (char ch=’a’;ch<=’z’;ch++){
printf(“%c\n”,ch);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Infinite loop using for loop

A

include<stdio.h></stdio.h>

int main(){
for (int i=1;;i++){
printf(“hello\n”);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

While loop

A

include<stdio.h></stdio.h>

while (condition) {
// statement
}

int main() {
int i=1;
while (i<=5)
{
printf(“hello\n”);
i++;
}
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Do while loop

A

include<stdio.h></stdio.h>

do{
//do something
} while (condition);

do first then check condition hence the loop will run 1 time no matter what

int main() {
int i=1;
do {
printf(“%d\n”,i);
i++;
} while (i<=5);

return 0; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

sum of first n natural numbers

A

include<stdio.h></stdio.h>

int main() {
int i,n,sum;
printf(“enter n :”);
scanf(“%d”,&n);
i=n;
while (i>=1)
{
sum+=i;
i–;
}
printf(“sum is %d”,sum);

}

this is done in reverse

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

find the sum of n natural number and print these number in reverse order at the same time using while loop and for loop seperately

A

include<stdio.h></stdio.h>

int main() {
int i=1,n,sum,j;
printf(“enter n :”);
scanf(“%d”,&n);
j=n;
while (i<=n && j>=1)
{
sum+=i;
i++;
printf(“%d\n”,j);
j–;
}
printf(“sum is %d”,sum);

}

int main() {
int n,sum=0;
printf(“enter n :”);
scanf(“%d”,&n);
for(int i=1,j=n;i<=n && j>=1;i++,j–){
sum+=i;
printf(“%d\n”,j);
}
printf(“sum is %d”,sum);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

break in nested loops

A

if we use break inside a nested loop then it exits from all the loops

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

continue statement

A

skip to next iteration

int main() {
for(int i=1;i<=5;i++) {
if(i==3){
continue;
}
printf(“%d\n”,i);
}
return 0;
}

int main() {
int i = 1;
while (i <= 5) {
if (i == 3) {
i++;
continue;
}
printf(“%d\n”, i);
i++;
}
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Factorial of a number

A

include<stdio.h></stdio.h>

int main() {
int n,factorial=1;
printf(“enter n :\n”);
scanf(“%d”,&n);

for(int i=1;i<=n;i++) {
    factorial*=i;
}
printf("factorial :%d",factorial);
return 0;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

print mn matrix of “

A

include<stdio.h></stdio.h>

int main() {
int rows,column,i,j;
char ch=’*’;
printf(“rows :”);
scanf(“%d”,&rows);
printf(“columns :”);
scanf(“%d”,&column);

for(i=1;i<=rows;i++){
    for (j=1;j<=column;j++){
        printf("%c",ch);
    }
    printf("\n");
}
return 0;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

check for prime

A

include <stdio.h></stdio.h>

int main() {
int n, i, is_prime = 1;
printf(“Enter a positive integer: “);
scanf(“%d”, &n);

if (n <= 1) {
    is_prime = 0;
} else {
    for (i = 2; i <= n / 2; i++) {
        if (n % i == 0) {
            is_prime = 0;
            break;
        }
    }
}

if (is_prime == 1) {
    printf("%d is a prime number\n", n);
} else {
    printf("%d is not a prime number\n", n);
}

return 0; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

prime numbers between a range

A

include <stdio.h></stdio.h>

int main() {
int start, end, i, j, is_prime;

printf("Enter the start of the range: ");
scanf("%d", &start);
printf("Enter the end of the range: ");
scanf("%d", &end);

printf("Prime numbers between %d and %d are:\n", start, end);

// Check each number in the range
for (i = start; i <= end; i++) {
    // Assume the current number is prime
    is_prime = 1;

    // Check if the current number is divisible by any number from 2 to i/2
    for (j = 2; j <= i/2; j++) {
        if (i % j == 0) {
            is_prime = 0;
            break;
        }
    }

    // If the current number is prime, print it
    if (is_prime == 1 && i != 1) {
        printf("%d ", i);
    }
}

return 0; }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly