Given the following code, what is the final value of X written to the conole? sh
ID: 3672857 • Letter: G
Question
Given the following code, what is the final value of X written to the conole? show how you arrived at the result (intermediate values of x within the program) as well.
int N=0, M=0, I=0, X;
X = Convert.ToInt32( 100.9+ (31.6 / Math.Pow( 2, 3)) * Math.Ceiling(-3.2));
if ((x < 36)) || (x> M + N) && (x ==12))
{
do
{
X += I;
I += X;
} while (I <= X);
}
else
{
if ((X==45) || ((X >=20) && (X<=80)))
X +=1;
else if ((X >=20) && (X<=80)))
X +=1;
else if ((X >=30) && (X<=90))
Convert.ToInt32(X /=2);
else
x=0;
}
Console.WriteLine(X)
Console.ReadLine();
Explanation / Answer
// The c sharp program with final X value with evaluation of expressions
// and explanations for X value is calculated
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FinalValue
{
class Program
{
static void Main(string[] args)
{
int N=0, M=0, I=0, X;
//Math.Ceiling(-3.2) gives ceiling value of -3.2 is -3
//Math.Pow( 2, 3) is 2 power of 3 is 8
//31.6 / Math.Pow( 2, 3) =31.6/8=3.95
//3.95*(-3)=-11.85
//100.9+(-11.85)=89.05
//since X is integer value , decimal 0.5 is discarded, X=89
X = Convert.ToInt32( 100.9+ (31.6/Math.Pow( 2, 3)) * Math.Ceiling(-3.2));
//X=89 <36 is false
//X> M + N
//=> 89 >(0+0) is true
//X==12 is false
//Evaluation of expression :
//(X < 36) || (X> M + N) && (X ==12)
//false ||(true && false)
//false || false
//false
//This condition becomes false
if ((X < 36) || (X> M + N) && (X ==12))
{
do
{
X += I;
I += X;
} while (I <= X);
}
else
{
//Here X=89
//Evaluation of expression :
// || or opeation
// && and opeation
//(X==45) || ((X >=20) && (X<=80))
//false || true && false
//false || false
//false
//This condition becomes false
if ((X==45) || ((X >=20) && (X<=80)))
X +=1;
//Here X=89
//Evaluation of expression :
//(X >=20) && (X<=80)
//true || false ( || or opeation)
//true ||false ( || or opeation)
//false
//This condition becomes false
else if ((X >=20) && (X<=80))
X +=1;
//Here X=89
//Evaluation of expression :
//(X >=30) && (X<=90)
//true && true
//true && true
//true
//This condition becomes true
else if ((X >=30) && (X<=90))
//This statement is exeucted
//X /=2 => X=X/2
//X=89/2=44.5
//Since the 44.5 is converted to decimal value , X gets the only 44
//0.5 decimla is discarded
Convert.ToInt32(X /=2);
else
X=0;
}
//print X = 44 to console output
Console.WriteLine(X);
Console.ReadLine();
}
}
}
Sample Output:
44
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.