"21 days of good habits" phase I-4

Keywords: C

Today's topic:

Enter two non-zero integers a and B on the keyboard. If both a and B are even, the sum of the two numbers will be output. If both a and B are odd, the difference between the two numbers will be output. If a is odd, B is even, the product of the two numbers will be output. If a is even, B is odd, and the A/B result will be output

Input format:
Enter two non-zero integers separated by commas

Output format:
If both a and B are even numbers, output: A+B = sum of two numbers;   If both a and B are odd numbers, output: A-B = difference between the two numbers;   If a is odd and B is even, output: A*B = product of two numbers;   If a is even and B is odd, output: A/B = quotient of two numbers.   If the input data a is 0 or B is 0, there is no output.

#include<stdio.h>
int main()
{
	int a,b,c,d,e,f;
	scanf("%d,%d",&a,&b);
	if(a!=0&&b!=0)
	{
		if(a%2==0&&b%2==0&&a!=0&&b&&b!=0)
			{
			c=a+b;
			printf("%d+%d=%d",a,b,c);	
			}
			else if(a%2!=0&&b%2!=0&&a!=0&&b!=0)
			{
				d=a-b;
				printf("%d-%d=%d",a,b,d);
			}
			else if(a%2==!0&&b%2==0&&a!=0&&b!=0)
			{
				e=a*b;
				printf("%d*%d=%d",a,b,e);
			}
			else
			{
				f=a/b;
				printf("%d/%d=%d",a,b,f);
			}
	}
	return 0;
}

At the beginning, only ab is defined as non-0 in if, and then it is noted that ab is not defined as non-0 in else, which is not in line with the meaning of the question. After scanf, it is directly defined that ab in the whole function is non-0. This code is not perfect. At the beginning, the ab specified in if () is not zero and has not been deleted. You can delete the redundant ab which is not zero to save resources.

17. for loop of loop structure

Form:

Its execution process is as follows:

Step 1: execute expression 1 to initialize the loop variable (give the start value);

Step 2: judge expression 2. If its value is true (not 0), execute the code block in the body of the for loop, and then execute downward; If the value is false (0), the cycle is ended; {if true, continue the loop, if false, push the loop}

Step 3: execute expression 3;

Step 4: execute the second step after executing the code block in the for loop;

Step 5: the cycle ends and the program continues to execute downward.

The execution process diagram is as follows:

Note: after for; Be sure to write

example

Add numbers within 10

#include <stdio.h>

int main()

{

    int sum, num;   // Define variable sum, num

    sum = 0;

    for(num = 0;   num<=10   ;   num++    ) // For loop condition and variation value of num

    {

          sum+=num;    // Calculate the sum between each number, which means sum=sum+num

    }

    Printf ("the sum of numbers within 10 is:% d", sum);

    return 0;

}

In the for loop, expression 1 is one or more assignment statements, which are used to control the initial value of the variable; expression 2 is a relational expression, which determines when to exit the loop; expression 3 is the step value of the loop variable, which defines how the loop variable changes after each loop. These three parts are separated by semicolons (;).

When using the for statement, you should pay attention to:

1. Expressions 1, 2 and 3 in the for loop can be defaulted, but the semicolon (;) cannot be defaulted.

2. Omitting "expression 1 (initial value of cyclic variable)" means that no initial value is assigned to the cyclic variable. For example:

Uploading... Re upload cancel

3. Omit "expression 2 (loop condition)" and do no other processing. The loop is always executed (dead loop). For example:

Uploading... Re upload cancel

4. Omit "expression 3 (loop variable increment)" and do no other processing. The loop is always executed (dead loop). For example:

Uploading... Re upload cancel

Note: the dead loop can be solved by using break, which will be discussed later

5. Expression 1 can be an assignment expression that sets the initial value of a cyclic variable or other expressions, such as:

Uploading... Re upload cancel

6. Expression 1 and expression 3 can be a simple expression or multiple expressions separated by commas. For example:

Uploading... Re upload cancel

The running result is Uploading... Re upload cancel

7. Expression 2 is generally a relational expression or a logical expression, but it can also be a numerical expression or a character expression. As long as its value is non-zero, the loop body is executed.

Uploading... Re upload cancel

8. The variables in each expression must be defined before the for loop. For example:

Uploading... Re upload cancel

Narcissus number example: output all Narcissus numbers. Small knowledge: if you want the number on the hundreds of digits in 512, you can use 512 / 100. If you want the number on the tens of digits in 512, you can use 512 / 10% 10 (512 / 10 = 51% 10), which is the remaining 1. 1 is the number on the tens of digits in 512. It is required that the number on the tens of digits in 512 directly 512% 10 to obtain the remainder 2, and 2 is the single digit.

The so-called "narcissus number" refers to a three digit number, and the sum of each digit cube is equal to this number. For example, 153 is a narcissus number, 153 = 1 * 1 * 1 + 5 * 5 + 3 * 3 * 3.

#include <stdio.h>

int main()
{
	//Define three digit num, single digit sd, ten digit td and hundred digit hd
	int num, sd, td, hd;
	//Loop all three digits
	for( num=100 ; num<1000 ; num++ )
	{
		//Get the number of three digits num hundreds
		hd = num/100 ;
		//Get the three digit num ten digit number
		td = num/10%10 ;
		//Gets the number of three num digits
		sd = num%10 ;
		//What are the conditions for the number of daffodils?
		if(num ==hd*hd*hd+td*td*td+sd*sd*sd )
		{
			printf("Daffodil number:%d\n", num);
		}
	}
	return 0;
}

Other codes:

#include<stdio.h>
int main()
{
	int i;
	for(i=1;i<=10;i=i+1)
	{
		printf("The first%d Copy all over: computer\n",i);
	}
	return 0;
}
#include<stdio.h>
int main()
{
	int number;
	int year;
	scanf("%d%d",&number,&year);//Input items are added to make it more user-friendly
	do
	{
		year=year+1;
		number=number*1.2;
	}while(number<1000);//Don't forget to add a do while loop after while;
	printf("here we are%d The annual recruitment scale exceeded 1000",year);
	return 0;
}
#include<stdio.h>
int main()
{
	int a;//Previous fruit prices
	int b;//Current fruit prices
	scanf("%d,%d",&a,&b);//Small improvements
	printf("Shall we buy fruit");
	printf("%c",a>=b?'y':'n');
	return 0;
}

Posted by irvieto on Tue, 26 Oct 2021 06:29:42 -0700