#include <bits/stdc++.h> using namespace std ; #define MOD 1000000007 ; #define int long long int int32_t main (){ ios_base :: sync_with_stdio ( false ); cin . tie ( NULL ); #ifndef ONLINE_JUDGE freopen ( "input.txt" , "r" , stdin ); #endif //----Solution-----// int n ; cin >> n ; while ( n != 1 ){ cout << n << " " ; if ( n & 1 ){ // if n is not divisible by 3 then n = n * 3 + 1 ; // multiply it by 3 and add 1 } else { n /= 2 ; // else divide it by 2 } } cout << n ; return 0 ; }
Discover the power of arrays and loops in programming with our latest blog post. We present a clear and concise example in C programming that demonstrates how to efficiently find the largest digit among a series of inputs. Learn our step-by-step approach to gather user input, store it in an array, and employ loops for seamless comparison. Uncover the foundational concepts behind this problem-solving technique and gain insights into the essential role of arrays and loops in programming. Whether you're a beginner or honing your coding skills, this post offers practical knowledge and a deeper understanding of these fundamental programming tools. #include <stdio.h> int main () { int a ; printf ( "How many digits you want to compare: \n " ); scanf ( " %d " , & a ); int num [ a ]; // Declare an array to store the input digits // Input loop: Prompt user to enter digits and store them in the array ...
Description: This blog post presents a C code to find the greatest number among three user-input integers. The code uses scanf to interact with the user and employs an optimized comparison logic to efficiently determine the largest value. By eliminating unnecessary nested if statements, the program becomes more concise and easier to understand. The post includes a commented version of the code, explaining each step of the process for better comprehension. You can also use this way to approach this problem using arrays and loops: Click here . #include <stdio.h> int main () { int a , b , c ; // Prompt the user to enter three integers printf ( "Enter first number: " ); scanf ( " %d " , & a ); printf ( "Enter second number: " ); scanf ( " %d " , & b ); printf ( "Enter third number: " ); scanf ( " %d " , & c ); // Compare the three numbers to fi...
Comments
Post a Comment