c language ask a triangle formula - c

Write a program that outputs the length of the sides of the possible triangle and the number of triangles, when you want to create a triangle by inputting the sum of the lengths of the three sides of the triangle.(the three sides are integer variables.)
(the sum of the lengths of the two sides of the triangles is greater than the length of the other side)
For example:
Sum of three sides: 6
1 3 2\n
2 2 2\n
2 3 1\n
3 1 2\n
3 2 1\n
number of triangle: 5
my code:
#include <stdio.h>
int main() {
int a = 0, b = 0, c = 0, count=0;
int sum = a + b + c;
printf("Sum of three sides: ");
scanf_s("%d", &sum);
for (a = 1; a < sum; a++)
for (b = 1; b < sum; b++)
for (c = 1; c < sum; c++)
if (a + b > c && a+b+c==sum)
printf("%d\t %d\t %d\n", a, b, c);
printf("number of triangle: %d", count );
}
I don't know how to output the number of triangles and how to put
"the sum of the lengths of the two sides of the triangles is greater than the length of the other side" into code.
4 1 1 is not a valid triangle. a + b > c is the triangle formula.

As it has been pointed out in comments/answers the main problem is that your code only checks for a + b > c where it should for all combination of "sum of two sides being larger than the last side".
Besides that there is no reason for using 3 loops. When a and b is selected by the outer two loops, c can be calculated like c = sum - a - b. Something like:
#include <stdio.h>
int main() {
int a, b, c, count=0;
int sum;
printf("Sum of three sides: ");
scanf_s("%d", &sum);
for (a = 1; a < sum; a++)
{
for (b = 1; b < sum; b++)
{
c = sum - a - b;
if (c < 1) break; // No reason to continue so break out of inner loop
if ((a + b > c) && (a + c > b) && (b + c > a))
{
printf("%d\t %d\t %d\n", a, b, c);
++count;
}
}
}
printf("number of triangle: %d", count );
return 0;
}
You can improve the program performance further by considering the condition for the loops:
for (a = 1; a < sum; a++)
Is it really necessary to continue all the way to sum - 1 ?
Can there ever be a triangle where a is greater or equal to sum/2 ?

Your code does not increment the count variable at any place. It just lies there unchanged throughout. Also, you are checking for the validity of just one side and not for the satisfaction of conditions for all sides.
And also, your sample output is wrong, [1 3 2], [2 3 1], [3 1 2], [3 2 1], etc are not valid triangles. Only [2,2,2] satisfies the triangle condition. Please cross check the expected output before posting it as a requirement here.
here is a possible working code:
#include<stdio.h>
int main() {
int a = 1, b = 1, c = 1, count=0;
int sum = 0;
printf("Sum of three sides: ");
scanf("%d", &sum);
while(a < sum) {
b=1;
while(b < sum) {
c=1;
while(c < sum) {
if ((a+b+c == sum)&&(a+b>c)&&(a+c>b)&&(c+b>a))
{
count+=1;
printf("Triangle %d:\nSide 1: %d Side 2: %d Side 3: %d\n\n", count, a, b, c);
}
c++;
}
b++;
}
a++;
}
printf("Possible number of triangles: %d", count );
return(0);
}

You have to check the condition for all three sides and you have to increment count for a successful test.
#include<stdio.h>
#include<stdlib.h>
int main() {
int a = 0, b = 0, c = 0, count=0;
int sum = a + b + c;
printf("Sum of three sides: ");
scanf("%d", &sum);
for (a = 1; a < sum; a++)
for (b = 1; b < sum; b++)
for (c = 1; c < sum; c++)
if ((a+b+c==sum) && ((a + b > c) && (a + c > b) && (c + b > a)))
{
printf("%d\t %d\t %d\n", a, b, c);
count++;
}
printf("number of triangles: %d", count );
}
Output:
Sum of three sides: 6
2 2 2
number of triangle(s): 1
Sum of three sides: 7
1 3 3
2 2 3
2 3 2
3 1 3
3 2 2
3 3 1
number of triangle(s): 6

The condition ((a+b+c==sum) && ((a + b > c) && (a + c > b) && (c + b > a))) may be improoved by adding && a<=b && b<=c to avoid the generation of dublicate triangles. for example, 3 3 1 is the same as 3 1 3 and 1 3 3.
(P.s I put this as an answer because I do not have enough reputation to post a comment. )

Related

Must print a numerical sequence 15, 12, 24, 21, 42, 39, 78, 75, 150, 147

As I see it the numerical sequence consists of 2 separate sequences. This is the code that I have so far. I am not sure if you must use a while or a for loop. I am fairly new at coding so if someone please could help me.
if the entered value is 10 it must give the first 10 terms of the sequence, and if I enter 5 it must give me the first 5 terms of the sequence.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int a, n = 1, t, y = 1; // First Numerical Sequence
int b, m = 2, s, x = 2; // Second Numerical Sequence
int d, r, z; // Extra
printf("Enter A Tn : ");
scanf(" %d", &z);
printf("\n");
while (n <= z) {
a = 15;
r = pow(2, n - y);
d = (9 * (r - 1)) / (2 - 1);
t = a + d;
printf("%d\n", t);
n += 2;
y++;
}
while (m <= z) {
b = 12;
r = pow(2, m - x);
d = (9 * (r - 1)) / (2 - 1);
s = b + d;
printf("%d\n", s);
m += 2;
x++;
}
printf("\n");
return 0;
}
This will get the job done.
#include <stdio.h>
int main(){
int val,ic; //iteration count, will print ic*2 number
scanf("%d %d",&val,&ic);
for(int i = 0;i<ic;i++){
printf("%d ",val);
val-=3;
printf("%d ",val);
val*=2;
}
printf("\n");
}
How to compile & run:
C:\Users\stike\Desktop>rem assume you saved it in a.c
C:\Users\stike\Desktop>gcc -o a a.c
C:\Users\stike\Desktop>a
15
5
15 12 24 21 42 39 78 75 150 147
If you want to print the same sequence starting from 15 and o till a certain number which the user inputs, you can follow the following code.
Hope you understood the sequence pattern when a number is given it is printed and reduce the number by 3, then it is printed and then twice the number and printed, and again reduce by 3, likewise, it flows on.
#include <stdio.h>
int main() {
int endNum;
int beginNum = 15;
printf("Enter the end: ");//(lineA) here we initialize the variables with beginNum as 15
scanf("%d", &endNum); //(Line B) let the user to input endNum of the sequence,in the example it is 147
while ((beginNum-3) <= endNum) { // checks the condition
printf("%d ", beginNum);
if(beginNum==endNum) return 0; //check whether you print the end number.
beginNum -= 3; // reduce by 3
printf("%d ", beginNum);
beginNum *= 2; // multiply by 2
}
return 0;
}
if you don't need to user input a endNum just initialize the value 147 to variable endNum.
And delete the lines A and B.
Here's another approach using static variables
#include <stdio.h>
int next(void) {
static int last, n = 0;
if (n++ == 0) return last = 15; // 1st element of sequence
if (n % 2) return last = last * 2; // odd elements
return last = last - 3; // even elements
}
int main(void) {
for (int k = 0; k < 10; k++) {
printf("%d ", next());
}
puts("");
return 0;
}

Fail in recursion with odd numbers

I have just write this funtion to print a list with the numbers between a and b.
void list(int a, int b){
if(a == b){
printf("%d", a);
}else{
if(a < b){
printf("%d ", a);
printf("%d ", b);
list(a + 1, b - 1);
}
if(b < a){
printf("%d ", a);
printf("%d ", b);
list(a - 1, b + 1);
}
}
}
When I call from the main the funtion, it works just when the number of numbers is odd. For example:
int main(){
list(2, 8);
return 0;
}
It works correctly and it prints: 2 8 3 7 4 6 5. But in this case:
int main(){
list(2, 7);
return 0;
}
It prints 2 7 3 6 4 5 5 4 4 5 5 4 4 5 5 4 4 5 5 4 4... forever.
What is wrong in the function??
It's because it never ends. When the difference between a and b is odd, they never reach the == condition and it continues forever. To solve this, you can check if a and b are going to pass each other, kike this:
void list(int a, int b)
{
if(a == b)
printf("%d", a);
else if(a < b)
{
printf("%d ", a);
printf("%d ", b);
if (a + 1 != b)
list(a + 1, b - 1);
}
else if(b < a)
{
printf("%d ", a);
printf("%d ", b);
if (b + 1 != a)
list(a - 1, b + 1);
}
}
As everytime you are making an iteration, you are substracting/adding 1 to both a and b. This means that when it is an odd number and a pair number a and b Will never be the same. I recommend you that when you start learning basic algorithms (specially for recursivion) you try some iterations on paper.
EDIT: A recommendation would be to simply add an exit clause (another else if), where you exit recursiĆ³n if a becomes bigger than b (or viceversa, depending on which number was higher, you could save it in a boolean).
(2,7)
2 7 3 6 4 5
When a = 4, b = 5, the next is a = 5, b = 4, so you got the loop.
try this:
void list(int a, int b){
if ( ( b-a ) % 2 == 0 ){ // even
if(a == b){
printf("%d", a);
}else{
if(a < b){
printf("%d ", a);
printf("%d ", b);
list(a + 1, b - 1);
}// if
else if(b < a){
printf("%d ", a);
printf("%d ", b);
list(a - 1, b + 1);
}
}
}
else { // odd
if(a < b){
printf("%d ", a);
printf("%d ", b);
list(a + 1, b - 1);
} // if
}
}
In each turn when a is not equal to b, you are incrementing one(of a or b) and reducing the other. so the difference between the numbers now decrease by 2 (or increase by 2). Simple observation says that for termination of the re-calling the function, the numbers (a,b) have to be equal. so, for the difference to be 0 (a=b, which will not call the function again), the difference between a and b in the starting should be a multiple of 2. so only both even or both odd are acceptable. In other situations, the function will be called again and again and a particular sequence will repeat(in your case 4 5 5 4)

Check whether X is divisible by Y, i.e. no remainder

I'm trying to write a program that takes numbers from user (a and b) then
the program will count up from 1 to x (which is a) and output whether the number in each iteration is divisible by y (which is b) or not.
//while loop deciding if a is divided by b
for (count = 1; count <= a; count++) {
if (a / b == 0) {
printf("%d is divisible by %d\n", a, b);
}
else if (a / b != 0) {
printf("%d is not divisible by %d\n", a, b);
}
}
return 0 ;
}
but when i enter 10 for a and 2 for b the output is
10 is not divisible by 2
ten times
how can i change the code so that each iteration is checked?
First - a / b tells how many times a can be divided by b, e.g. 9 / 2 will give you 4.
To know whether the division produces a reminder, you must use a % b == 0. Example: 9 % 2 will give you 1 while 8 % 2 will give you 0.
Next - You keep using a and b for the calculation inside the loop. You need to use count instead of a. This applies to the printfas well.
And - You don't need a condition on the else part.
Try something like:
for (count = 1; count <= a; count++) {
if (count % b == 0) {
printf("%d is divisible by %d\n", count, b);
}
else {
printf("%d is not divisible by %d\n", count, b);
}
}
#include <stdio.h>
int main()
{
int a;
printf("Enter a 2-digit number: ");
scanf("%d",&a);
for (int i = 0; i < a; i++)
{
if (i%2 == 0)
{
printf("\n%d is even.", i);
}
else if (i%2 != 0)
{
printf("\n%d is odd.",i);
}
}
return 0;
}
This is the code for checking whether the number is even or odd.
Your program has two logical errors -
You are running a for loop to go from 1 to a, but are not using the loop variable count anywhere inside the loop.
a/b == 0 cannot be used to check if b divides a, instead use a%b == 0. % is modulus operator which returns the remainder when a is divided by b.
Correct Code -
for (count = 1; count <= a; count++) {
if (count%b == 0) {
printf("%d is divisible by %d\n", count, b);
}
else {
printf("%d is not divisible by %d\n", count, b);
}
}
every iteration you are using same values, then you can use count variable and at last print value of variable.
you can write like this,
for (count = 1; count <= a; count++)
{
if (count % b == 0)
{
printf("%d is divisible by %d\n", a, b);
n += 1;
}
}
printf("count : %d\n",n);
it will show the count.

3-digit integer number program won't execute

Yes, this is a basic C coding homework problem. No, I am not just looking for someone to do it for me. Considering that this is my first programming class, I'm not surprised that I can't get it to work, and I'm certain there is plenty wrong with it. I just want some help pointing out the problems in my code and the things that are missing so that I can fix them on my own.
Homework Question:
Write a program to read ONLY one integer number (your input must be
one 3 digit number from 100 to 999), and to think of a number as
being ABC (where A, B, and C are the 3 digits of a number). Now,
form the number to become ABC, BCA, and CAB, then find out the
remainder of these three numbers when they are divided by 11.
Assume remainders would respectively be X, Y, and Z and add them
up as X+Y, Y+Z, and Z+X. Now if any of these summations is odd
number, increase it by 11 if the summation plus 11 is less than 20,
otherwise decrease the summation by 11 (this summation operation
must be positive number but less than 20). Finally, divide each
of the sums in half. Now, print out all the resulting digits.
My Code:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
//Declare all variables
int OrigNumber;
int x, y, z;
int number;
number = x, y, z;
int sum;
//
printf("Input a three digit number");
//
int c;
c = OrigNumber %10;
//
int b;
b=((OrigNumber - c) % 100)/10;
//
int a;
a = (OrigNumber - (b + c))/100;
//
int abc, bca, cab;
abc = (a*100) + (10*b) + c;
bca = (10*b) + c + (a*100);
cab = c + (a*100) + (10*b);
//
if((number % 2) == 1)
{
if(number + 11 < 20)
number += 11;
else if((100 - 11 > 0) && (100 - 11 < 20))
number -= 11;
}
//
x = abc/11;
y = bca/11;
z = cab/11;
//
sum = (x + y),
(y + z),
(z + x);
}
To start with, you need to read the input. Start with a prompt that includes a carriage return:
printf("Input a three digit number: \n");
Since it's a three digit number, you could add the following line to read the input:
scanf("%3d", &OrigNumber);
The next bit of code works quite well until you get to your if (number % 2) which is meaningless since you didn't really define number - well, you did, but the line
number = x, y, z;
does NOT do what you think it does. If you add
printf("So far I have abc=%d, bca=%d, cab=%d\n", abc, bca, cab);
after you first read in the number and computed those three, you will see you are well on your way.
Note that
number = x, y, z;
Uses a thing called the "comma operator". All the things (a,b,c) are "evaluated" but their values are not returned. At any rate, where you have that line, you didn't yet assign a value to x,y and z.
Is that enough to get your started?
update now that you have had a few hours to mull this over, here are a few more pointers.
Your computation of abc, cab, bca makes no sense. I will show you just one of them:
cab = c*100 + a*10 + b;
Next you need to compute each of x, y and z. Again, here is one of the three:
y = bca%11;
Now you have to make the sums - I call them xy, yz, and zx. Just one of them:
zx = z + x;
Next, to deal with the instruction: "Now if any of these summations is odd number, increase it by 11 if the summation plus 11 is less than 20, otherwise decrease the summation by 11:
if(xy % 2 == 1) {
if(xy + 11 < 20) xy += 11; else xy -= 11;
}
use similar code for all three sums. Then "divide by 2":
xy /= 2;
repeat as needed.
Finally, print out the result:
printf("xy: %d, yz: %d, zx: %d\n", xy, yz, zx);
The amazing thing is that if you did this right, you get the original numbers back...
You could make the code more compact by using an array of values and looping through it - rather than repeating the code snippets I wrote above with different variables. But I suspect that is well outside the scope of what you are expected to know at this point.
Can you take it from here?
#include <stdio.h>
int main()
{
//Declare all variables
int OrigNumber;
int a, b, c;
int abc, bca, cab;
int x, y, z;
int xplusy , yplusz, xplusz;
printf(" A program to read ONLY one integer number.\n Input must be one 3 digit number from 100 to 999 : ");
scanf("%d", &OrigNumber); // Get input from console
if(OrigNumber > 999 || OrigNumber < 100) {
printf("Invalid number. Quiting program. This is error handling. Important while learning programming.");
return 0;
}
c = OrigNumber %10; // digit at unit's place
b=((OrigNumber) % 100)/10; //digit at the ten's place
a = (OrigNumber)/100; //digit at the 100's place. Note: 734/100 = 7. NOT 7.34.
printf("\n Three numbers say A,B, C : %d, %d , %d ", a, b, c);
abc = a*100 + 10*b + c;
bca = 100*b + 10*c + a;
cab = c*100 + a*10 + b;
printf("\n Three numbers say ABC, BCA, CAB : %d, %d , %d ", abc, bca, cab);
x = abc % 11; // Reminder when divided by 11.
y = bca % 11;
z = cab % 11;
printf("\n Three numbers say X, Y, Z : %d, %d , %d ", x, y, z);
xplusy = x + y; // Adding reminders two at a time.
yplusz = y + z;
xplusz = x + z;
printf("\n Three numbers X+Y, Y+Z, X+Z : %d, %d , %d ", xplusy, yplusz, xplusz);
if((xplusy % 2) == 1) {
if(xplusy + 11 < 20)
xplusy += 11;
else
xplusy -= 11;
}
if((yplusz % 2) == 1) {
if(yplusz + 11 < 20)
yplusz += 11;
else
yplusz -= 11;
}
if((xplusz % 2) == 1) {
if(xplusz + 11 < 20)
xplusz += 11;
else
xplusz -= 11;
}
xplusy /= 2; // Finally, divide each of the sum in half.
yplusz /= 2;
xplusz /= 2;
printf("\n Now print out all the resulting digits : %d, %d , %d \n", xplusy, yplusz, xplusz);
return 0;
}
int abc, bca, cab;
abc = (a*100) + (10*b) + c;
bca = (10*b) + c + (a*100);
cab = c + (a*100) + (10*b);
I suggest printing out the numbers at this point in the code.
printf( "%d %d %d", abc, bca, cab );
I think you'll see one of the problems you need to solve.
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int n, a, b, c, abc, bca, cab, x, y, z, p, q, r;
scanf("%d", &n);
c=n%10;
b=(n/10)%10;
a=n/100;
abc=a*100+b*10+c;
bca=b*100+c*10+a;
cab=c*100+a*10+b;
x=abc%11;
y=bca%11;
z=cab%11;
p=x+y;
q=y+z;
r=z+x;
return 0;
}
Now if any of these summations is odd number, increase it by 11 if the
summation plus 11 is less than 20, otherwise decrease the summation by
11 (this summation operation must be positive number but less than
20). Finally, divide each of the sums in half. Now, print out all the
resulting digits.
i didnt get the final part, can you explain it more clearly?

Finding which Pythagorean triple has the smallest angle

Information: a being the length of the shortest leg of a right triangle and b being the length of the other leg, the larger the difference between a and b the smaller the angle. That is:
the triple (3, 4, 5) has a difference of 4-3=1
the triple (5, 12, 13) has a difference of 12-5=7
Therefore the smallest angle would be in the triple (5, 12, 13)
I'm writing a program that compares all of the pythagorean triples defined in a range and prints the triple with the smallest angle. What I have so far is not working and I have no idea where I can go from here.
#include <stdio.h>
int smallest(int a, int b) {
int difference = b - a;
return 0;
}
int main() {
int a = 0, b = 0, c = 0, n, counter = 1, i = 0;
printf("Please Enter a Positive Integer: \n");
scanf("%d", &n);
for (c = 0; c < n; c++) {
for (b = 0; b < c; b++) {
for (a = 0; a < b; a++) {
if (a * a + b * b == c * c ) {
printf("%d:\t%d %d %d\n", counter++, a, b, c);
}
}
}
i = counter - 1;
}
printf ("The difference is %d\n", smallest(a, b));
printf ("There are %d Pythagorean Triples in this range.\n", i);
return 0;
}
The program just prints the difference is 0
what I am looking for the program to print is, for the example above "The triangle with the smallest angle is (5, 12, 13)"
I know I have to sort the differences and compare them but this is all I have so far, any tips?
Are you sure your 'information' is right? Because triple (30, 40, 50) has a difference of 10 and just the same angle as (3, 4, 5).
You don't have to sort, you have to remember difference/angle/whatever you minimize, as already mentioned in comments. Also, you have to remember values of 'minimal' (a,b,c) to print them later. Something like this:
//...
if (a * a + b * b == c * c ) {
printf("%d:\t%d %d %d\n", counter++, a, b, c); //if you use ++counter instead, you won't need i ;)
if(minDifference < b-a){ //Don't be afraid of variables with long names
minA = a; minB = b; minC = c; minDifference = b-a;
}
}
//...
printf("The triangle with the smallest difference is (%d, %d, %d)", minA, minB, minC);
Your smallest-code returns 0 in every case. Are you sure you don't want to return difference instead (or even b-a)?
int smallest(int a, int b) {
return b-a;
}

Resources