Jump to content

Algoritm Afisare Numar De Cifre Pare Si Impare In C++


Recommended Posts

Posted

Acest algoritm arata numarul de cifre, cifre pare si impare a numerelor dintr-un sir:

 

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
long int n,N,q,Q,p,im,i;
using namespace std;
int main()
{
    for(i=1;i<=10;++i)
    {
        N=n=rand();
        while(n!=0)
        {
            if((n%10)%2==0) p++;
            else ++im;
            n/=10;
            q++;
        }
        q=abs(q);
        printf("Nr=%ld",N);
        Q=8-q;
        while(Q!=0)
        {
            printf(" ");
            Q--;
        }
        printf("Cifre=%ld   Pare=%ld   Impare=%ldn",q,p,im);

        q=p=im=0;
    }
    return 0;
}

Respecta si vei fi respectat.

Posted

Varianta in care scri de la tastatura:

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
long int N[100],q[100],p[100],NR,n,im,Q,i;
using namespace std;
int main()
{
    printf("NR="); scanf("%ld",&NR);
    for(i=1;i<=NR;++i)
    {
        printf("Nr="); scanf("%ld",&n);
        N[i]=n;
        while(n!=0)
        {
            if((n%10)%2==0) p[i]++;
            n/=10;
            q[i]++;
        }
    }
    for(i=1;i<=NR;++i)
    {
        printf("Nr=%ld",N[i]);
        Q=8-q[i];
        im=q[i]-p[i];
        while(Q!=0)
        {
            printf(" ");
            Q--;
        }
        printf("Cifre=%ld   Pare=%ld   Impare=%ldn",q[i],p[i],im);
    }
    return 0;
}

Respecta si vei fi respectat.

  • Moderators
Posted
#include<iostream>
#include<fstream>

dafuq?

 

Oricum, care e scopul programului? Citesti mai multe numere si afisezi nr. de cifre, nr. de cifre pare si nr. de cifre impare?

 

Uite ceva gen: citesti numere pana la intalnirea valorii 0:

#include <iostream>
using namespace std;
int main(void) 
{
	int n,cp=0,ci=0,x,y,s=0;
	cout<<"Se vor introduce numere pana la intalnirea valorii 0"<<'n';
	while (cin>>n && n)
	{
        	y=n;
        	while (y) 
		{
            		if (y%2==0)
                		cp++;
            		else
                		ci++;
            		s++;
            		y=y/10;
        	}
       		x--;
	}
	cout<<"Sirul are "<<s<<" cifre, dintre care: "<<cp<<" pare si "<<ci<<" impare";
	return 0;
}
  • Moderators
Posted

Ba da, dar la stdio.h nu ai nevoie de using namespace std.


App, programu merge si fara bibliotecile alea, iostream, fstream, math.h si stdlib.

  • 4 years later...
Posted
On 12/12/2013 at 8:27 PM, Courage said:

Ba da, dar la stdio.h nu ai nevoie de using namespace std.

 

App, programu merge si fara bibliotecile alea, iostream, fstream, math.h si stdlib.

 

esti sigur? 😂

  • Moderators
Posted
7 minutes ago, FML said:

esti sigur? 😂

iostream iti ofera acces la cin/cout/cerr/etc din namespace-ul std.

printf/scanf sunt definite in stdio.h

Posted
Just now, Courage said:

iostream iti ofera acces la cin/cout/cerr/etc din namespace-ul std.

printf/scanf sunt definite in stdio.h

pai aia e, ca tu ai si cin, cout, deci e eronat ce ai zis.

  • Moderators
Posted
Just now, FML said:

pai aia e, ca tu ai si cin, cout, deci e eronat ce ai zis.

Ma refeream la programelul de aici

Spoiler
On 12/12/2013 at 2:02 AM, Ovidiu29 said:

Varianta in care scri de la tastatura:



#include<iostream>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
long int N[100],q[100],p[100],NR,n,im,Q,i;
using namespace std;
int main()
{
    printf("NR="); scanf("%ld",&NR);
    for(i=1;i<=NR;++i)
    {
        printf("Nr="); scanf("%ld",&n);
        N[i]=n;
        while(n!=0)
        {
            if((n%10)%2==0) p[i]++;
            n/=10;
            q[i]++;
        }
    }
    for(i=1;i<=NR;++i)
    {
        printf("Nr=%ld",N[i]);
        Q=8-q[i];
        im=q[i]-p[i];
        while(Q!=0)
        {
            printf(" ");
            Q--;
        }
        printf("Cifre=%ld   Pare=%ld   Impare=%ldn",q[i],p[i],im);
    }
    return 0;
}

 

 

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.