Classic examples of branch and loop statements

Posted by jollyjumper on Sat, 30 Oct 2021 10:13:46 +0200

1, Write code to output three integers from large to small

  Method 1

#include<stdio.h>
 int main()
{
	int a = 0;
	int b = 0;
	int c = 0;
	scanf("%d %d %d", &a, &b, &c);
	if (a > b && b > c)
		printf("%d %d %d", a, b, c);
	else if (a > c && c > b)
		printf("%d %d %d", a, c, b);
	else if (b > a && a > c)
		printf("%d %d %d", b, a, c);
	else if (b > c && c > a)
		printf("%d %d %d", b, c ,a);
	else if (c> a && a > b)
		printf("%d %d %d", c, a, b);
	else 
		printf("%d %d %d", c, b, a);
	return 0;
}

This code conceives six cases of three integer size sorting, and uses the if else statement to solve the size sorting.

2, Write a code to print all multiples of 3 between 1-100

#include<stdio.h>
int main()
{
	int a = 0;
	for (a = 3; a < 100; a++)
	{
		if(a%3==0)
			printf("%d ", a);
	}
	
	return 0;
  •   This code uses for loop and if statement.

3, Given two numbers, find the greatest common divisor of the two numbers

 

#include<stdio.h>
int main()
{
	int a = 0;
	int b = 0;
	int c = 0;
	scanf("%d %d", &a, &b);
	if (a > b)
		c = b;
	else
		c = a;

	while (1)
	{
		if (a % c == 0 && b % c == 0)
		{
			printf("%d\n", c);
			break;
		}
			c--;
	}

	return 0;
}

  1. First, you need to use the if statement to find the minimum value of the two numbers
  2. Use the while and if statements to find the maximum common divisor;

3, Print leap years between 1000 and 2000

  Leap year condition: if N can be divided by 4 and cannot be divided by 100, it is a leap year; Or: N can be divided by 400, which is also a leap year

#include<stdio.h>
int main()
{
	int a = 0;
	for (a = 1000; a <= 2000; a++)
	{
		if ((a % 4 == 0 && a % 100 !=0)|| a % 400==0)
		{
			printf("%d ", a);
		}
	}
	
	return 0;

}

&&And, both need to be

||Means or, either party can be established.

The operation code is as follows:

4, Write a code: print prime numbers between 100 and 200

#include<stdio.h>
int main()
{
	int a = 0;
	int b = 0;
	int count = 0;
	for (a = 100; a <=200; a++)
	{
		for (b = 2; b <= a; b++)
		{
			if (a % b == 0)
			{
				break;
			}
		}
			if (a ==b)
			{
				count++;
				printf("%d ", a);
			}
		
	}
	printf("%d ", count);
	return 0;
}
  1.   Prime: that is, prime. If there is no divisor other than 1 and itself, the data is prime
  2. The two for loops in the code mean; Divide each number in a (100 ~ 200) by the number in b(2~a):
  • If there is a number that can be divisible (a%b == 0), end the loop (break)
  • If it all loops to the case of a=b and has not been divided, then the number 'a' and 'B' are prime numbers.
  • The operation results are as follows:

 

5, Write a program to count the number 9 in all integers from 1 to 100

#include<stdio.h>
int main()
{
	int a = 0;
	int count = 0;
	for (a = 1; a <= 100; a++)
	{
		if (a % 10 == 9)
		{
			count++;
		}
		if (a / 10 == 9)
		{
			count++;
		}
	}
	printf("%d ", count);
	return 0;

}
  •   If (a% 10 = = 9) solves 9 19 29
  • if (a / 10 == 9) solves 90 91 92
  • 99 both conditions are met, a number count+2

The operation results are as follows:

6, Given the number of seconds, convert seconds into hours, minutes and seconds  

#include<stdio.h>
int main()
{
	int t = 0;
	int h = 0;
	int min = 0;
	int s = 0;
	scanf("%d", &t);
	h = t / 3600;
	min = t % 3600 / 60;
		s = t % 3600 % 30;
	printf("%d\n", h);
	printf("%d\n", min);
	printf("%d\n", s);
	return 0;

}

  Input 3661s, output 1 hour, 1 minute and 1 second.

 

  7, Enter the scores (integers) of 5 students from the keyboard to find their average score (floating point number, keep one decimal place).

#include<stdio.h>
int main()
{
	int i = 0;
    int  s = 0;
	int input = 0;
	for (i = 1; i <= 5; i++)
	{
		scanf("%d", &input);
		s = (s+input);
	}
	printf("%.1f", s/5.0);
	return 0;
}

 

  •   The for loop plus scanf statement simplifies the input code.
  • printf("%.1f", s/5.0); Represents the number of float type input with one decimal place reserved.

The operation results are as follows:

  8, Reverse output a four digit number

 

#include<stdio.h>
int main()
{
	int a = 0;
	scanf("%d", &a);
	while (a)
	{
		printf("%d", a % 10);
		a = a / 10;

	}
	return 0;
}
  • A number is first taken from 10, and the first result is the last number in a series of books;
  • In divide by 10, because shaping can only take an integer, it will delete the last number in a string of numbers;
  • Repeat this operation to obtain the reverse order of this string of numbers.

  The operation code is as follows:

 

Topics: C++