1. 编写一个程序打印一个表,表的每一行都给出一个整数,它的平方以及它的立方。要求用户输入表的上限与下限。使用for循环。
#include <iostream>
#include<math.h>
using namespace std;
void funtiongetin(int &a,int &b);
int main() {
int a,b;
int &h=a;
int &k=b;
funtiongetin(h,k);
cout<<"*****************************"<<endl;
cout<<" NO square cubic"<<endl;
for(int i=a;i <= b;i++)
{
cout<<" "<<i<<" "<< pow(i,2)<<" "<<pow(i,3)<<endl;
}
cout<<"*****************************"<<endl;
system("pause");
return 0;
}
void funtiongetin(int &a,int &b)
{
cout<<"this program is to calculate the squire and the cubic of the input NO"<<endl;
cout<<"please input the lower band and the high band of the NO. you want to count"<<endl;
cin>>a>>b;
}
2. 编写一个程序,实现9*9乘法表
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
//int i;
for(int i=1; i<=9; i++)
{
cout<<endl;
for(int j=1; j<=i; j++)
{
cout<<i<<" * "<<j<<" = "<<i*j<<" ";
}
}
return 0;
}
但是貌似这样用cout无法保证输出的格式(宽度不对齐),输出往往会有不对齐的情况。当然应该也有相应的解决方法的。。另外可以考虑使用printf:
int main()
{
int i,j,n;
for(j=1;j<10;j++,printf("\n"))
for(i=1;i<=j;i++)
{n=i*j;printf("%2d*%2d=%2d",i,j,n);}
return 0;
}
3. 从键盘输入整数n,输出1+3+5+7+……前n项的和。(n<=100)
int main()
{
int i;
int sum=0;
cout<<"please input the NO. you want to calculate the sum"<<endl;
cin>>i;
for(int k=1;k<=i;k++)
sum=sum+k;
cout<<"the sum is :"<<sum<<endl;
return 0;
}
4. 一个正整数与3的和是5的倍数,与3的差是6的倍数,编写一个程序求符合条件的最小数。
int main()
{
int x=1;
for(;;)
{
if (((3+x)%5==0)&&((x-3)%6==0))
{
cout<<x<<endl;
break;
}
else x++;
}
return 0;
}
5. 判断101-200之间有多少个素数,并输出所有素数及素数的个数。
#include<math.h>
#include<stdio.h>
int main()
{
int a,b,i;
int k=0;
float c;
a=101;
i=2;
for(a=101; a<=200; a++)
{
b=1;
c=sqrt(a);
for(i=2; i<=c; i++)
{
if(a%i==0)
{
b=0;
break;
}
}
if (b==1)
{
printf("%d\n",a);
k++;
}
}
cout<<"the total prime number is "<<k<<endl;
return 0;
}
6. 打印以下图案.
#include<math.h>
#include<conio.h>
#include"stdio.h"
int main()
{
int i,j,row; /*变量的定义*/
for(row=1; row<=4; row++) /*前四排星号*/
{
for(i=1; i<=4-row; i++) printf(" ");
for(j=1; j<=2*row-1; j++) printf("*");
printf("\n");
}
for(row=1; row<=3; row++) /*后三排星号*/
{
for(i=1; i<=row; i++) printf(" ");
for(j=1; j<=2*(4-row)-1; j++) printf("*");
printf("\n");
}
}