Question

3. I. Calculate 10 factorial with for-loop. Compare the result you obtained with factorial(10). II. Calculate...

3. I. Calculate 10 factorial with for-loop. Compare the result you obtained with factorial(10). II. Calculate the greatest common divisor of the numbers 258, 387, 516 with the help of control structures (for, while, if, etc.)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C Code : For factorial

#include<stdio.h>

#include<conio.h>

// declare a factorial function

int factorial(int num){

    // initializing a variable

    int i,fact=1;

    // use for loop find fact

    for(i=1;i<=num;i++){

         fact=fact*i;    

     }

    // return the value of fact

    return fact;

}

//main

int main(){

  // initializing a variable

  int number;    

  // ask for the input

  printf("Enter a number: ");    

  scanf("%d",&number);    

  // call the function and print the result

  printf("Factorial of %d is: %d",number,factorial(number));

  return 0;

}

OUTPUT:

C Code : For greatest common divisor

#include<stdio.h>

#include<conio.h>

// declare a factorial function

int greatestNumber(int x, int y, int z){

    // if x is greater than y and z then return x

    if(x>=y && x>=z){

        return x;

    }

    // if y is greater than x and z then return y

    else if(y>=x && y>=z){

        return y;

    }

    // if z is greater than x and z then return z

    else if(z>=x && z>=y){

        return z;

    }

}

//main

int main(){

    // initializing a variable

    int a,b,c;

    // ask for the input variable

    printf("Enter first numbers: ");

    scanf("%d",&a);

    printf("Enter second numbers: ");

    scanf("%d",&b);

    printf("Enter third numbers: ");

    scanf("%d",&c);

    int i;

    // call the function

    for(i=greatestNumber(a,b,c); i>=1; i--){

        // check the conditions

        if(a%i==0 && b%i==0 && c%i==0){

            break;// break the loop

        }

    }

   // print the result

    printf("GCD is : %d\n",i);


  return 0;

}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
3. I. Calculate 10 factorial with for-loop. Compare the result you obtained with factorial(10). II. Calculate...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. (10 points) We want to compare the numbers 3 and -6. Using 4-bit signed 2's complement numbers, show how we can use the process of binary addition to calculate a result that will tell us how t...

    1. (10 points) We want to compare the numbers 3 and -6. Using 4-bit signed 2's complement numbers, show how we can use the process of binary addition to calculate a result that will tell us how these two numbers compare. (Just show the calculation here. The next part will be the interpretation of the result.) Now briefly explain how this result can be interpreted by a hardware circuit to indicate how the two numbers compare. (You don't need to...

  • LANGUAGE C++ I need help with all 3 questions. Thank you in advance Password Generator← 10...

    LANGUAGE C++ I need help with all 3 questions. Thank you in advance Password Generator← 10 10.201.51. eRAD D scheduling山UitPro 6 Fizz Buzz Write a function "void FizzBuzz(int n)" that lists all of the numbers from 1 to n, but replace all numbers divisible by 3 but not divizible by 5 with the word "Fizz", replace all numbers divisible by 5 but not divisible by 3 with the word "Buzz", and replace all numbers divisible by both 3 and 5...

  • Can someone help me with this Python code Summary In this lab, you work with the...

    Can someone help me with this Python code Summary In this lab, you work with the same Python program you worked with in Labs 5-1 and 5-3. As in those earlier labs, the completed program should print the numbers 0 through 10, along with their values multiplied by 2 and by 10. However, in this lab you should accomplish this using a while loop with a break statement. Instructions Make sure that the file NewestMultiply.py is selected and open. Write...

  • LC3 stack (factorial) I need help in writing factorial in Lc3 language by using stack.. ; Begin ...

    LC3 stack (factorial) I need help in writing factorial in Lc3 language by using stack.. ; Begin reserved section: do not change ANYTHING in reserved section! .ORIG x3000 BR Main ; Parameter and result Param .FILL x0004 Result .BLKW 1 ; Constants Stack .FILL x4000 One .FILL #1 MinusOne .FILL #-1 ; End reserved section: do not change ANYTHING in reserved section! ;------------------------------------------------------------------------------- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; int Factorial(int N) ; Returns N! (must be a recursive function) ; Factorial ;__________________...

  • C programming Question (Please help if you can) - Create a "loop" that will execute n...

    C programming Question (Please help if you can) - Create a "loop" that will execute n times, displaying numbers starting at b and incrementing by i. The numbers that would result should be inserted into an array for example n = 5 b = 6 i = 4 resulting array= {6 10 14 18 22} -------------------------------- n = 6 b = 10 i = 3 resulting array = {10 13 16 19 22 25}

  • I need help finding the several errors and several loop errors for I get the final...

    I need help finding the several errors and several loop errors for I get the final output. C++ cLion #include <iostream> using namespace std; int main() { cout << "Welcome to Loop World" << endl; cout << endl; cout << "******************" << endl; cout << "Section I" << endl; cout << "******************" << endl; int sum; // Accumulates the total int i; // Used as loop control variable int numIter; // The number of times to iterate cout << "Enter...

  • For this assignment, you will apply what you learned in analyzing for, while, and do-while loops...

    For this assignment, you will apply what you learned in analyzing for, while, and do-while loops by writing these statements yourself. The Java™ program you write should do the following: Display a pyramid of asterisks onscreen (i.e., a nested for loop) Display the integers 10 to 1 in decreasing order, one number per line (i.e., a while/do-whlie loop) Add 7 until the sum becomes greater than 157, at which point the program should display both the sum and the number...

  • C++ PROGRAM ONLY! For this lab you need to write a program that will read in...

    C++ PROGRAM ONLY! For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using a recursive implementation of the Euclidean algorithm) to a file. In Lab #3, you implemented this program using an iterative method. Greatest Common Divisor In mathematics, the greatest common divisor (GCD) of two or more integers (when at least one of of them is zero then the larger value is the GCD....

  • Loop Invariants. I need help on the 15 point question. The 10 point question is the...

    Loop Invariants. I need help on the 15 point question. The 10 point question is the "procedure" from the previous question. 2: (10 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is an array A. You must assume that indexing begins at 1. 1: for j = 2: A.length do key = A[j] i=j-1 while i > 0) and A[i] > key do A[i+1] = A[i] i=i-1 A[i+1] = key 3: 4: 5: 6:...

  • 1. DEF Company is comparing three different capital structures. Plan I would result in 800 shares...

    1. DEF Company is comparing three different capital structures. Plan I would result in 800 shares of stock and $9,000 in debt. Plan II would result in 700 shares of stock and $13,500 in debt. Plan III is an all-equity plan and would result in 1,000 shares of stock. The firm’s EBIT will be $8,000 per year until infinity. The interest rate on the debt is 10%.                                                                                            (12 marks total) a. Ignoring taxes, compute the EPS for each...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT