关于C的小程序-4

来源:互联网 发布:kk聊天软件 编辑:程序博客网 时间:2024/06/05 18:35
1.编写代码模拟三次密码输入的场景
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<string.h>
int main()
{
char passwd[10] = { 0 };


int i = 0;
for (i = 0; i<3; i++)
{
scanf("%s", passwd);
if (strcmp(passwd, "123456") == 0)
{
break;
}
else
{
printf("密码错误,重新输入\n");
}
}


if (i == 3)
{
printf("退出系统\n");
}
else
{
printf("登录成功\n");
}


system("pause");
return 0;
}
2.编写一个程序,可以一直接收键盘字符,如果是小写字符就输出对应的大写字符,如果接收的是大写字符,就输出对应的小写字符,如果是数字不输出。
#include<stdio.h>
int main()
{
int ch = 0;
printf("请输入:\n");
while ((ch = getchar()) != EOF)
{
if (ch >= 'a'&&ch <= 'z')
{
putchar(ch - 32);
printf("\n");
}
if (ch>='0' && ch<='9')
{
continue;
}
if (ch >= 'A' && ch <= 'Z')
{
putchar(ch + 32);
printf("\n");
}
}
system("pause");
return 0;
}
3.计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值。
#include<stdio.h>
int main()
{
double sum;
int i;
for (sum = 0, i = 1; i <= 100; i++)
{
if (i % 2 == 1)
{
sum += 1.0 / i;
}
else if (i % 2 == 0)
{
sum -= 1.0 / i;
}
}
printf("结果:%lf", sum);
system("pause");
return 0;

}
#include <stdio.h>
int main()
{
int i = 0; double sum1 = 0; double sum2 = 0;
 double sum = 0;
for (i = 1; i <= 100;i++)
{
if (i % 2 == 0)
{
sum1=sum1-(1.0 / i);
}
if (i % 2 == 1)
{
sum2=sum2+( 1.0 / i);
}
}
sum = sum1 + sum2;
printf("sum=%lf", sum);
system("pause");
return 0;
}

4.输出一个整数的每一位。
#include<stdio.h>
int main()
{
int date;
int temp = 0;
scanf_s("%d", &date);
printf("%d这个数从低位到高位输出的是:", date);
while (date>0)
{
temp = date % 10;
printf("%d  ", temp);
date /= 10;
}
system("pause");
return 0;
}
5.编写程序数一下 1到 100 的所有整数中出现多少次数字 9
#include<stdio.h>
int main()
{
int n = 1;
int count = 0;
while (n<100)
{
if (n % 10 == 9)
{
count++;
}
if (n % 100 - n % 10 == 90)
{
count++;
}
n++;
}
printf("1-100含的数字9个数为:%d\n", count);
system("pause");
return 0;
}
0 0
原创粉丝点击