0% found this document useful (0 votes)
14 views30 pages

C Programming Level 4-5

The document contains multiple C programming code snippets that prompt the user for a number, manipulate the digits in various ways, and print the results. It includes operations such as reversing the last two digits of a four-digit number, modifying two-digit and three-digit numbers, and checking conditions based on the digits. The code snippets demonstrate basic input/output operations and arithmetic manipulations in C.

Uploaded by

suhealahamed27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
14 views30 pages

C Programming Level 4-5

The document contains multiple C programming code snippets that prompt the user for a number, manipulate the digits in various ways, and print the results. It includes operations such as reversing the last two digits of a four-digit number, modifying two-digit and three-digit numbers, and checking conditions based on the digits. The code snippets demonstrate basic input/output operations and arithmetic manipulations in C.

Uploaded by

suhealahamed27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
OG IDE € jo exooeR 1 sinclude 2 3- int main() { ae a int nun, lastTwoDigits, reversedwum; 5 6 77 Ask the user to input a four-digit number 7 printf("Enter a four-digit number: “); a scanf("ad", &num); 9 10 #/ Extract the last two digits u last TwoDigits = num % 100; 12 13 4) Reverse the last two digits 4 int lastDigit = lastTwoDigits % 10; 1s int secondLastDigit = lasttwoDigits / 10; 16 reversedwum = lastDigit * 10 + secondLastDigit; 7 18 + Construct the final number with reversed last two di 19 int finalNum = (num ‘ 100) * 100 + reversedwun; 20 2 // Print the final number 22 printf("The number with reversed last tuo digits is: 23 24 return 0; Copy > Rup > RunsURL. (Generates URL a8 well) Time(sec) :0,001 Memory( MB) : 2.609375 Output: ‘copy or a four-digit rember: The musber with reversed Last twa digits is: 9516 OG IDE c ° e#eo/Rr 1 Finclude 2 Cael 3+ int maint) ¢ 4 int num, lastTwoDigits, reversedNum; ce 5 6 dé Ask the user to input a four-digit number dave 7 printf("Enter a four-digit number: "); 8 scanf("xd", &num); Fea 9 PHP 10 é/ Extract the last two digits "1 lastTwoDigits = nur % 100; Pythons | 12 veens | 53s Reverse the last two digits seaa | 14 int lastDigit = lastTwoDigits % 10; 15 int secondLastDigit = lastTwoDigits / 10; swift | 16 reversedmun = lastDigit * 10 + secondLastOigit; v Rust 18 // Construct the final number with reversed last two d: 19 int finalllun = (num / 100) * 100 + reversedWun; R 2 /é Print the final number 22 printf("The number with reversed last two digits is: return 0; Time{sec) : 0.004 ‘Memory(MB) : 292578125 Output: Copy er a four-digit rumber: The number aith reversed Last twa digits is: 3805 = OG IDE Ea c ° C#S07S on | i include cH 3+ int main() { | 4 int num, lastTwoDigits, reversedNum; cr 5 } 6 #7 Ask the user to input a four-digit number Java 7 printf("Enter a four-digit number: "); (8 scanf("%d", &num); Perl 9 pup | 10 // Extract the last two digits 1 lastTwoDigits = num % 100; 12 on 8 ee | 13 ¢f Reverse the last two digits sea | 14 int lastDigit = lastTwoDigits % 10; | 15 int secondLastDigit = lastTwoDigits / 10: Swift 16 reversedNum = lastDigit * 10 + secandlastDigit: | 17 Rust 18 ## Construct the final number with reversed last two dig Ganp Wate int finalNum = (num / 100) * 100 + reversedNul etang | 99 . | 21 #7 Print the final number | 22 printf("The number with reversed last two digits is: %d! 23 Node JS | 24 return 0; H™L us| 25} 2 ose1 Copy > Run > Run+URL (Generates URL as wall) Time(sec) : 0.004 Memory(MB) : 2.859375 Output: Copy a four-digit number; The number with reversed last two digits is: 9510 oem © ° C#eDsE CH 1 #include a cH 3- int maint 4 int num, lastTwoDigits, reversedtum; ce Ss 6 /7 Ask the user to input a four-digit number ves | 7—oprintf("Enter a four-digit number: "); 8 scanf("%d", &num); va | § PHP 10, /? Extract the last twa digits WwW LastTwoDigits = num % 100; Ww = ne 13 ff Reverse the last twa digits Scala 14 int lastDigit = lastTwoDigits % 10; | 45 int secondLlastDigit - lastInoDigits / 10; swt | 18 reversedNum = lastDigit * 10 + secondLast0igit; , 17 Rust 18 /f Construct the final number with reversed last two di) |} 19 int finalNum = (num / 100) * 100 + reversedNum; courg | ae iz 21 /f Print the fimal number 22 printfc*The nunber with reversed last two digits isi %d Nodeus | 2% 24 return 0; wmiass) 25} | » ° 3859 Copy > Run > RunFURL (Generates URL as well) Time(sec) 0.002 Memory(MB) : 2.94140625 Output: Copy * a four-digit number: The number with reversed last two digits is: 3695 OG IDE | ¢ 60 C#HeD’R cH i #inelude onma | 3+ int main() ¢ 4 int num, modifiedNum; oF 5 6 // Ask the user to input a two-digit number v8 ? printf("Enter a two-digit number: "); a scanf("%d", &num): Pad a PHP 10 // Set the ones digit to 0 n modifiedNum = (num / 10) * 10; Pythons | 12 ‘ . 13 // Print the modified number Scala 4 printf("The modified number is: %d\n", modifiedNum); 15 ‘Swift 16 return 0; 17 } aust Solang ® Node JS HTML 845 ¥. 95 Copy > Run > Time(sec) : 0.003 Output: Enter a two-digit number: > Run+URL (Generates URL as well) Memory(MB) : 2.8984375 Copy The modified number is: 90 — #include | Cee Cred ce Java Perl PHP Python 3 Scala ‘swift Rust colang R Node JS HIML & Js) > > Time(sec) : 0.004 1 co 3 4 5 6 7 a 9 10 " 12 13 14 18 16 17a} Output: + int main() £ int num, modifiedNun; d/ Ask the user to input a two-digit number printf("Enter a two-digit number: "); scanf("%d", &num); // Set the ones digit to 0 modifiedum = (num ¢ 10) * 10; J/ Print the modified number printf("The modified number is: return 0; Copy > Run sed\n", modi fiedNum) ; > Run+URL (Generates URL a8 well) Enter a two-digit number: The modified number is Memory(MB) : 2.9375 Copy 10 c lo CRGONH ore 1 #inelude i: Cred 3+ int main() { a int num, modifiecNum; ce 5 6 JJ Ask the user to input a tno-digit number Jove | 7 printf("Enter a two-digit number: *); 8 scanf("éd", &num); Pal 3 PHP 10 // Set the tens digit to 1 i modifiedNum = (1 * 10) + (num % 10); 12 Python nna | 43 1J Print the modified number Scala 14 printf("The modified number is: %d\n", modifiedNum); 15 Swift 16 return 0; 7} ust Golang R Node Js HTML Bus = 95 copy > Run > RUN+URL (Generates URL as well) Time(sec) : 0.004 Memory(MB) : 2.859375 Output: Copy Enter a two-digit number: The modified number is: 15 OG IDE exeoe i 2 cH 3+ int main() if 4 ‘int num, modifiedNum; ce s 6 J Ask the user to input a two-digit number veve 7 printf("Enter a two-digit number: "); 8 scanf("%d", Snum): rat | @ Poe 10 // Set the tens digit to 1 11 modifiedNum = (1°10) + (rum % 10); 12 ih Prons | 53 JV Print the modified number Scala 14 printf("The modified number is: Kd\n", modifiedNum); 15 ‘Swift 16 return 0; ay Rust Goleng R Node JS HTL aus * «2 Copy > Run > > RunsUR (Generates URL as well) Time(sec) 0.003 Memory(MB) ° 28359375 Output Copy Enter a two-digit number: The modified number ix: 12 = OG IDE | i e#e078 oe 1 #include 2 cea 3+ int main) f | 4 int num, modi fiedNu ct 5 | 6 JW Ask the user to input a three-digit number Java 7 printf("Enter a three-digit number: | 3 seanf("&d", &num); Per 3 ar lee JJ Set the ones digit to 2 | modifiedNun = (num / 10) * 10 + 2; rons [eda Phen: te 74 Print the modified nunber san | 14 printf("The modified number is: sdin", modi fiedNum) ; 15 Swit | 16 return 0; 7 31 rust Golang ® Node JS HTML 8us oes copy > Run ° > RuntURL (Generates URL as well) Time(sec) | 0.003 Memory(MB) : 2.921875 Output Copy Enter @ three-digit number: The modified sunber is: 692 = OG IDE ° Cxebes i cre 7 include Run > > RUntURL (Generates URL a wll) Time(sec) : 0.001 Memory(MB) | 2.84375 Output Copy Enter @ three-digit number: The modified nunber is: 182 — OG IDE CHEO?E 1 #include 2 cea | 3+ int main) { 4 int number, result; ce 5 printf("Enter a number: " 6 scanf("%d", &number): Java 7 8 result = number - (number % 2) * 5; Peri 9 PHP a printf("Result: %d\n", result); Python 3 ie ; return 03 Sela | 14 ‘Swift Rust Golang R Node JS. HTML & JS) #1) 195 Copy > Run > Time(sec) ; 0.003 Output: Enter a number: Result: > Run+URL (Generates URL as well) Memory(MB) : 2.875 Copy 199 = 6 we ze S ° #eDd. ce 1 #include 2 cag | 3+ int main) { 4 int number, result; ca 5 printf("Enter a number: 6 scanf("kd", &number); Java 7 | 8 result = number - (number % 2) * 5; Perl 9 TT i printf(*Result: din", result); ] 12 return 0; hon 3 : Freon? Tea Scala 14 Swift rust Golang 8 Node JS HTML & 18 = 182 Copy > Run * > RuntURL (Generates URL as well), Time(sec) : 0.002 Memory(MB) ° 289453125 Output: ‘Copy Enter a number: Result: 182 Ez ° euenwen ae 1 #include a ced 3> int main(y { 4 int num, tensDigit, isodd, result; ce 5 6 J/ Ask the user to input a number Java 7 printf ("Enter a number: "); a scanf("d", &num); Pel a ap | to 1/ Extract the tens digit "1 tensDigit = (num / 10) % 10; 12 en 2 Punon3 | 43 14 Check if the tens digit is odd scan | 14 isOdd = tensDigit & 1; 15 swt | 16 J/ Subtract 5 from the number if the tens digit is odd 17 result = num - (isOdd << 2); fust | 18 19 J/ Print the result Solang | 30 printf("Result; d\n", result); 2 e 22 return 0; Node ss | 23 HTML &.us wt ces Copy > Run > > Run+URL (Generates URL as well) Time(sec) : 0.002 Memory(MB): 2.8984375 Output: Copy Enter a number: Result: 685 He lo exeos/ CH 1 #include 2 cHid | 3+ int maing) { 4 int num, tensDigit, isOdd, result cH 5 6 Jf ksk the user to input a number Java 7 printf("Enter a number: "); mar : scanf("id", &num); pup | (10 Jf Extract the tens digit " tensDigit = (num f 10) % 10; Python3 | 12 5 ; 13 Jf Check if the tens digit is odd Scala | 14 isOdd = tensDigit 2 1; 15 swift | 16 Jf Subtract 5 from the number if the tens digit is odd 7 result = num - (isOdd << 2); Rust | 18 19 Jf Print the result Golang 20 printf("Result: %d\n", result); ; 21 22 return Q; Nodes | 23 HTML AS) & win Copy > Run e > Run#URL (Generates URL as well) Time(sec) : 0.003 Memory(MB) : 2.8984375 Output: Copy Enter a nusber: Result: 89163 — 1 #include OG IDE eeeoo. cor 2 cia | 3+ int maint) £ 4 int num; ce 5 printf("Enter a two-digit number: "); 6 scanf("%d", &num); ave 7 8 /f Subtract 5 if the sum of digits is odd Pert 9 rum -= ((num / 10+ num % 10) & 1) * §; PHP jo Ww printf("Result: %d\n", num); 12 return O; Python 3 : 13} scala | 14 ‘Sweift ust olang R Node Js HTML &.5| t * 95 ‘Copy > Run > Time(sec) : 0,000 Output: Enter a two-digit number: Result 95 > RunFURL (Generates URL aswell] Memory(MB) : 2.90234375 Copy OG IDE Hm: lle Gxeo-g cH 1 #include 2 ceria | 3+ int main() { 4 int num; ce 5 printf("Enter a two-digit number: "); 6 seanf("%d", &num) ; eva 7 8 1 Subtract 5 if the sum of digits is odd va 9 num -= ((num / 10 + num % 10) & 1) * 5; 10 PHP | oad printf("Result: d\n", num); 12 return 0; non 3 i Freon’ ig Scala 14 Seat Rust Golang R Node ws HTML & us = R Copy > Run > Run+URL (Generates URL as well) Time(sec) 0.003 Memory(MB) : 2.84375 Output Copy Enter a two-digit number: Result: 67 EE 5 jo Cee nr ue {1 include 2 cred, 3- int main() { 4 int num, result; ce 5 6 printf("Enter a three-digit number: "); Java 7 scanf(*%d", num); 8 Ped 9 result = num - 5 - ((num / 100 % 10 * num % 10) & 1); PHP 10) W printf("Result: %d\n", result); 2 hon 3 nee 13 return 0; scala 14} ‘Swift Rust Solang R Node JS: HTML us ® 595 Copy > Run > > Run+URL (Generates URL as well) Time(sec) : 0.004 Memory(MB) : 2.7851 5625 Output: Copy Enter a three-digit number: Result: 590 mm: 1 \o cH 1 #include 2 orig | 3 int main() 4 int num, result; cr 5 | 6 printf("Enter a four-digit number: "); Java ? scanf("md", Enum); a Perl 2 result = num - 5 - ((num / 1000 % 10 * num / 100 % 10) | ae | 10 "1 printf("Result: %d\n", result); 12 hon ae 13 return 0; Scala 14} ‘Swat Rust Golang R Node JS WIM & as { © 505 Copy > run > > Run+URL (Generates URL as welll) Time(sec) : 0.004 Output: Enter 3 four-digit number: Result: OG IDE C#etw’x Memory(MB) : 2.88671875 ‘Copy 7500 = OG IDE mM: e exonses cH 7 Winclude 2 CH4 3-int main() { 4 int number; ce 5 printf("Enter a four-digit number: "); 6 scanf("%d", &number}; Java 7 8 int tens_digit = (number / 10) % 10; Perl 9 int hundreds_digit = (number / 100) % 10; 10 PHP oa int result = number - (tens_digit == hundreds digit ? ‘ 12 3 Prthon'S Tis printf("Result: %d\n", result); ‘Scala 14 15 return 0; Swift 16 } VW Rust Golang R Node JS HTML & JS) 2 | 7595 Copy > Run * > RuntURL (Generates URL as well) Time(sec) : 0.003 Memory(MB) : 2.875 Output: copy Enter a four-digit number: Result: 7595 OG IDE a “#include ] oa = int main() { aint number ; ca printf("enter a four-digit number: “); scanf("%d", number); dave ; int tens_digit = (number / 10) % 10; ie int hundreds_digit = (number / 100) % 10; id int result = number - (tens_digit == hundreds_digit ? ¢ Python 3 printf("Result: ‘d\n", result): Soala return 0; sft } ust Golang R Nodes HTML & JS) ® ll am copy > Run > > Run+URL (Generates URL as weil) Time(sec) : 0.007 Memory(MB) : 2,83203125 Output: Cony Enter a four-digit number: Result: 3767 ma: lo C#HeDWH cr i #include cetg | 3> ant main() | 4 int num, first_digit, second_digit; ce 5 é printf("Enter a two-digit number: "); Java a scanf("ad", &num); g Pet! 9 df Extracting digits PHP 10 first_digit = num / 10; al second_digit = num % 10; 12 Peet! i! Checking if the sum of digits is 10 seala | 14- if (first_digit + second_digit -- 10) { | 45, print?("Successin"); Swit | 16- J else { | 17 print?("Failure\n"); Rust | 18 | 19 Golang | 20 return 0; R 21} Nowe JS HTML JS) = 56 Copy > Run > > RuntURL (Generates URL as well) Time(sec) : 0.003 Memory(MB) : 2.85546875 Output: Copy Enter a two-digit number: Failure _ OG IDE Ea ° exooe oes T include 2 cae 3+ int main() { 4 int num, first_digit, second_digit; ca 5 6 printf("enter a two-digit number: ™ Java 7 scanf("%d", anum); 8 Lies 2 i Extracting digits pup | (10 first_digit = num / 10; 11 second digit - num % 10: 12 won 8 Pynen$ | 43 vf Checking if the sum of digits is 10 Scola 14> if (first_digit + second_digit 10) { 15 printf("Successin™); ‘Swift 16° } else { | 17 printf(*Failure\n"); Rust 18 } 19 Golang 20 return 0; 5 2} Node Js HTML & J5) 2 7 Copy > Run = > Run+URL (Generates URL as well) Time(sec) : 0.003 Memory(MB) : 29140625 Output: Copy Enter a two-digit number: success = OG IDE ma CH 7 #include 2 CH4 3+ int main() ¢ 4 int num, first_digit, second_digit, third_digit; ce 5 6 printf("Enter a three-digit number: "); Java Ez scanf("%d", &num); 8 Perl 9 /# Extracting digits pup | 10 first_digit = num / 100; W second_digit = (num / 10) % 10; Python 3 fe third_digit = num % 10; Seale | 14 i Checking if the sum of digits is 10 1S- if (first_digit + second digit + third digit == 10) ¢ Swift 16 printf("Success\n"); Wy else { Rust 18 printf("Failure\n"); 19 Golang | 29 t : 2 return 0; 225} Node JS HTML& JS) ® || 986 Copy > Run = > RuntURL (Generates URL as well) Time(sec) : 0.001 Memory(MB) : 2.828125 Output: cay Enter a three-digit number: Failure = OG IDE i: ° C#eDez cH 7 Winclude 2 cea | 3+ int main() [f 4 int num, first_digit. second_digit. third_digit: ce 5 6 print ("Enter a three-digit number: ” a T scant¢"%d", &num); & Perl 9 +f Extracting digits oe (18 first_digit = num / 100; " second_digit = (num / 10) % 10; Python 3 ie third_digit = num % 10; scala) 14 1 Checking if the sum of digits is 10 15+ if (first_digit + second_digit + third digit == 10) { ‘Sunt 16 printf("Successin™): te? } else { ust 18 printf("Failurexn"); 19 + Gelana | 2g = 2 return 0; 223 Node JS. HTML 8S e@ lfin Copy > Run ° > Run#URL (Generates URL aa welll) Time(sec) : 0.002 Memory(MB) : 2.890625 Output: Copy Enter a three-digit number: Success ia ° xeon de cH cota | 3+ int main() ¢ | 4 int num, ones_digit, hundreds_digit; ce 5 6 printf("€nter a three-digit number: "); Jews 7 scanf("%d", &num); | 3 Pan: 9 // Extracting digits op ee ones_digit = num % 10; ou hundreds digit = num / 100; pyttona | 12 | 13 i Checking if the sum of ones digit and hundreds digit sel 14° if Cones_digit + hundreds_digit < 10) { 15 printf("Success\n"); Swit | 16+ else { | 47 printf("Failure\n"); Rust 18 13 Golang 20 return 0; 21} R Node JS HTML & JS) t * 509 ‘copy > Run = > Run+URL (Generates URL as well) Time(sec) | 0.003 Memory(MB) : 2.92578125 Output: Copy Enter a three-digit number: Failure m= ° e#ooe7n #include cH 1 u ceed Bint main() € | 4 int num, ones_digit, hundreds_digit: ce 5 6 printf("Enter a three-digit number: Java a scanf("d", &num); | 8 Pan 9 // Extracting digits oe ee ones_digit = num % 10; | a9 hundreds digit = num / 100; Pythons | 12 : Eo | 13 /# Checking if the sum of ones digit and hundreds digit Scala | 14- if (ones_digit + hundreds_digit < 10) { | 45 printf ("Success\n"); Switt 16- y else { | 17 printf("Failure\n" ); Rust 18 } 19 Golang | 20 return 0; 5 21 Node Js TML & JS { oo sis Copy > Run ° > RumtURL (Generates URL as well) Time(sec) : 0.003 Memory(MB) : 2,86328125 Output: Copy Enter a three-digit number: Success OG IDE : % e#e07R #include > int main) int num, tens_digit, hundreds_digit; printf("Enter a four-digit number: scanf("%d", &num); ff Extracting digits tens_digit = (num / 10) % 10; hundreds_digit = (num / 100) % 10; Pythona | 12 13 // Checking if the sum of tens digit and hundreds digit scala | 14° if (tens_digit + hundreds_digit > 9) { 15 printf("Success\n"); smk | 16- jf else { 7 princt("Failure\n"); Rust | 18 + 19° Goleng 29 return 0; R 21 3 Node Js HTML & us * 7529 Copy > Run & > Run+URL (Generates URL as well) Time(sec) : 0.004 Memory(MB) : 2.859375 Output: Copy Enter a four-digit number: Failure OG IDE ° exeoen #include saint mainc) { int num, tens_dig hundreds digit; printt(venter a four-digit number: scanf("%d", &num); J/ Extracting digits tens_digit = (num / 10) % 10; hundreds_digit = (num / 190) % 19; Ji Checking if the sum of tens digit and hundreds digit if (tens_digit + hundreds_digit > 9) { printf("Success\n"); } else { printf(*Failurein" t return ; Copy > Run > RuntUR (Generates URL as weil) Time(sec) : 0.002 Memory(MB) : 2.84375 cr 1 2 cura | 3 4 ce 5 6 Java 7 8 Perl 9 10 pap | 2 hon 3 en Scala | 14° 15 Swit | 16 W Rust | 1g 19 Golang | 20 7 21 >| Node JS HTML 8 us| 2 | s285 > Output: Copy Enter a four-digit number: success OG IDE ce °o Cx#eO’N 7 #include { 2 ced 3- int maint) if 4 int num, tens_digit, hundreds digit, thousands_digit, ¢ ce 5 6 printf("Enter a four-digit number: "); Java 7 scanf("%d", &num); 8 a 9 17 Extracting digits oe ee ones_digit = num % 10; 1 tens_digit = (num / 10) % 10; prions az hundreds_digit = (num / 100) % 10; 2 thousands_digit = {num / 1000) % 10; seala 14 15 // Checking if the sum of tens digit and hundreds digit ‘Swat 16 /} and if one of the digits is more than 7 17+ if ({tens_digit + hundreds digit == 10) && (tens_digit Fut | 18 printf("Success\n"); 19>} else { Golang | 30 printf("Failure\n"); 21 + B 22 23 return 0; Nodes | 54 HTML & Js FY ssas copy > Run il > Run+URL (Generates URLs well) Time(sec) : 0.001 Memory(MB) : 2.8671875 Output: Copy Enter a four-digit number: Failure OG IDE Hm: oo GCReOV7H cH py tinelude stato. cH 3- int main() { 4 int num, tens_digit, hundreds_digit, thousands digit, ¢ cs 5 6 printf("Enter a four-digit number: Java 7 seanf("%d", &num); a Rest 8 vf Extracting digits up Wed ones_digit = num % 10; " tens_digit = (num / 10) % 10; python3 | 12 hundreds_digit = (num / 100) % 10; 13 thousands digit = (num / 1000) % 10; Scala 14 15 i1 Checking if the sum of tens digit and hundreds digit Swift 16 // and if one of the digits is more than 7 V7- if ({tens_digit + hundreds_digit == 10) £& (tens_digit Rust 18 printf(“Success\n"); 19- else { Golang | 20 printf("Failure\n"); R 21 22 23 return Q; Nodes 3h HTML & US ° 9286 copy > Run = ‘> RuntURL (Generates URL as well) Time(sec) ; 0.003 Memory(MB) : 2.8671875 ‘Output: ‘Copy Enter a four-digit number: Success

You might also like