如何使用for循环编写程序 [英] how to write a program using for loop
问题描述
编写一个程序,要求用户输入正整数N.然后程序在屏幕上显示N个星(每行一个星)。您需要使用for循环。
Write a program that asks the user for a positive integer N. The program then displays N stars on the screen (one star per line). You are required to use a for loop.
推荐答案
拿起一本书并学习编程的概念。
它会帮助你你尝试编写程序并学习编程。
Pick up a book and learn the concepts of programming.
It will help you as you try and write programs and learn about programming.
#include <iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int main()
{
int N,i;
system("cls");
cout<<"enter the no of rows to be printed";
cin>>N;
for(i=1;i<=N;i++)
{
cout<<"*\n";
}
getch();
}
请注意,当您使用旧编译器时,只需替换#include< iostream>到#include< iostream.h>并删除使用命名空间std.Here我已经声明了两个变量N和i。变量N被声明并用于根据你的问题询问用户一个正整数。并且变量i用于执行for循环直到用户的条件满足。请注意转义序列\ n非常重要,因为它完全满足您的问题。
Kindly note that when you work on an old compiler just replace #include<iostream> to #include<iostream.h> and remove using namespace std.Here I have declared two variables N and i.The variable N is declared and used to ask the user for a positive integer as per your question.And the variable i is used in the execution of for loop till the user's condition satisfies.Please note the an escape sequence \n is very much important because it satisfies your question completely.
这篇关于如何使用for循环编写程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!