<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[aus gerade zahle rausfiltern]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>ich muss von einer zahl gerade zahle rausfiltern und ungerade lassen<br />
z.b 123456 -&gt; 135</p>
<p>wo stimmt meine programmlogik nicht? in if schleife gehe ich doch nur dann wenn die zahl ungerade ist und das mache ich bis letztezahl</p>
<pre><code>#include&lt;iostream&gt;
using namespace std;

int main(){

	int zahl, res = 1, i = 1;

	cin &gt;&gt; zahl;

	for( ; zahl ; zahl= zahl/10){

		if(zahl%10/2 != 0){

			res = zahl%10*i;

			i *=10;

		}

	}

	cout &lt;&lt; res &lt;&lt; endl;

	return 0;

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/310515/aus-gerade-zahle-rausfiltern</link><generator>RSS for Node</generator><lastBuildDate>Tue, 04 Aug 2026 13:18:54 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/310515.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 13 Nov 2012 17:30:15 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to aus gerade zahle rausfiltern on Tue, 13 Nov 2012 17:30:15 GMT]]></title><description><![CDATA[<p>Hallo Leute,</p>
<p>ich muss von einer zahl gerade zahle rausfiltern und ungerade lassen<br />
z.b 123456 -&gt; 135</p>
<p>wo stimmt meine programmlogik nicht? in if schleife gehe ich doch nur dann wenn die zahl ungerade ist und das mache ich bis letztezahl</p>
<pre><code>#include&lt;iostream&gt;
using namespace std;

int main(){

	int zahl, res = 1, i = 1;

	cin &gt;&gt; zahl;

	for( ; zahl ; zahl= zahl/10){

		if(zahl%10/2 != 0){

			res = zahl%10*i;

			i *=10;

		}

	}

	cout &lt;&lt; res &lt;&lt; endl;

	return 0;

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2270529</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270529</guid><dc:creator><![CDATA[khati]]></dc:creator><pubDate>Tue, 13 Nov 2012 17:30:15 GMT</pubDate></item><item><title><![CDATA[Reply to aus gerade zahle rausfiltern on Tue, 13 Nov 2012 18:30:35 GMT]]></title><description><![CDATA[<p>Das einfachste wäre, die Zahl in einen String umzuwandeln und das Ergebnis zu filtern.<br />
Hier mal für positive Ganzzahlen:</p>
<p><strong>C++98/03:</strong></p>
<pre><code>int zahl = 123456;

        std::stringstream stream;
        stream &lt;&lt; zahl;
        std::string str = stream.str();

        std::string::iterator iter = str.begin();
        while(iter != str.end())
                if((*iter - '0') % 2 == 0)
                        iter = str.erase(iter);
                else 
                        ++iter;

        stream.str(str);
        stream &gt;&gt; zahl;

        std::cout &lt;&lt; zahl;
</code></pre>
<p><strong>C++11:</strong></p>
<pre><code>int zahl = 123456;

        std::string str = std::to_string(zahl);

        str.erase(std::remove_if(str.begin(), str.end(), [](char const c){return (c - '0') % 2 == 0;}),
                  str.end());

        zahl = std::stoi(str);

        std::cout &lt;&lt; zahl;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2270537</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270537</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Tue, 13 Nov 2012 18:30:35 GMT</pubDate></item><item><title><![CDATA[Reply to aus gerade zahle rausfiltern on Tue, 13 Nov 2012 17:55:09 GMT]]></title><description><![CDATA[<p>ich habe keine Programmiererfahrung und wir dürfen nur mit schleifen machen und nicht mit arrays</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2270541</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270541</guid><dc:creator><![CDATA[khati]]></dc:creator><pubDate>Tue, 13 Nov 2012 17:55:09 GMT</pubDate></item><item><title><![CDATA[Reply to aus gerade zahle rausfiltern on Tue, 13 Nov 2012 17:55:19 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p><strong>C++11:</strong></p>
<pre><code>str.erase(std::remove_if(str.begin(), str.end(), [](char const c){return (c - '0') % 2 == 0;}),
                  str.end());
</code></pre>
</blockquote>
<p>Besser:</p>
<pre><code class="language-cpp">std::remove_copy_if(s1.begin(), s1.end(), std::back_inserter(s2),
                      [](char c){return (c-'1')%2; });
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2270542</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270542</guid><dc:creator><![CDATA[zehnelf]]></dc:creator><pubDate>Tue, 13 Nov 2012 17:55:19 GMT</pubDate></item><item><title><![CDATA[Reply to aus gerade zahle rausfiltern on Tue, 13 Nov 2012 18:10:56 GMT]]></title><description><![CDATA[<p>zehnelf schrieb:</p>
<blockquote>
<p>Sone schrieb:</p>
<blockquote>
<p><strong>C++11:</strong></p>
<pre><code>str.erase(std::remove_if(str.begin(), str.end(), [](char const c){return (c - '0') % 2 == 0;}),
                  str.end());
</code></pre>
</blockquote>
<p>Besser:</p>
<pre><code class="language-cpp">std::remove_copy_if(s1.begin(), s1.end(), std::back_inserter(s2),
                      [](char c){return (c-'1')%2; });
</code></pre>
</blockquote>
<p>Wenn schon zwei Strings, dann gleich mit <code>copy_if</code> .</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2270548</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270548</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Tue, 13 Nov 2012 18:10:56 GMT</pubDate></item><item><title><![CDATA[Reply to aus gerade zahle rausfiltern on Tue, 13 Nov 2012 18:22:02 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<pre><code>for(std::string::iterator iter = str.begin();iter != str.end();++iter)
            if((*iter - '0') % 2 == 0)
                iter = str.erase(iter) - 1;
</code></pre>
</blockquote>
<p>Das sollte UB sein, wenn die erste Ziffer ungerade ist. Außerdem ist das O(n²). Bei sowas sollte man immer <code>remove_if</code> verwenden (oder der Trick mit dem <code>swap</code> , wenn die Reihenfolge keine Rolle spielt).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2270556</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270556</guid><dc:creator><![CDATA[ipsec]]></dc:creator><pubDate>Tue, 13 Nov 2012 18:22:02 GMT</pubDate></item><item><title><![CDATA[Reply to aus gerade zahle rausfiltern on Tue, 13 Nov 2012 18:28:15 GMT]]></title><description><![CDATA[<p>ipsec schrieb:</p>
<blockquote>
<p>Sone schrieb:</p>
<blockquote>
<pre><code>for(std::string::iterator iter = str.begin();iter != str.end();++iter)
            if((*iter - '0') % 2 == 0)
                iter = str.erase(iter) - 1;
</code></pre>
</blockquote>
<p>Das sollte UB sein, wenn die erste Ziffer ungerade ist. Außerdem ist das O(n²). Bei sowas sollte man immer <code>remove_if</code> verwenden (oder der Trick mit dem <code>swap</code> , wenn die Reihenfolge keine Rolle spielt).</p>
</blockquote>
<p>Verdammt! Du hast Recht, der Trick mit dem -1 war nicht gut durchdacht...</p>
<p>Ich wollte halt versuchen, das ohne eine zusätzliche Funktion zu implementieren...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2270559</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270559</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Tue, 13 Nov 2012 18:28:15 GMT</pubDate></item><item><title><![CDATA[Reply to aus gerade zahle rausfiltern on Tue, 13 Nov 2012 18:30:04 GMT]]></title><description><![CDATA[<p>Hab es jetzt mit 'ner while-Schleife gelöst:</p>
<pre><code>std::string::iterator iter = str.begin();
        while(iter != str.end())
                if((*iter - '0') % 2 == 0)
                        iter = str.erase(iter);
                else 
                        ++iter;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2270560</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270560</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Tue, 13 Nov 2012 18:30:04 GMT</pubDate></item><item><title><![CDATA[Reply to aus gerade zahle rausfiltern on Tue, 13 Nov 2012 19:45:24 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>Hab es jetzt mit 'ner while-Schleife gelöst:</p>
</blockquote>
<p>Da du offensichtlich den Hinweis auf O(n²) nicht verstehst: Meine Variante mit <code>std::copy_if</code> ist ca. 100x schneller als deine Schleifenlösung (kein Witz!!!!!!1011).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2270573</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270573</guid><dc:creator><![CDATA[zehnelf]]></dc:creator><pubDate>Tue, 13 Nov 2012 19:45:24 GMT</pubDate></item><item><title><![CDATA[Reply to aus gerade zahle rausfiltern on Tue, 13 Nov 2012 19:52:21 GMT]]></title><description><![CDATA[<p>Und wenns inplace und eine Schleife sein soll:</p>
<pre><code class="language-cpp">std::string::const_iterator in = s1.begin(), end=s1.end();
std::string::iterator out = s1.begin();
for (; in != end; ++in)
    if ((*in - '0')%2)
        *out++ = *in;
s1.erase(out, s1.end());
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2270576</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270576</guid><dc:creator><![CDATA[zehnelf]]></dc:creator><pubDate>Tue, 13 Nov 2012 19:52:21 GMT</pubDate></item><item><title><![CDATA[Reply to aus gerade zahle rausfiltern on Tue, 13 Nov 2012 21:01:50 GMT]]></title><description><![CDATA[<p>khati schrieb:</p>
<blockquote>
<p>ich habe keine Programmiererfahrung und wir dürfen nur mit schleifen machen und nicht mit arrays</p>
</blockquote>
<pre><code>#include &lt;iostream&gt;

int main()
{ 
    int number = 1234567890;
	int result = 0;
	int pos = 1;

	for( ; number; number /= 10 ) {

		if( number % 10 % 2 == 0 ) {

			result += number % 10 * pos;
			pos *= 10;
		}
	}

	std::cout &lt;&lt; result &lt;&lt; '\n';
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2270594</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270594</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Tue, 13 Nov 2012 21:01:50 GMT</pubDate></item><item><title><![CDATA[Reply to aus gerade zahle rausfiltern on Tue, 13 Nov 2012 23:22:17 GMT]]></title><description><![CDATA[<p>kann so mit schleifen wie ich gemacht habe nicht funktionieren?<br />
was ist fehler in meinem Logik?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2270614</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270614</guid><dc:creator><![CDATA[khati]]></dc:creator><pubDate>Tue, 13 Nov 2012 23:22:17 GMT</pubDate></item><item><title><![CDATA[Reply to aus gerade zahle rausfiltern on Wed, 14 Nov 2012 00:03:03 GMT]]></title><description><![CDATA[<p>khati schrieb:</p>
<blockquote>
<p>was ist fehler in meinem Logik?</p>
</blockquote>
<p>Wenn du eine gerade Zahl durch 2 teilst, kommt nicht 0 heraus.</p>
<p>Probier's mal aus <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2270615</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2270615</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Wed, 14 Nov 2012 00:03:03 GMT</pubDate></item></channel></rss>