Some practice questions about loops

1. Write a program; create an array of 26 elements, store 26 lowercase letters in it, and then print the contents of the array

result:
insert image description here

Code:

#include<stdio.h>
int main()
{
    
    
	char arr[26] = {
    
     0 };
	int a = 0;

	for (a = 0; a < 26; a++)
	{
    
    
		arr[a] = 'a'+a;
		printf("%c\t", arr[a]);
	}
	return 0;
}
//方法二:
//#include<stdio.h>
//int main()
//{
    
    
//	char alphabet[26] = { 0 };
//	int i = 0;
//	char ch = 'a';
//
//	for (i = 0; i < 26; i++,ch++)
//		alphabet[i] = ch;
//
//	for (i = 0; i < 26; i++, ch++)
//		printf("%c", alphabet[i]);
//
//	return 0;
//}

2. Use nested loops to print the format

result:

insert image description here
Code:

#include<stdio.h>
int main()
{
    
    
	char ch = '$';
	int a = 0;
	int b = 0;

	for (a = 0; a < 6; a++)
	{
    
    
		for (b = 0; b <= a; b++)
		{
    
    
			printf("%c", ch);
		}
		printf("\n");
	}
	return 0;
}

3. Use nested loops to print characters

result:
insert image description here

Code:

#include<stdio.h>
int main()
{
    
    
	char ch = 'A';
	int a = 0;
	int b = 0;

	for (a = 0; a < 6; a++)
	{
    
    
		for (b = 0; b <= a; b++)
		{
    
    
			printf("%c", ch+b);
		}
		printf("\n");
	}

	return 0;
}

4. Printing with Nested Loops

A
BC
DEF
GHIJ
KLMNO
PQRSTU
RESULTS:
insert image description here

Code:

#include<stdio.h>
int main()
{
	char ch = 'A';
	int a = 0;
	int b = 0;
	int d = 0;

	for (a = 0; a < 6; a++)
	{
		for (b = 0; b <= a; b++,d++)
		{
			printf("%c", ch+d);
		}
		printf("\n");
	}
	return 0;
}

5. Write a program to type capital letters

Format:
A
ABA
CBABA
Result:
insert image description here
Code:

#include<stdio.h>
int main()
{
    
    
	int a = 0;
	char ch = 'Q';
	char ch2 = 'A';
	scanf("%c", &ch);
	int b = ch - 'A' ;
	int c = 0;

	for (a = 0; a < 5; a++)
	{
    
    
		for (c = 0; c < b; c++)
		{
    
    
			printf(" ");

		}
		b--;

		for (ch2 = 'A', c = 0; c <= a; c++)
		{
    
    
			printf("%c", ch2++);
		}

		for (c = 0; c < a; c++)
		{
    
    
			printf("%c", ch2--);
		}
		printf("\n");




	}
	return 0;
}

6. Write a program to print a table, each line prints an integer, the square of the number, the cube of the number, and the user is required to input the upper and lower limits of the table. for loop

Result:
insert image description here
code:

#include<stdio.h>
int main()
{
    
    
	int a = 0;
	int b = 0;
	printf("请输入下限:");
	scanf("%d", &a);

	printf("请输入上限:");
	scanf("%d", &b);

	for (; a <= b; a++)
	{
    
    
		printf("%d\t%d\t%d\t", a, a * a, a * a * a);
		printf("\n");
	}
	return 0;
}

7 Write a program that requires the user to input two floating-point numbers, and prints the result of dividing the difference between the two numbers by the product of the two numbers. Before the user enters a non-number, the program loops through each pair of values ​​entered by the user

Result:
insert image description here
code:

#include<stdio.h>
int main()
{
    
    

	float x = 0;
	float y = 0;
	while (scanf("%f %f", &x, &y) == 2)
	{
    
    
		printf("the anwser is %f\n", (x - y) / (x * y));
		printf("please enter two float data");
	}

	return 0;
}

Modification 7: Use a function to calculate the returned result

#include<stdio.h>
float calc(float x, float y)
{
    
    
	float result = (x - y) / (x * y);
	return result;
}

int main()

{
    
    
	float a = 0;
	float b = 0;
	while (scanf("%f %f", &a, &b) == 2)
	{
    
    
		printf("the answer is %f\n", calc(a, b));
		printf("please enter two float data");

	}
	return 0;
}

8. Write a program that reads eight integers into the array, and then reads the eight integers in reverse order

Result:
insert image description here
code:

#include<stdio.h>

int main()
{
    
    
	int a = 0;
	int arr[20] = {
    
     0 };
	for (a = 0; a < 8; a++)
	{
    
    
		scanf("%d", &arr[a]);

	}

	for (a = 0; a < 8; a++)
	{
    
    
		printf("%d  ", arr[7 - a]);

	}
	return 0;
}



Guess you like

Origin blog.csdn.net/cainiaochufa2021/article/details/121049500