Question

C code. Write a program to find all the triangles with integer side lengths and a...

C code.

Write a program to find all the triangles with integer side lengths and a user

specified perimeter. Perimeter is the sum of all the sides of the triangle. Integers a,

b and c form a triangle if the sum of the lengths of any two sides is greater than the

third side. Your program should find all the triples a, b and c where a + b + c is user

specified perimeter value. Print each triple only once in increasing order. For example,

3, 4 and 5 forms a triple with perimeter 12. Only print 3 4 5 instead of printing all

the permutations of the triple.

Sample output for this recitation is as follows

Enter perimeter

24

Triangles with perimeter 24

2 11 11

3 10 11

4 9 11

4 10 10

5 8 11

5 9 10

6 7 11

6 8 10

6 9 9

7 7 10

7 8 9

8 8 8

Count = 12

Test your program with different values for perimeter. Number of such triangles is know

as Alcuin’s sequence and list of values is available at http://oeis.org/A005044/list

You need to use nested loops for this problem. Name your program recitation2.c

Write a program to find all the triangles with integer side lengths and a user

specified perimeter. Perimeter is the sum of all the sides of the triangle. Integers a,

b and c form a triangle if the sum of the lengths of any two sides is greater than the

third side. Your program should find all the triples a, b and c where a + b + c is user

specified perimeter value. Print each triple only once in increasing order. For example,

3, 4 and 5 forms a triple with perimeter 12. Only print 3 4 5 instead of printing all

the permutations of the triple.

Sample output for this recitation is as follows

Enter perimeter

24

Triangles with perimeter 24

2 11 11

3 10 11

4 9 11

4 10 10

5 8 11

5 9 10

6 7 11

6 8 10

6 9 9

7 7 10

7 8 9

8 8 8

Count = 12

Test your program with different values for perimeter. Number of such triangles is know

as Alcuin’s sequence and list of values is available at http://oeis.org/A005044/list

You need to use nested loops for this problem. Name your program recitation2.c

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

//If you have any doubt please comment me

Code:-

              #include <stdio.h>
int main()
{
    int n;
    printf("Enter perimeter\n");
    scanf("%d",&n);
    printf("Triangles with perimeter %d\n",n);
    int count = 0;   //it is used for the counting total number of ways
    for(int a = 2 ; a < n; ++a)
    {
        for(int b = a; b < n; ++b)
        {
            for(int c = a; c < n; ++c)
            {
                if(a+b+c == n)
                {
                   if((a<=b) && (b<=c) && (a+b >c))
                    {
                       ++count;
                         printf("%d %d %d\n",a,b,c);
                    }
                }
            }
        }
    }
    printf("Count = %d",count);
    return 0;
}

Output1:-

Output2:-

Add a comment
Know the answer?
Add Answer to:
C code. Write a program to find all the triangles with integer side lengths and a...
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
  • Write the java program: A right triangle can have sides whose lengths are all integers. The...

    Write the java program: A right triangle can have sides whose lengths are all integers. The set of three integer values for the length of the sides of a triangle is called a Pythagorean triple. The length of the three sides must satisfy the relationship that the sum of the squares of the sides is equal to the square of the hypotenuse. Write a Java application that prompts the user for an integer that represents the largest side value and...

  • 2. Write a program that prompts a user to enter the lengths of sides and angles...

    2. Write a program that prompts a user to enter the lengths of sides and angles for the two triangles described below. The program should calculate the length of the side of the triangle indicated below. Print the two answers to 3 decimal places onto the console screen Triangle 1 Read in the value of the angle, alpha, and the length, a, of the side indicated in the picture below. Calculate the length of the hypotenuse, c, of this right...

  • Write a C++ program to analyze a variety of triangles. The program should determine all angles...

    Write a C++ program to analyze a variety of triangles. The program should determine all angles and all sides for a triangle for three different options (give the user a menu of choices): 1) Given two sides and the angle between 2) Given two angles and one side 3) Given three sides More details for each option is provided below. Option 1: Given two sides and the angle between First check to be sure that the three values entered are...

  • PLEASE READ AND CODE IN BASIC C++ CODE. ALSO, PLEASE CODE USING THIS DO WHILE LOOP...

    PLEASE READ AND CODE IN BASIC C++ CODE. ALSO, PLEASE CODE USING THIS DO WHILE LOOP AND FORMAT: #include <iostream> using namespace std; int main() { //variables here    do { // program here             }while (true);    } Objectives To learn to code, compile and run a program containing SELECTION structures Assignment Write an interactive C++.program to have a user input the length of three sides of a triangle. Allow the user to enter the sides...

  • to be done in c language. follow prompt Second Program: triples.c 8. Create a program named...

    to be done in c language. follow prompt Second Program: triples.c 8. Create a program named triples.c "C How to Program", 8th edition, problem 4.27 on page 154. In other editions, it may be on a different page Here's the problem... A right triangle can have sides that are all integers. The set of 3 integers for the sides of a right triangle is called a Pythagorean triple Write a C program to find all of the triples with hypotenuse...

  • Problem a (PA4a.java) Write a program to evaluate the area of a triangle given the lengths...

    Problem a (PA4a.java) Write a program to evaluate the area of a triangle given the lengths of its sides using Heron's Formula. Here is an outline: Get the three side lengths from the user (which might have decimal values): a, b, c. Check to ensure that the sides are valid for a triangle. Importantly, the sum of the lengths of any two sides must be larger than the length of the third side (you must check all three sides this...

  • help im alittle lost my teacher posted all this its suppose to be in C++ language....

    help im alittle lost my teacher posted all this its suppose to be in C++ language. can yoh leave comments ans type the code thank you Write a C++ program that accepts the lengths of three sides (a,b,c) of a triangle as input from the user Validate the user input, so that sides a, b, c can only be POSITIVE. The program output should indicate whether or not the triangle is an Equilateral Triangle, a Right Triangle, Isosceles which is...

  • 15. The perimeter of a triangle is 23 cm. All the side lengths are integers. If...

    15. The perimeter of a triangle is 23 cm. All the side lengths are integers. If one side measures 5 cm, what is the length of the largest possible side? (A) 9 cm (B) 11 cm (C) 12 cm (D) 15 cm

  • MIPS - Takes two inputs from the user, which are the lengths of two sides of...

    MIPS - Takes two inputs from the user, which are the lengths of two sides of a polygon. It adds those two lengths and prints the sum. Your task is modify the program to make it specific to a triangle, and to print the perimeter of that triangle. See blue highlighted. preamble: prompt1: prompt2: answer: endline: .data ascii .asciiz asciiz .asciiz asciiz .asciiz "\nThis program, written by <YOUR NAME>," " can be used to add the length of two sides...

  • Write a C++ program using user defined functions that will read in the lengths of two...

    Write a C++ program using user defined functions that will read in the lengths of two side of a right triangle and then calculate the length of the hypotenuse. The program should do this twice. The program should the following user defined functions: Function readA will read in a value for side A from the user and return it to main. (it will not receive any input data from main) Function readB will read in a value for side B...

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
ADVERTISEMENT