1.用循环实现n=1+2+....+100
#include"stdio.h"
main()
{int i,n=0;
for(i=1;i<=100;i++)
n+=i;
printf("%d\n",n);
}
2.小写字母转换成大写字母,直至输入非字母字符.
#include"stdio.h"
main()
{char c;
while((c=getchar())!='\n')
{if(c>='a' && c<='z')c=c-'a'+'A';
putchar(c);
}
putchar('\n');
}
3.读10个整数存入数组,找出其中最大值和最小值.
#include "stdio.h"
main()
{
int i,a[10],max,min;
for(i=0;i<10;i++)
scanf("%d",&a[i]);
max=min=a[0];
for(i=0;i<10;i++)
if(maxelse if(min>a[i])min=a[i];
printf("max is %d, min is %d\n",max,min);
}
5.求二维数组中最大元素值及其行列号.
#include
void max_value(int a[3][4] )
{ int i,j,t=a[0][0];
for(i=0;i<3;i++)
for(j=0;j<4;j++)
if (tprintf("max value is %d\n",t);
printf("%d %d",i,j);
}
main()
{ int a[3][4]={{1,3,5,7},
{2,4,6,8},{15,17,34,12}};
max_value(a);
6.输入一行字符,统计其中有多少个单词.
#include
main()
{
char a[100];
int i,count=0,flag=1;
gets(a);
for(i=0;a[i];i++)
{
if((a[i]>='a' && a[i]<='z') || (a[i]>='A' && a[i]<='Z'))
{
flag=0;
continue;
}
if(!flag)
{
count++;
flag=1;
}
}
i--;
if((a[i]>='a' && a[i]<='z') || (a[i]>='A' && a[i]<='Z'))
count++;
printf("%d\n",count);
}
8.个学生各学4门课,计算总平均分,并输出第n个学生成绩.
#include
main()
{ int i,j,n;
int a[3][4]={{70,80,85,82},{90,83,86,88},{75,77,94,83}};
float sum=0.0,avr;
for(i=0;i<3;i++)
for(j=0;j<4;j++)
sum+=a[i][j];
avr=sum/12;
printf("%f",avr);
printf("请输入要显示第几个学生的成绩:");
scanf("%d",&n);
for(i=0;i<=4;i++)
printf("%d ",a[n-1][i]);
}
//第一题
#include
#define N 100
int main(void)
{
int i;
int s;
s=(1+N)*N/2; //由数学定理可得该公式.
printf("1+2+...+100=%d\n",s);
return 0;
}
//第二题
#include
#include
int main(void)
{
char ch;
while(isalpha(ch=getch()))
putchar(toupper(ch));
putchar('\n');
return 0;
}
//第三题
#include
int main(void)
{
int num[10];
int i;
int max,min;
printf("Please enter 10 integers:\n");
for(i=0;i<10;++i)
scanf("%d",&num[i]);
max=min=num[0];
for(i=1;i<10;++i)
{
if(max
if(min>num[i])
min=num[i];
}
printf("There are your numbers:\n");
for(i=0;i<10;++i)
{
printf("%d ",num[i]);
}
printf("\nThe max number is %d\nThe min number is %d\n",max,min);
}
//第四题
#include
int main(void)
{
int num[10];
int i,j;
int maxid;
int temp;
printf("Please enter 10 integers:\n");
for(i=0;i<10;++i)
scanf("%d",&num[i]);
for(i=0;i<10;++i)
{
maxid=i;
for(j=i+1;j<10;++j)
if(num[maxid]
if(maxid!=i)
{
temp=num[maxid];
num[maxid]=num[i];
num[i]=temp;
}
}
printf("After sort:\n");
for(i=0;i<10;++i)
{
printf("%d ",num[i]);
}
printf("\n");
return 0;
}
//第五题
#include
#define ROW 3
#define COL 3
int main(void)
{
int num[ROW][COL];
int i,j,max;
int a,b;
printf("Please enter %d integers:\n",ROW*COL);
for(i=0;i
printf("There are your numbers:\n");
for(i=0;i
for(j=0;j
printf("\n");
}
max=num[0][0];
a=1;
b=1;
for(i=0;i
max=num[i][j];
a=i+1;
b=j+1;
}
printf("The max number is %d\nIt is the %d col of the row %d\n",max,b,a) ;
return 0;
}
//第六题
#include
#include
#include
int main(void)
{
int i=0,num=0;
char str[100];
gets(str);
do
{
while(str[i]== ' ')
++i;
++num;
while(str[i]!=' ' && str[i]!='\0')
++i;;
}while(str[i]!='\0');
printf("number of word:%d\n",num);
}
//第七题
#include
void swap(int *a,int *b);
int main(void)
{
int a,b;
printf("Please enter two integers:\n");
scanf("%d%d",&a,&b);
printf("a=%d,b=%d\n",a,b);
printf("After swap the a and b :\n");
swap(&a,&b);
printf("a=%d,b=%d\n",a,b);
return 0;
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
1.使用一个for循环即可:
int add=0; //用于存放和的值,初值为0
for(int i=1;i<=100;i++)//循环100次
{
add=add+i;//逐次累加
}
接下来的慢慢给你