Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- **Filename : exercise_1
- **
- **Made by : LIONTI Theo / DESJARDINS Alexis
- **
- **Description : Affichage de nombres régulier dans un intervalle [a;b] saisi par l'utilisateur.
- **
- */
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- int a, b, temp, digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8, i, regulars, y, sum, x, restart, c, w;
- do{
- x=w=0;
- a = b = 129; //allows the loop to start
- printf("Entrez un interval d'entiers [a,b] avec a > -128 et b < 128\n");
- while(a < -127 || a > 127){
- printf("\na = "); //forces user to input values between -127 and 127
- scanf("%d", &a);
- }
- while(b < -127 || b > 127 || a > b){
- printf("\nb = ");
- scanf("%d", &b);
- }
- printf("Les nombres reguliers entre %d et %d sont :", a, b);
- regulars = 0;
- for(y=0;y<=1;y++){
- for(i=a;i<=b;i++){ //converts i to binary. Every digit is stocked in a separate variable
- temp = abs(i);
- digit1 = temp % 2;
- temp = temp / 2;
- digit2 = temp % 2;
- temp = temp / 2;
- digit3 = temp % 2;
- temp = temp / 2;
- digit4 = temp % 2;
- temp = temp / 2;
- digit5 = temp % 2;
- temp = temp / 2;
- digit6 = temp % 2;
- temp = temp / 2;
- digit7 = temp % 2;
- temp = temp /2;
- digit8 = 0;
- if(i<0){ // if working on negative values converts the absolute value to negative value by replacing each digit
- digit8 = 1;
- if (digit7)
- digit7 = 0;
- else
- digit7 = 1;
- if(digit6)
- digit6 = 0;
- else
- digit6 = 1;
- if(digit5)
- digit5 =0;
- else
- digit5 = 1;
- if (digit4)
- digit4 = 0;
- else
- digit4 = 1;
- if(digit3)
- digit3 = 0;
- else
- digit3 = 1;
- if(digit2)
- digit2 =0;
- else
- digit2 = 1;
- if(digit1)
- digit1 = 0;
- else
- digit1 = 1;
- if(!digit1){ //then adds 1
- digit1++;
- }
- else {
- digit1 = 0;
- if(!digit2){
- digit2++;
- }
- else {
- digit2 = 0;
- if(!digit3){
- digit3++;
- }
- else {
- digit3 = 0;
- if(!digit4){
- digit4++;
- }
- else{
- digit4 = 0;
- if(!digit5){
- digit5++;
- }
- else{
- digit5 = 0;
- if(!digit6){
- digit6++;
- }
- else{
- digit6 = 0;
- if(!digit7){
- digit7 = 1;
- }
- else{
- digit7 = 0;
- }
- }
- }
- }
- }
- }
- }
- }
- sum = digit1+digit2+digit3+digit4+digit5+digit6+digit7+digit8; //if the number is regular, the sum of its digits is 4
- if(sum == 4){
- if(y == 0){
- regulars++;
- }
- if(y==1){
- x++;
- if(x == regulars-1){
- printf(" %d et", i)
- }
- else if(x==regulars){
- printf(" %d.\n", i);
- w=1;
- }
- else
- printf(" %d,", i);
- }
- }
- }
- }
- if(regulars==0){
- printf(" ERREUR - il n'y a pas d'entiers reguliers entre %d et %d", a, b); //ouptuts
- // return 0;
- }
- if(w==1){
- do{
- while(c != '\n' && c != EOF) //emptying buffer
- {
- c = getchar();
- }
- printf("\n\nRelancer ? [Y/N] : ");
- temp = 0; //alloxs the user to start again with != values
- scanf("%c", &temp);
- }while(temp != 'y' && temp != 'Y' && temp !='n' && temp != 'N');
- if(temp=='y'||temp=='Y')
- restart = 1;
- if(temp =='n' || temp =='N')
- restart = 0;
- }
- }while(restart);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement