Vissza
Az if, switch utasítások
Házi feladatok:
1. Írjunk programot, mely annyit sípol, amennyi a
beolvasott szám utolsó számjegye.
2. Írjunk programot, mely egyet sípol ha a szám
páros és kettőt ha a szám páratlan.
3. Olvassunk be öt számot, és amikor negatív
számot olvasunk be, akkor sípoljon egyet a gép.
4. Olvassunk be öt számot és sípoljon egyet a
gép, ha az összes szám páros volt.
5. Határozzuk meg a beolvasott hat szám közül a
legnagyobbat.
6. Írjunk programot, mely annyit sípol ahány
páros számjegye van egy ötjegyű számnak.
7. Rendezzünk hat számot csökkenő sorrendbe.
Megoldott feladatok:
További megoldott feladatok
1. Írjunk programot, mely
egyet sípol, ha a
beolvasott szám utolsó számjegye 1 és
kettőt, ha a szám utolsó számjegye 2.
#include
<iostream>
using namespace std;
int main()
{
int x;
cout << "szam = ";
cin >> x;
if (x%10 == 1)
{
cout << "sip" <<
(char)7 << endl;
}
if (x%10
== 2) {
cout << "sip"
<< (char)7 << endl;
cout << "sip"
<< (char)7 << endl;
}
return 0;
}
2. Írjunk programot, mely egyet sípol ha egy
beolvasott szám negatív és kettőt ha a szám
pozitív vagy nulla.
#include
<iostream>
using namespace std;
int main()
{
int x;
cout << "szam = ";
cin >> x;
if (x < 0) {
cout << "sip" <<
(char)7 << endl;
}
else {
cout << "sip"
<< (char)7 << endl;
cout << "sip"
<< (char)7 << endl;
}
return 0;
}
3. Írjunk programot, mely annyit sípol ahány
páros számjegye van egy háromjegyű számnak.
#include
<iostream>
using namespace std;
int
main(){
int x;
cout << "szam = ";
cin >> x;
if (x%2 == 0) {
cout << "sip" << (char)7
<< endl;
}
x = x/10;
if (x%2 ==
0) {
cout << "sip" << (char)7
<< endl;
}
x =
x/10;
if (x%2 == 0) {
cout << "sip" << (char)7
<< endl;
}
x = x/10;
return 0;
}
4. Olvassunk be négy számot, majd írassuk ki őket
olyan sorrendbe, hogy a
legnagyobb az utolsó helyre
kerüljön.
#include
<iostream>
using namespace std;
int
main()
{
int x1, x2, x3, x4, s;
cout << "szam = ";
cin >> x1;
cout << "szam = ";
cin >> x2;
cout << "szam = ";
cin >> x3;
cout << "szam = ";
cin >> x4;
if (x1 > x2) {s
= x1; x1 = x2; x2 = s;}
if (x2 > x3) {s = x2; x2 = x3; x3 = s;}
if (x3 > x4) {s = x3; x3 = x4; x4 = s;}
cout << x1 << " " << x2 << "
";
cout << x3 << " " << x4 <<
endl;
return 0;
}
5. Rendezzünk négy számot
növekvő sorrendbe.
#include
<iostream>
#include <stdlib.h>
using namespace std;
int
main()
{
int x1, x2, x3, x4, s;
cout << "szam = ";
cin >> x1;
cout << "szam = ";
cin >> x2;
cout << "szam = ";
cin >> x3;
cout << "szam = ";
cin >> x4;
if (x1 > x2) {s = x1; x1 = x2; x2 = s;}
if (x2 > x3) {s = x2; x2 = x3; x3 = s;}
if (x3 > x4) {s = x3; x3 = x4; x4 = s;}
cout << endl;
cout << "Meg
nincs rendezve: " << endl;
cout << x1 << " " << x2 << "
";
cout << x3 << " " << x4 <<
endl;
system("pause");
if (x1 > x2) {s = x1; x1 = x2; x2 = s;}
if (x2 > x3) {s = x2; x2 = x3; x3 = s;}
cout << endl;
cout << "Meg nincs rendezve: " <<
endl;
cout << x1 << " " << x2 << "
";
cout << x3 << " " << x4 <<
endl;
system("pause");
if (x1 > x2) {s = x1; x1 = x2; x2 = s;}
cout << endl;
cout << "Rendezve:"
<< endl;
cout << x1 << " " << x2 << "
";
cout << x3 << " " << x4 <<
endl;
return 0;
}
A fenti feladatok standard C
író/olvasó függvényekkel
1. Írjunk programot, mely
egyet sípol, ha a
beolvasott szám utolsó számjegye 1 és
kettőt, ha a szám utolsó számjegye 2.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x;
printf("szam = ");
scanf("%i", &x);
if (x%10 == 1) {
printf("sip %c", 7);
}
if (x%10 == 2) {
printf("sip %c", 7);
printf("sip %c", 7);
}
system("pause");
return 0;
}
2. Írjunk programot, mely egyet sípol ha egy
beolvasott szám negatív és kettőt ha a szám
pozitív vagy nulla.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x;
printf("szam = ");
scanf("%i", &x);
if (x < 0) {
printf("sip %c", 7);
}
else {
printf("sip %c", 7);
printf("sip %c", 7);
}
system("pause");
return 0;
}
3. Írjunk programot, mely annyit sípol ahány
páros számjegye van egy háromjegyű számnak.
#include<stdio.h>
#include<stdlib.h>
int
main(){
int x;
printf("szam = ");
scanf("%i", &x);
if (x%2 == 0) {
printf("sip %c", 7);
}
x = x/10;
if (x%2 == 0) {
printf("sip %c", 7);
}
x = x/10;
if (x%2 == 0) {
printf("sip %c", 7);
}
x = x/10;
system("pause");
return 0;
}
4. Olvassunk be négy számot, majd írassuk ki őket
olyan sorrendbe, hogy a legnagyobb az utolsó helyre
kerüljön.
#include<stdio.h>
#include<stdlib.h>
int
main()
{
int x1, x2, x3, x4, s;
printf("x1= ");
scanf("%i", &x1);
printf("x2= ");
scanf("%i", &x2);
printf("x3= ");
scanf("%i", &x3);
printf("x4= ");
scanf("%i", &x4);
if (x1 > x2) {s = x1; x1 = x2; x2 = s;}
if (x2 > x3) {s = x2; x2 = x3; x3 = s;}
if (x3 > x4) {s = x3; x3 = x4; x4 = s;}
printf("%4i%4i%4i%4i\n", x1, x2, x3, x4);
system("pause");
return 0;
}
5. Rendezzünk négy számot
növekvő sorrendbe.
#include<stdio.h>
#include<stdlib.h>
int
main()
{
int x1, x2, x3, x4, s;
printf("x1= ");
scanf("%i", &x1);
printf("x2= ");
scanf("%i", &x2);
printf("x3= ");
scanf("%i", &x3);
printf("x4= ");
scanf("%i", &x4);
if (x1 > x2) {s = x1; x1 = x2; x2 = s;}
if (x2 > x3) {s = x2; x2 = x3; x3 = s;}
if (x3 > x4) {s = x3; x3 = x4; x4 = s;}
printf("%4i%4i%4i%4i\n", x1, x2, x3, x4);
system("pause");
if (x1 > x2) {s = x1; x1 = x2; x2 = s;}
if (x2 > x3) {s = x2; x2 = x3; x3 = s;}
printf("%4i%4i%4i%4i\n", x1, x2, x3, x4);
system("pause");
if (x1 > x2) {s = x1; x1 = x2; x2 = s;}
printf("Rendezve:\n%4i%4i%4i%4i\n", x1, x2, x3, x4);
system("pause");
return 0;
}