Jump to content

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


Ovidiu29

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Moderators
#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;
}
Link to comment
Share on other sites

  • 4 years later...
  • Moderators
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;
}

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • 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.