Jump to content
Ovidiu29

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

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.

Share this post


Link to post
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.

Share this post


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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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? 😂

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}

 

 

Share this post


Link to post
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...