Chapter3
This commit is contained in:
parent
bb35fb2db6
commit
8e36a1a11c
26
DeitelC/Chapter3/BinarytoDecimal.c
Normal file
26
DeitelC/Chapter3/BinarytoDecimal.c
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
int i;
|
||||||
|
|
||||||
|
puts("Enter binary!");
|
||||||
|
scanf("%d", &i);
|
||||||
|
|
||||||
|
int t = i;
|
||||||
|
int final = 0, power = 1;
|
||||||
|
|
||||||
|
while (t > 0) {
|
||||||
|
if(t % 10 == 1) {
|
||||||
|
final = power + final;
|
||||||
|
t = t / 10;
|
||||||
|
} else if (t % 10 == 0) {
|
||||||
|
t = t / 10;
|
||||||
|
} else {
|
||||||
|
puts("NOT BINARY!");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
power = 2 * power;
|
||||||
|
}
|
||||||
|
printf("Decimal: %d, Binary: %d\n", final, i);
|
||||||
|
return 0;
|
||||||
|
}
|
54
DeitelC/Chapter3/Crypted.c
Normal file
54
DeitelC/Chapter3/Crypted.c
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main (void) {
|
||||||
|
int i = 0, final = 0, mult = 1;
|
||||||
|
puts("Enter 4 digit integer:");
|
||||||
|
scanf("%d", &i);
|
||||||
|
|
||||||
|
if (i / 1000 >= 10) {
|
||||||
|
puts("More than 4 digit");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (i > 0) {
|
||||||
|
int digit = (((i % 10) + 7) %10);
|
||||||
|
final = digit * mult + final;
|
||||||
|
mult *= 10;
|
||||||
|
i /= 10;
|
||||||
|
}
|
||||||
|
int t = 0000;
|
||||||
|
t = (final / 100) % 10;
|
||||||
|
t = (final / 1000)*10 + t;
|
||||||
|
t = (final%10)*100 + t;
|
||||||
|
t = ((final / 10)%10)*1000 + t;
|
||||||
|
final = t;
|
||||||
|
printf("Crypted int: %d\n", final);
|
||||||
|
|
||||||
|
puts("Enter crypted integer: ");
|
||||||
|
scanf("%d", &i);
|
||||||
|
if (i / 1000 >= 10) {
|
||||||
|
puts("More than 4 digit");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
t = i;
|
||||||
|
t = (final / 100) % 10;
|
||||||
|
t = (final / 1000)*10 + t;
|
||||||
|
t = (final%10)*100 + t;
|
||||||
|
t = ((final / 10)%10)*1000 + t;
|
||||||
|
i = t;
|
||||||
|
//printf("Crypted int: %d\n", t);
|
||||||
|
|
||||||
|
|
||||||
|
mult = 1, final = 0;
|
||||||
|
|
||||||
|
while (i > 0) {
|
||||||
|
int digit = (((i % 10)+3) % 10);
|
||||||
|
final = digit * mult + final;
|
||||||
|
mult *= 10;
|
||||||
|
i /= 10;
|
||||||
|
}
|
||||||
|
printf("Encypted integer: %d\n", final);
|
||||||
|
return 0;
|
||||||
|
}
|
54
DeitelC/Chapter3/DanglingElse.c
Normal file
54
DeitelC/Chapter3/DanglingElse.c
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
|
||||||
|
puts("a) x = 5 and y = 8");
|
||||||
|
|
||||||
|
int x = 5, y = 8;
|
||||||
|
|
||||||
|
if (y == 8)
|
||||||
|
if (x == 5)
|
||||||
|
puts("@@@@@");
|
||||||
|
else
|
||||||
|
puts("#####");
|
||||||
|
puts("$$$$$");
|
||||||
|
puts("&&&&&");
|
||||||
|
|
||||||
|
|
||||||
|
puts("\nb) x = 5 and y = 8");
|
||||||
|
|
||||||
|
if (y == 8) {
|
||||||
|
if (x == 5)
|
||||||
|
puts("@@@@@");
|
||||||
|
} else {
|
||||||
|
puts("#####");
|
||||||
|
puts("$$$$$");
|
||||||
|
puts("&&&&&");
|
||||||
|
}
|
||||||
|
|
||||||
|
puts("\nc) x = 5 and y = 8");
|
||||||
|
|
||||||
|
if (y == 8) {
|
||||||
|
if (x == 5)
|
||||||
|
puts("@@@@@");
|
||||||
|
} else {
|
||||||
|
puts("#####");
|
||||||
|
puts("$$$$$");
|
||||||
|
}
|
||||||
|
puts("&&&&&");
|
||||||
|
|
||||||
|
|
||||||
|
puts("\nd) x = 5 and y = 7");
|
||||||
|
x = 5, y = 7;
|
||||||
|
if (y == 8) {
|
||||||
|
if (x == 5)
|
||||||
|
puts("@@@@@");
|
||||||
|
} else {
|
||||||
|
puts("#####");
|
||||||
|
puts("$$$$$");
|
||||||
|
puts("&&&&&");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -19,7 +19,7 @@ int main(void){
|
|||||||
}
|
}
|
||||||
while(b > 1 && b < a) {
|
while(b > 1 && b < a) {
|
||||||
printf("*");
|
printf("*");
|
||||||
while(c < a) {
|
while(c < a-1) {
|
||||||
printf(" ");
|
printf(" ");
|
||||||
c++;
|
c++;
|
||||||
}
|
}
|
||||||
@ -38,4 +38,4 @@ int main(void){
|
|||||||
b++;
|
b++;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
BIN
DeitelC/Chapter3/a.out
Normal file
BIN
DeitelC/Chapter3/a.out
Normal file
Binary file not shown.
6
DeitelC/Chapter3/inttest.c
Normal file
6
DeitelC/Chapter3/inttest.c
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
printf("%d\n", 4-10);
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user