1:
#include
int main(void)
{
long int i,n;
printf("This program prints a table of squares.\n");
printf("Enter number of entries in table:");
scanf("%ld",&n);
for(i=1;i<=n;i++)
printf("%10ld%10ld\n",i,i*i);
return 0;
}
2:
#include
int main (void)
{
int i, n;
printf ("This program prints a table of squares. \n");
printf ("Enter number of entries in table: ");
scanf ("%d", &n);
getchar();
for (i = 1; i <= n; i++) {
printf ("%10d%10d\n", i, i * i);
if (i % 24 == 0) {
printf ("Press Enter to continue...");
getchar ();
}
}
return 0;
}
3:
#include
int main (void)
{
double n, sum = 0.0;
printf ("This program sums a series of integers.\n");
printf ("Enter inetgers (0 to terminate): ");
scanf ("%lf", &n);
while (n != 0.0) {
sum += n;
scanf ("%lf", &n);
}
printf ("The sum is:%f\n", sum);
return 0;
}
4:
#include
int main (void)
{
int ch;
printf ("Enter phone number: ");
while ((ch = getchar ()) != '\n') {
if (ch <= 'Z' && ch >= 'A') {
switch (ch) {
case 65: case 66: case 67:
printf ("2");
break;
case 68: case 69: case 70:
printf ("3");
break;
case 71: case 72: case 73:
printf ("4");
break;
case 74: case 75: case 76:
printf ("5");
break;
case 77: case 78: case 79:
printf ("6");
break;
case 81: case 82: case 83: case 80:
printf ("7");
break;
case 84: case 85: case 86: case 87:
printf ("8");
break;
case 88: case 89: case 90:
printf ("9");
break;
}
continue;
}
printf ("%c", ch);
}
return 0;
}
5:
#include
int main (void)
{
int ch;
printf ("Enter phone number: ");
while ((ch = getchar ()) != '\n') {
if (ch <= 'Z' && ch >= 'A') {
switch (ch) {
case 65: case 66: case 67:
printf ("2");
break;
case 68: case 69: case 70:
printf ("3");
break;
case 71: case 72: case 73:
printf ("4");
break;
case 74: case 75: case 76:
printf ("5");
break;
case 77: case 78: case 79:
printf ("6");
break;
case 81: case 82: case 83: case 80:
printf ("7");
break;
case 84: case 85: case 86: case 87:
printf ("8");
break;
case 88: case 89: case 90:
printf ("9");
break;
}
continue;
}
printf ("%c", ch);
}
return 0;
}
6:
#include
int main(void)
{
printf("int大小=%d\n",(int)sizeof(int));
printf("short大小=%d\n",(int)sizeof(short int));
printf("long大小=%d\n",(int)sizeof(long int));
printf("float大小=%d\n",(int)sizeof(float));
printf("double大小=%d\n",( int)sizeof(double));
printf("long double大小=%d\n",( int)sizeof(long double));
return 0;
}
7:
#include
int main (void)
{
int num1, denom1, num2, denom2, result_num, result_denom;
char ch;
printf ("Enter two fractions separated by a sign which wanted: ");
scanf ("%d/%d%c%d/%d", &num1, &denom1, &ch, &num2, &denom2);
switch (ch) {
case '+':
result_num = num1 *denom2 + num2 * denom1;
result_denom = denom1 * denom2;
break;
case '-':
result_num = num1 *denom2 - num2 * denom1;
result_denom = denom1 * denom2;
break;
case '*':
result_num = num1 * num2;
result_denom = denom1 * denom2;
break;
case '/':
result_num = num1 *denom2;
result_denom = num2 * denom1;
break;
}
int n, m, temp;
n = result_num;
m = result_denom;
while (n != 0) {
temp = n;
n = m % n;
m = temp;
}
result_num = result_num / m;
result_denom = result_denom / m;
printf ("The result is %d/%d", result_num, result_denom);
return 0;
}
8:
#include
int main (void)
{
int num1, denom1, num2, denom2, result_num, result_denom;
char ch;
printf ("Enter two fractions separated by a sign which wanted: ");
scanf ("%d/%d%c%d/%d", &num1, &denom1, &ch, &num2, &denom2);
switch (ch) {
case '+':
result_num = num1 *denom2 + num2 * denom1;
result_denom = denom1 * denom2;
break;
case '-':
result_num = num1 *denom2 - num2 * denom1;
result_denom = denom1 * denom2;
break;
case '*':
result_num = num1 * num2;
result_denom = denom1 * denom2;
break;
case '/':
result_num = num1 *denom2;
result_denom = num2 * denom1;
break;
}
int n, m, temp;
n = result_num;
m = result_denom;
while (n != 0) {
temp = n;
n = m % n;
m = temp;
}
result_num = result_num / m;
result_denom = result_denom / m;
printf ("The result is %d/%d", result_num, result_denom);
return 0;
}
9:
#include
int main(void)
{
int x,y;
char ch;
printf("Enter a 12-hour time:");
scanf("%d:%d",&x,&y);
while((ch=getchar())!='\n'){
switch(ch){
case 'p':
case 'P':x=x+12;
break;
case 'A':
case 'a':x=x+1-1;
break;}}
printf("Equivalent 24-hour time: %d:%d\n",x,y);
return 0;
}
10:
#include
int main(void)
{
char ch;
int sum=0;
printf("Enter a sentence:");
while((ch=getchar())!='\n'){
if ((ch=='a')||(ch=='e')||(ch=='i')||(ch=='o')||(ch=='u')||(ch=='A')||(ch=='E')||(ch=='I')||(ch=='O')||(ch=='U')){
sum++;}
}
printf("Your sentence contains %d vowels\n",sum);
return 0;
}
11:
#include
int main(void)
{
char x,y,ch;
int sum=0;
printf("Enter a first and last name:");
scanf(" %c",&x);
while(1)
{
y=getchar();
if(65<=y&&y<97)
{
printf("%c",y);
while((ch=getchar())!='\n'){
if(ch==' ')break;
printf("%c",ch);
sum++;
}
}
if(sum>=1)break;
}
printf(", %1c.",x);
return 0;
}
```c
12:
#include
int main(void)
{
char x,y,ch;
int sum=0;
printf("Enter a first and last name:");
scanf(" %c",&x);
while(1)
{
y=getchar();
if(65<=y&&y<97)
{
printf("%c",y);
while((ch=getchar())!='\n'){
if(ch==' ')break;
printf("%c",ch);
sum++;
}
}
if(sum>=1)break;
}
printf(", %1c.",x);
return 0;
}
13:
#include
int main(void)
{
char ch;
float sum=0.0,sum1=1.0;
printf("Enter a sentence:");
while((ch=getchar())!='\n')
{
sum++;
if(ch==' '){
sum1++;
sum--;
}
}
printf("Average word length:%.1f\n",sum/sum1);
return 0;
}
14:
#include
#include
int main(void)
{
double x,y=1;
printf("Enter a positive number:");
scanf("%lf",&x);
while(fabs((((x/y)+y)/2)-y)>=0.00001*y)
{
y=((x/y)+y)/2;}
printf("Square root:%.5f\n",y);
return 0;
}
15:
#include
int main (void)
{
float num;
float res = 1;
printf ("Enter a positive integer: ");
scanf ("%f", &num);
for (int i = 1; i <= num; i++) {
res *= i;
}
printf ("Factorial of %f:%f", num, res);
return 0;
}