1.
#include <stdio.h>
int main()
{
int code1, code2, code3, code4, code5;
int score1, score2, score3, score4, score5;
int max = 0;
int second_max = 0;
int second_max_code;
printf("Enter student 1 code and score: ");
scanf("%d %d", &code1, &score1);
printf("Enter student 2 code and score: ");
scanf("%d %d", &code2, &score2);
printf("Enter student 3 code and score: ");
scanf("%d %d", &code3, &score3);
printf("Enter student 4 code and score: ");
scanf("%d %d", &code4, &score4);
printf("Enter student 5 code and score: ");
scanf("%d %d", &code5, &score5);
if (score1 > max)
{
second_max = max;
max = score1;
second_max_code = code1;
}
else if (score1 > second_max)
{
second_max = score1;
second_max_code = code1;
}
if (score2 > max)
{
second_max = max;
max = score2;
second_max_code = code2;
}
else if (score2 > second_max)
{
second_max = score2;
second_max_code = code2;
}
if (score3 > max)
{
second_max = max;
max = score3;
second_max_code = code3;
}
else if (score3 > second_max)
{
second_max = score3;
second_max_code = code3;
}
if (score4 > max)
{
second_max = max;
max = score4;
second_max_code = code4;
}
else if (score4 > second_max)
{
second_max = score4;
second_max_code = code4;
}
if (score5 > max)
{
second_max = max;
max = score5;
second_max_code = code5;
}
else if (score5 > second_max)
{
second_max = score5;
second_max_code = code5;
}
printf("The student with the second highest score is %d with a score of %d\n", second_max_code, second_max);
return 0;
}
out:
Enter student 1 code and score: 1
20
Enter student 2 code and score: 2
40
Enter student 3 code and score: 3
45
Enter student 4 code and score: 4
67
Enter student 5 code and score: 5
2
The student with the second highest score is 4 with a score of 45
2.
#include <stdio.h>
int main()
{
int num, r, i;
int b, a, statb;
char ans;
printf("please enter your number\n:");
scanf("%d", &num);
for (i = 1; i < 100000000; i++)
{
b = 2;
a = i;
statb = b;
for (int j = 0; j < a; j++)
b = b * statb;
r = b - 1;
if (num % r == 0)
{
b = 2;
a = i - 1;
statb = b;
for (int j = 0; j < a; j++)
b = b * statb;
if (num / r == b)
{
printf("yes this number is perfect!");
break;
}
else
{
printf("no this number is not perfect :(");
break;
}
}
}
printf("\n\ndo you wanna continue?(Y,y,1,N,n,0)\n:");
scanf(" %c", &ans);
if (ans == 'Y' || ans == 'y' || ans == '1')
main();
else
return 0;
return 0;
}
out:
please enter your number
:6
yes this number is perfect!
do you wanna continue?(Y,y,1,N,n,0)
:y
please enter your number
:23
no this number is not perfect :(
do you wanna continue?(Y,y,1,N,n,0)
:y
please enter your number
:481
no this number is not perfect :(
do you wanna continue?(Y,y,1,N,n,0)
:n
3.
#include <stdio.h>
int main()
{
int i, j;
const char ch = '*';
printf("you know whats gonna happen ;)\n\n ");
for (i = 1; i <= 6; i++)
{
for (j = 1; j <= i; j++)
printf("%c", ch);
printf("\n");
}
return 0;
}
out:
you know whats gonna happen ;)
*
**
***
****
*****
******
4.
#include <stdio.h>
int main()
{
char color;
printf("Enter the first letter of a color: ");
scanf("%c", &color);
if (color >= 'a' && color <= 'z')
{
color = color - 'a' + 'A';
}
switch (color)
{
case 'R':
printf("Red\n");
break;
case 'O':
printf("Orange\n");
break;
case 'Y':
printf("Yellow\n");
break;
case 'G':
printf("Green\n");
break;
case 'B':
printf("Blue\n");
break;
case 'I':
printf("Indigo\n");
break;
case 'V':
printf("Violet\n");
break;
default:
printf("Invalid input\n");
}
return 0;
}
out:
Enter the first letter of a color: R
Red
---
Enter the first letter of a color: y
Yellow
---
Enter the first letter of a color: r
Red
5.
#include <stdio.h>
int main()
{
int n, a = 1, b = 1, i;
unsigned int r;
printf("here comes the fibo, but at first, tell me how many steps you wanna go furthur?\n:");
scanf("%d", &n);
for (i = 2; i <= n; i++)
{
if (i % 2 == 0)
{
a = a + b;
}
if (i % 2 != 0)
{
b = a + b;
}
}
if (i % 2 == 0)
r = a;
if (i % 2 != 0)
r = b;
printf("the n fibo you chose issss %llu", r);
return 0;
}
out:
here comes the fibo, but at first, tell me how many steps you wanna go furthur?
:6
the n fibo you chose issss 8⏎
---
here comes the fibo, but at first, tell me how many steps you wanna go furthur?
:100
the n fibo you chose issss 3314859971⏎
6.
#include <stdio.h>
int main()
{
int a, b, i = 0;
printf("enter your 2 numbers\n:");
scanf("%d%d", &a, &b);
while (a >= b)
{
a = a - b;
i++;
}
printf("\n%d", i);
return 0;
}
out:
enter your 2 numbers
:6
2
3⏎
---
enter your 2 numbers
:100
5
20⏎
7.
basic:
#include <stdio.h>
int main()
{
int en = 3;
int enumber1, enumber2, enumber3;
float eincome1, eincome2, eincome3;
float tax1, tax2, tax3;
float pure1, pure2, pure3;
for (int i = 0; i < en; i++)
{
printf("please enter employee number %d code\n:", i + 1);
if (i == 0)
scanf("%d", &enumber1);
if (i == 1)
scanf("%d", &enumber2);
if (i == 2)
scanf("%d", &enumber3);
printf("please enter employee number %d income\n:", i + 1);
if (i == 0)
scanf("%f", &eincome1);
if (i == 1)
scanf("%f", &eincome2);
if (i == 2)
scanf("%f", &eincome3);
}
for (int i = 0; i < en; i++)
{
if (i == 0)
{
if (eincome1 < 400000)
tax1 = 0;
if (eincome1 > 400001 && eincome1 < 500000)
tax1 = eincome1 * 0.1;
if (eincome1 > 500001 && eincome1 < 700000)
tax1 = eincome1 * 0.15;
if (eincome1 > 700001)
tax1 = eincome1 * 0.17;
}
if (i == 1)
{
if (eincome2 < 400000)
tax2 = 0;
if (eincome2 > 400001 && eincome2 < 500000)
tax2 = eincome2 * 0.1;
if (eincome2 > 500001 && eincome2 < 700000)
tax2 = eincome2 * 0.15;
if (eincome2 > 700001)
tax2 = eincome2 * 0.17;
}
if (i == 2)
{
if (eincome3 < 400000)
tax3 = 0;
if (eincome3 > 400001 && eincome3 < 500000)
tax3 = eincome3 * 0.1;
if (eincome3 > 500001 && eincome3 < 700000)
tax3 = eincome3 * 0.15;
if (eincome3 > 700001)
tax3 = eincome3 * 0.17;
}
}
pure1 = eincome1 - tax1;
pure2 = eincome2 - tax2;
pure3 = eincome3 - tax3;
int maxnum = -1;
for (int i = 0; i < en; i++)
{
float current_pure_income;
if (i == 0)
current_pure_income = pure1;
if (i == 1)
current_pure_income = pure2;
if (i == 2)
current_pure_income = pure3;
printf("employee with code %d has income of %f\n", i + enumber1 - maxnum, current_pure_income);
float max_pure_income;
if (maxnum == -1)
max_pure_income = current_pure_income;
if (maxnum == -1)
maxnum = i + enumber1 - maxnum;
else
{
if (i == maxnum)
max_pure_income = current_pure_income;
else
{
if (current_pure_income > max_pure_income)
{
maxnum = i + enumber1 - maxnum;
max_pure_income = current_pure_income;
}
}
}
}
printf("and employee with code %d has the highest pure income of %f\n", maxnum, maxnum == enumber1 ? pure1 : maxnum == enumber2 ? pure2
: pure3);
}
// this code is very basic because its for season 3 of the book, for more advanced code please check 7adv.c
advanced:
#include <stdio.h>
void read(int enuum[], float ein[], int en)
{
for (int i = 0; i < en; i++)
{
printf("please enter employee number %d code\n:", i + 1);
scanf("%d", &enuum[i]);
printf("please enter employee number %d income\n:", i + 1);
scanf("%f", &ein[i]);
}
}
void calc(float ein[], float tax[], int en)
{
for (int i = 0; i < en; i++)
{
if (ein[i] < 400000)
tax[i] = 0;
if (ein[i] > 400001 && ein[i] < 500000)
tax[i] = ein[i] * 0.1;
if (ein[i] > 500001 && ein[i] < 700000)
tax[i] = ein[i] * 0.15;
if (ein[i] > 700001)
tax[i] = ein[i] * 0.17;
}
}
void comp(float ein[], float tax[], float pure[], int en, int *maxnum)
{
for (int i = 0; i < en; i++)
pure[i] = ein[i] - tax[i];
*maxnum = 0;
for (int i = 0; i < en; i++)
{
if (pure[i] > pure[*maxnum])
{
*maxnum = i;
}
else
continue;
}
}
int main()
{
int en, maxnum;
printf("enter the number of the employee\n:");
scanf("%d", &en);
float tax[en], pure[en], eincome[en];
int enumber[en];
read(enumber, eincome, en);
calc(eincome, tax, en);
comp(eincome, tax, pure, en, &maxnum);
for (int i = 0; i < en; i++)
{
printf("employee with code %d has income of %f\n", enumber[i], pure[i]);
}
printf("and employee with code %d has the highest pure income of %f\n", enumber[maxnum], pure[maxnum]);
}
out:
please enter employee number 1 code
:1
please enter employee number 1 income
:120000
please enter employee number 2 code
:2
please enter employee number 2 income
:550000
please enter employee number 3 code
:3
please enter employee number 3 income
:490000
employee with code 1 has income of 120000.000000
employee with code 2 has income of 467500.000000
employee with code 3 has income of 441000.000000
and employee with code 2 has the highest pure income of 467500.000000
8.
#include <stdio.h>
int main()
{
int x =5;
while(--x>0)
printf("%3d",x);
}
out:
4 3 2 1⏎
9.
#include <stdio.h>
int main()
{
for(int i=1;i<=10;i++)
{
if(i<5 && i!=2)
printf("%c",x);
}
}
10.
#include <stdio.h>
int main()
{
int n, i, j;
float r = 0;
float b;
printf("how many steps you wanna continue?\n:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
b = i;
if (b == 0)
b = 1;
else
{
for (j = b - 1; j > 0; j--)
b = b * j;
}
r = r + 1 / b;
}
printf("%f", r);
return 0;
}
out:
how many steps you wanna continue?
:4
1.708333⏎
---
how many steps you wanna continue?
:2
1.500000⏎
11. didnt get it;
12.
#include <stdio.h>
int main()
{
char day;
printf("please enter the number of the day\n:");
scanf(" %c", &day);
switch (day)
{
case '1':
printf("\nsaturday");
break;
case '2':
printf("\nsunday");
break;
case '3':
printf("\nmonday");
break;
case '4':
printf("\ntuesday");
break;
case '5':
printf("\nwednesday");
break;
case '6':
printf("\nthursday");
break;
case '7':
printf("\nfriday");
break;
}
return 0;
}
out:
please enter the number of the day
:3
monday⏎
13.
#include <stdio.h>
int main()
{
int d, m, y, h, hm, bd, bm, by;
int rd, rm, ry;
printf("please enter the day, month, year of your birthday\n:");
scanf("%d%d%d", &bd, &bm, &by);
printf("please enter the current minute, hour, day, month, year in order\n:");
scanf("%d%d%d%d%d", &hm, &h, &d, &m, &y);
rd = bd - d;
if (rd < 0)
rd *= -1;
rm = bm - m;
if (rm < 0)
rm *= -1;
ry = y - by;
printf("you are %d years and %d months and %d days and %d hours and %d minutes and 60 seconds old", ry, rm, rd, h, hm);
return 0;
}
out:
please enter the day, month, year of your birthday
:12
3
1383
please enter the current minute, hour, day, month, year in order
:58
18
6
3
1402
you are 19 years and 0 months and 6 days and 18 hours and 58 minutes and 60 seconds old⏎
14.
#include <stdio.h>
int main()
{
int a, b, c;
float r1, r2;
float delta;
double r;
int i;
printf("ax^2+bx+c:(a,b,c)\n:");
scanf("%d%d%d", &a, &b, &c);
delta = b * b - 4 * a * c;
if (delta < 0)
printf("there are no answers for it");
if (delta > 0)
{
r = delta / 2;
for (i = 0; i < 10; i++)
{
r = (r + delta / r) / 2;
}
r1 = (-1 * b + r) / (2 * a);
r2 = (-1 * b - r) / (2 * a);
}
if (delta == 0)
{
r1 = (-1 * b + r) / (2 * a);
r2 = 0;
}
printf("\nf1=%f\nf2=%f", r1, r2);
return 0;
}
out:
ax^2+bx+c:(a,b,c)
:1
-2
1
f1=1.000000
f2=0.000000⏎
---
ax^2+bx+c:(a,b,c)
:1
0
-1
f1=1.000000
f2=-1.000000⏎
---
ax^2+bx+c:(a,b,c)
:1
2
-15
f1=3.000000
f2=-5.000000⏎
15.
#include <stdio.h>
int main()
{
int i, j;
const char ch = '$';
for (i = 1; i <= 4; i++)
{
for (j = 1; j <= 6; j++)
printf("%c", ch);
printf("\n");
}
}
out:
$$$$$$
$$$$$$
$$$$$$
$$$$$$
16.
#include <stdio.h>
int main()
{
float f1, f2, t, d, m;
printf("please enter two floats\n:");
scanf("%f%f", &f1, &f2);
if (f1 != 0 && f2 != 0)
{
m = f1 - f2;
printf("f1-f2= %f\n", m);
d = f1 / f2;
printf("f1/f2= %f\n", d);
t = f1 * f2;
printf("f1*f2= %f\n", t);
main();
}
else
return 0;
}
out:
please enter two floats
:12.87
13.89
f1-f2= -1.020000
f1/f2= 0.926566
f1*f2= 178.764297
please enter two floats
:0
0
17.
#include <stdio.h>
int main()
{
char n;
scanf("%c", &n);
if (n != '\n')
{
main();
printf("%c", n);
}
return 0;
}
out:
34.981
189.43⏎
---
20.19671
17691.02⏎
18.
#include <stdio.h>
int main()
{
double num;
int intPart, decPart;
printf("Enter a number: ");
scanf("%lf", &num);
intPart = (int)num;
decPart = (num - intPart) * 1000;
decPart = (decPart > 0) ? (decPart + 0.5) : (decPart - 0.5);
printf("Integer part: %d\n", intPart);
printf("Decimal part: %d\n", decPart);
return 0;
}
out:
Enter a number: 12.891
Integer part: 12
Decimal part: 891