Raqamlar va sonlar :: Yechimlar
Masala A
Kiritilgan 3 xonali sonning raqamlari yig‘indisini ekranga chiqaradigan dastur tuzing.
Uch xonali son
Kiritilgan sonning raqamlari yig‘indisi
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
printf("%d", n / 100 + (n % 100) / 10 + n % 10);
return 0;
}
import math
import itboom
def main():
print(sum(map(int, input())))
if __name__ == '__main__':
main()
Masala B
Kiritilgan 4 xonali son palindrom son ekanligiga tekshiradigan dastur tuzing.
To‘rt xonali son
Agar kiritilgan son palindrom son bo‘lsa YES
, aks holda NO
so‘zi
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int m = 1000 * (n % 10) + 100 * ((n / 10) % 10) + 10 * ((n / 100) % 10) + n / 1000;
if (n == m) {
printf("YES");
} else {
printf("NO");
}
return 0;
}
import math
import itboom
def main():
n = input()
if n == n[::-1]:
print("YES")
else:
print("NO")
if __name__ == '__main__':
main()
Masala C
Kiritilgan sonni teskari tabribda ekranga chiqaradigan dastur tuzing.
\(1\) dan \(10^8\) gachaga bo‘lgan butun son
Kiritilgan sonning teskarisi
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <itboom.h>
int main() {
int n;
scanf("%d", &n);
int k = 1;
int n_ = n;
while(n_ > 0) {
k *= 10;
n_ /= 10;
}
int m = 0;
n_ = n;
while(n_ > 0) {
k /= 10;
m += k * (n_ % 10);
n_ /= 10;
}
printf("%d", m);
return 0;
}
import math
import itboom
def main():
n = input()
print(n[::-1].lstrip("0"))
if __name__ == '__main__':
main()
Masala D
C dasturlash tilida dastlab oddiy takrorlash operatorlari, ikkinchisida rekursiyadan va va oxirgisida char dan foydalanilgan kodlar keltirilgan.
Kiritilgan sondagi raqamlarni turgan joyida qiymaticha takrorlab, hosil bo‘lgan sonni ekranga chiqaradigan dastur tuzing.
\(1\) dan \(10^8\) gacha bo‘lgan butun son
Masala shartidagi son
#include <stdio.h>
int main() {
int n, k = 1, n_ = 0;
scanf("%d", &n);
n_ = n;
while(n_ > 0) {
k *= 10;
n_ /= 10;
}
k /= 10;
while(n > 0) {
int m = n / k;
for(int i = 0; i < m; i++) {
printf("%d", m);
}
n = n % k;
k /= 10;
}
return 0;
}
#include <stdio.h>
void fn(int n) {
if (n == 0) {
return 0;
}
fn(n / 10);
int k = n % 10;
for(int i = 0; i < k; i++) {
printf("%d", k);
}
}
int main() {
int n;
scanf("%d", &n);
fn(n);
return 0;
}
#include <stdio.h>
int main() {
int c;
while ((c = getchar()) != EOF) {
int k = c - '0';
for(int i = 0; i < k; i++) {
printf("%d", k);
}
}
return 0;
}
import math
import itboom
def main():
for k in input():
print(k * int(k), end="")
if __name__ == '__main__':
main()
Masala E
Kiritilgan raqamdan keyingi raqamni ekranga chiqaradigan dastur tuzing
0 dan 8 gacha bo‘lgan raqam
Kiritilgan raqamdan keyingi raqam
,+.