Skip to content

Commit

Permalink
Create lab1_q8
Browse files Browse the repository at this point in the history
  • Loading branch information
ZanButt authored Jan 9, 2021
1 parent a00af8e commit 612f22a
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lab1_q8
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <stdio.h>
#include <stdlib.h>

/*Write a program that prints backwardsa seven-digit positive integer.
For example, if the integer is 9806593, the program should print 3956089.
You are not allowed to use any function of C standard library other than scanf() and printf().
You are not allowed to use arrays either. For the case when the integer ends with 0, the number printed cannot have leading 0’s (Eg: input 3412400; output 42143).
Hint: Use the division and remainder operators to separate the number into its individual digits.b)Modify your program so it prints backwards any positive integer,
not necessarily a six-digit one. */



int main()
{ /* program prints 7 digit numbers in reverse order but omits leading 0s */
int integer,i,remainder,l,counter=0;
printf("Please input a seven-digit positive integer. ");
scanf("%d",&integer);
for(i=0;i<=6;i++) { // for loop goes from i = 0 to i=6 and runs through the multiple if/else statements below
if(counter != 0){ /* this will run if the counter does not equal to 0,
and that means that if there is a zero it now not omit it as this will be a middle 0 not an ending 0 */
remainder = integer%10;
printf("%d",remainder);
integer = integer/10;
}
else if(integer%10==0){
integer = integer/10;
}
else {
remainder = integer%10;
printf("%d",remainder); /* As soon as the print f statement is used, the counter
increases by one so it runs through the first if loop */
counter +=1;
integer = integer/10;
}
}
return 0;
}

0 comments on commit 612f22a

Please sign in to comment.