Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 10 Online
OpenStudy (anonymous):

Implement the following function. Do not use any local variables or loops. void pattern(unsigned int n) // Precondition: n > 0; // Postcondition: The output consists of lines of integers. The first line // is the number n. The next line is the number 2n. The next line is // the number 4n, and so on until you reach a number that is larger than // 4242. This list of numbers is then repeated backward until you get back // to n. /* Example output with n = 840: 840 1680 3360 6720 6720 3360 1680 840

OpenStudy (nick67):

#include <iostream> using namespace std; void pattern(unsigned int n) { if( n<= 8484) { cout<<n<<endl; pattern(n*2); cout<<n<<endl; } return; } int main() { int n; cin >> n; pattern(n); return 0; }

OpenStudy (anonymous):

C Implementation #include <stdio.h> #include <stdlib.h> // Function Prototyping void pattern(unsigned int); int main() { // Taking Input From User To Give The Number unsigned int n; printf("Enter a number greater than zero: "); scanf("%d", &n); pattern(n); //numList is the Funtion to print the list return 0; } // Function Begins void pattern(unsigned int n) { if(n<=8484) { printf("%d\n",n); pattern(n*2); printf("%d\n",n); } } C# Implementation using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace PatternCSharp { class Program { static void Main(string[] args) { Console.Write("Enter a number greater than zero: "); string numString=Console.ReadLine(); int num = Convert.ToInt32(numString); pattern(num); Console.ReadLine(); } private static void pattern(int num) { if (num <= 8484) { Console.WriteLine(num); pattern(num * 2); Console.WriteLine(num); } } } } However, there's something you need to know. Don't post a problem without trying. It's important for you to learn, submitting your assignment only makes sure that you did learn the concept. I don't know if you have tried this or even if you know what recursion is, but my point is that please read the code and try to learn the technique. If you don't understand any part of it, ask out. That would really help in the long run !

OpenStudy (anonymous):

Thanks. Actually, I know it seems like it's an assignment but I'm self- assigning myself study questions from a website online to help me prepare for an exam. I'm not submitting this in per se.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!