1. The first applet: generate 20 random numbers between 30-70 and calculate their and, use the Arc4random () function, generate a random number between "X Y", Arc4random ()% (y-x+1) + x need to add a header file Stdlib.h
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int a[20]={0};
int sum=0;
for (int i=0;i<20;i++) {
A[i]=arc4random ()%41+30;// to add stdlib header file, otherwise it will produce negative numbers
printf ("%d", a[i]);
Sum+=a[i];
}
printf ("\n%d", sum);
return 0;
}
2, the second small program: print 100 can not be divisible by 7, and does not contain 7 of the number
#include <stdio.h>
int main ()
{
int i;
for (i=1;i<=100;i++)
{
if (i%7==0 | | i%10==7| | i/10==7) {
Continue
}
printf ("%d", I);
}
return 0;
}
3, the third small program: shopping malls to buy things, more than 500, the portion of the beat 90 percent
#include <stdio.h>
int main ()
{
char c;
int money=0;
int realpay = 0;
do{
printf ("Please enter an amount:");
scanf ("%d", &money);
if (money>500)
{
Realpay= (money-500) *0.9+500;
}
else{
Realpay=money;
}
printf ("%d\n", Realpay);
printf ("Continue: y/n?");
GetChar ();
C=getchar ();
}while (c== ' Y ' | | c== ' y ');
return 0;
}
4, the fourth small program: Judge Leap year, four years a leap, a century does not leap, 400 years again leap
#include <stdio.h>
int main ()
{
char c;
int year=0;
do{
scanf ("%d", &year);
if (year%400==0| | (year%4==0&&year%100!=0))
printf ("%d years is a leap year \ n";
Else
printf ("%d years is not a leap year \ n";
printf ("Whether to continue: y/n?\n");
GetChar ();
C=getchar ();
}while (c== ' Y ' | | c== ' Y ');
return 0;
}
5, the 5th Small program: Structure of the structure of the members of the body
#include <stdio.h>
typedef struct date{
int year;
int month;
int day;
}mydate;
typedef struct stu{
int number;
MyDate birthday;
Char name[20];
Char sex[10];
int score;
}stu;
int main ()
{
Stu Students[5] = {
{001,{1991,1,1}, "Laoer", "Nan", 87},
{002,{1992,2,2}, "Laoer", "NV", 82},
{003,{1993,3,3}, "Laosan", "Nan", 88},
{004,{1994,4,4}, "Laosi", "Nan", 84},
{005,{1995,5,5}, "Laowu", "Nan", 85}
};
for (int i=0;i<5-1;i++) {
for (int j=0;j<5-1-i;j++) {
if (Students[j].score<students[j+1].score) {
Stu Temp=students[j];
STUDENTS[J]=STUDENTS[J+1]; //struct can be directly assigned value
Students[j+1]=temp;
}
}
}
printf ("%d%d-%d-%d%s%s%d\n", Students[i].number,students[i].birthday.year,students[i].birthday.month,students[i ].birthday.day,students[i].name,students[i].sex,students[i].score);
}
}
6, two-dimensional array learning: Generate 2 Rows 3 columns of the random number between 1-50 to sort and print, print the maximum number
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int number[2][3]={0};
for (int i=0;i<2;i++) {
for (int j=0;j<3;j++) {
Number[i][j]=arc4random ()%50;
printf ("%d", number[i][j]);
}
printf ("\ n");
}
int max=0;
for (int i=0;i<2;i++) {
for (int j=0;j<3;j++) {
if (Max<number[i][j]) {
MAX=NUMBER[I][J];
}
}
}
printf ("%d\n", Max);
for (int i=0;i<2;i++) {
for (int j=0;j<3;j++) {
if (Number[i][j]==max) {
printf ("%d%d", i,j);
}
}
}
return 0;
}
7. Enter a number to calculate the factorial
#include <stdio.h>
int main ()
{
int i;
int sum=1;
printf ("Please enter:");
scanf ("%d", &i);
for (int j=1;j<=i;j++) {
Sum*=j;
}
printf ("Factorial of%d is%d\n", i,sum);
}
8. Enter a date to determine the day of the year
typedef struct date{
int year;
int mouth;
int day;
}date;
Judging leap years
int pd_year (int a);
int pd_year (int a)
{
if (a%400==0| | (a%4==0&&a%100!=0))
return 1;
Else
return 0;
}
int Pd_day (struct date date);
int Pd_day (struct date date)
{
int sum=0,days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
for (int i=0; i<date.mouth-1; i++)
Sum+=days[i];
if (date.mouth>2)
Sum=sum+date.day+pd_year (date.year);
Else
Sum=sum+date.day;
return sum;
}
int main (void) {
struct date date;
int n;
printf ("Please enter the date (month and day) \ n");
scanf ("%d%d%d", &date.year,&date.mouth,&date.day);
N=pd_day ((Date));
printf ("The Day of the year is%d days", n);
}
return 0;
}
9, enter the score, Judge Abcde, three times the wrong chance
#include <stdio.h>
int main (void)
{
int gread=0;
int i=3;
printf ("Please enter your score: \ n");
scanf ("%d", &gread);
while (gread==0| | Gread) {
if (gread>=0&&gread<=100) {
Switch (GREAD/10) {
Case 10:
Case 9:
printf ("Your grades are a\n");
Break
Case 8:
printf ("Your grades are b\n");
Break
Case 7:
printf ("Your grades are c\n");
Break
Case 6:
printf ("Your grades are d\n");
Break
Case 5:
Case 4:
Case 3:
Case 2:
Case 1:
Case 0:
printf ("Your grades are e\n");
Break
Default
Break
}
}else
{
I.;
if (i>0) {
printf ("Input error, you also have%d input opportunities!") \ n ", i);}
else{
Break
}
}
scanf ("%d", &gread);
}
return 0;
}
C Language Learning record "July"