C语言入门课程学习笔记3
- 第12课 - if 语句编程练习
- 第13课 - switch 多分支选择语句
- 第14课 - 程序中的循环结构
- 第15课 - while 语句编程练习
- 第16课 - do...while 与 for
- 第17课 - break 与 continue
本文学习自狄泰软件学院 唐佐林老师的 C语言入门课程,图片全部来源于课程PPT,仅用于个人学习记录
第12课 - if 语句编程练习
#include <stdio.h>
int main()
{
int x = 0;
int y = 0;
printf("Input 2 integers: ");
scanf("%d%d", &x, &y);
if( x > y )
{
printf("The bigger is: %d\n", x);
}
else if( x < y )
{
printf("The bigger is: %d\n", y);
}
else
{
printf("The 2 integers is equal: x == y\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int x = 0;
int y = 0;
int z = 0;
int t = 0;
printf("Input 3 integers: ");
scanf("%d%d%d", &x, &y, &z);
if( x < y )
{
t = x;
}
else
{
t = y;
}
if( t > z )
{
t = z;
}
printf("The small one is: %d\n", t);
return 0;
}
#include <stdio.h>
int main()
{
int score = 0;
printf("Input score: ");
scanf("%d", &score);
if( (0 <= score) && (score <= 100) )
{
if( score >= 90 )
printf("A\n");
else if( score >= 80 )
printf("B\n");
else if( score >= 70 )
printf("C\n");
else if( score >= 60 )
printf("D\n");
else
printf("E\n");
}
else
{
printf("Invalid Score!\n");
}
return 0;
}
第13课 - switch 多分支选择语句
#include <stdio.h>
int main()
{
int a = 1;
int b = 2;
char sign = 0;
printf("Input a sign: ");
scanf("%c", &sign);
switch(sign)
{
case '+':
printf("%d + %d = %d\n", a, b, a + b);
break;
case '-':
printf("%d - %d = %d\n", a, b, a - b);
break;
case '*':
printf("%d * %d = %d\n", a, b, a * b);
break;
default:
printf("Unsupported Sign: %c\n", sign);
}
return 0;
}
#include <stdio.h>
int main()
{
int num = 0;
printf("Input a number: ");
scanf("%d", &num);
switch( num )
{
case 1:
printf("A\n");
break;
case 2:
printf("B\n");
break;
case 3:
printf("C\n");
break;
case 4:
printf("D\n");
break;
case 5:
printf("E\n");
break;
default:
printf("Invalid Input\n");
}
return 0;
}
#include <stdio.h>
int main()
{
int num = 0;
printf("Input a number: ");
scanf("%d", &num);
switch( num % 3 )
{
case 0:
printf("Object Number: %d\n", num);
break;
case 1:
printf("Bigger!\n");
break;
case 2:
printf("Smaller!\n");
break;
}
return 0;
}
#include <stdio.h>
int main()
{
int num = 0;
printf("Input a number: ");
scanf("%d", &num);
switch( num )
{
case 1:
case 2:
case 3:
case 4:
case 5:
printf("%c\n", 'A' + (num - 1));
break;
default:
printf("Invalid Input\n");
}
return 0;
}
第14课 - 程序中的循环结构
#include <stdio.h>
int main()
{
int sum = 0;
int i = 1;
while( i <= 100 )
{
sum = sum + i;
i = i + 1;
}
printf("sum = %d\n", sum);
printf("i = %d\n", i);
return 0;
}
//output:
//sum = 5050
//i = 101
#include <stdio.h>
int main()
{
int sum = 0;
int i = 1;
while( i <= 100 )
{
if( (i % 3) == 0 )
{
sum = sum + i;
printf("i = %d\n", i);
}
i = i + 1;
}
printf("sum = %d\n", sum);
return 0;
}
/*
output:
i = 3
i = 6
i = 9
i = 12
i = 15
i = 18
i = 21
i = 24
i = 27
i = 30
i = 33
i = 36
i = 39
i = 42
i = 45
i = 48
i = 51
i = 54
i = 57
i = 60
i = 63
i = 66
i = 69
i = 72
i = 75
i = 78
i = 81
i = 84
i = 87
i = 90
i = 93
i = 96
i = 99
sum = 1683
*/
第15课 - while 语句编程练习
#include <stdio.h>
int main()
{
int sum = 0;
int i = 1;
int an = 0;
while( sum < 10000 )
{
an = (2*i) * (2*i);
sum = sum + an;
i = i + 1;
}
i = i - 1;
printf("sum = %d\n", sum);
printf("an = %d\n", an);
printf("i = %d\n", i);
return 0;
}
#include <stdio.h>
int main()
{
double sum = 0.0;
double an = 0.0;
int i = 1;
int flag = 1;
while( i <= 1000000 )
{
an = flag * (1.0/(2*i - 1));
sum = sum + an;
flag = flag * (-1);
i = i + 1;
}
printf("PI = %f\n", sum*4);
return 0;
}
#include <stdio.h>
int main()
{
double sum = 0.0;
double an = 1.0;
int i = 1;
int flag = 1;
while( !((-(1e-6) < an) && (an < 1e-6)) )
{
an = flag * (1.0/(2*i - 1));
sum = sum + an;
flag = flag * (-1);
i = i + 1;
}
printf("PI = %f\n", sum*4);
return 0;
}
#include <stdio.h>
int main()
{
int i = 2;
while( i <= 100 )
{
int flag = 0;
int j = 2;
while( j < i )
{
if( i%j == 0 )
{
flag = flag + 1;
}
j = j + 1;
}
if( flag == 0 )
{
printf("%d, ", i);
}
i = i + 1;
}
printf("\n");
return 0;
}
第16课 - do…while 与 for
#include <stdio.h>
int main()
{
int sum = 0;
int input = 0;
int i = 0;
while( i > 100 )
printf("while( i > 0 )\n");
do
printf("do...while( i > 0 )\n");
while( i > 100 );
do
{
printf("Input: ");
scanf("%d", &input);
sum = sum + input;
} while( sum < 100 );
printf("sum = %d\n", sum);
return 0;
}
#include <stdio.h>
int main()
{
int esum = 0;
int osum = 0;
int i = 0;
for(i=1; i<=100; i=i+2)
{
osum = osum + i;
esum = esum + (i + 1);
}
printf("osum = %d\n", osum);
printf("esum = %d\n", esum);
return 0;
}
#include <stdio.h>
int main()
{
int sum = 0;
int i = 1;
for(; i <= 100; )
{
sum = sum + i;
i = i + 1;
}
printf("sum = %d\n", sum);
return 0;
}
第17课 - break 与 continue
#include <stdio.h>
int main()
{
int sum = 0;
int i = 1;
while( 1 )
{
sum = sum + i;
if( i == 100 )
break;
i = i + 1;
}
printf("sum = %d\n", sum);
printf("i = %d\n", i);
return 0;
}
#include <stdio.h>
int main()
{
int target = 30;
int input = 0;
for(;;)
{
printf("Input: ");
scanf("%d", &input);
if( input == target )
break;
else if( input < target )
printf("It's small!\n");
else if( input > target )
printf("It's big!\n");
}
printf("Target = %d\n", input);
return 0;
}
#include <stdio.h>
int main()
{
int i = 0;
for(i=1; i<=10; i=i+1)
{
switch( i % 2 )
{
case 0:
printf("%d is even number!\n", i);
break;
case 1:
printf("%d is odd number!\n", i);
break;
}
}
return 0;
}
//output:
//1 is odd number!
//2 is even number!
//3 is odd number!
//4 is even number!
//5 is odd number!
//6 is even number!
//7 is odd number!
//8 is even number!
//9 is odd number!
//10 is even number!
#include <stdio.h>
int main()
{
int k = 0;
int input = 0;
printf("Input: ");
scanf("%d", &input);
switch( input % 2 )
{
case 0:
printf("case 0\n");
for(k=0; k<=2; k=k+1)
if( k )
break;
break;
case 1:
printf("case 1\n");
break;
}
return 0;
}
#include <stdio.h>
int main()
{
int i = 0;
int n = 0;
for(i=1; i<1000; i=i+1)
{
if( i % 7 )
continue;
printf("%d ", i);
n = n + 1;
}
printf("\n");
printf("Count: %d\n", n);
return 0;
}
/*
output:
7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 112 119 126 133 140 147 154 161 168 175 182 189 196 203 210 217 224 231 238 245 252 259 266 273 280 287 294 301 308 315 322 329 336 343 350 357 364 371 378 385 392 399 406 413 420 427 434 441 448 455 462 469 476 483 490 497 504 511 518 525 532 539 546 553 560 567 574 581 588 595 602 609 616 623 630 637 644 651 658 665 672 679 686 693 700 707 714 721 728 735 742 749 756 763 770 777 784 791 798 805 812 819 826 833 840 847 854 861 868 875 882 889 896 903 910 917 924 931 938 945 952 959 966 973 980 987 994
Count: 142
*/