Jump to content

homework help

slitherin96

hey guys I kinda missed a lecture and wasn't able to quite understand it, but I have to make a program in c# that checks if the third digit in a number (right to left) is 5.
I wanted to go a bit above the original assignment and added a ReadLine to enter the number. my real problem is that I apparently can't do && to int/byte values.
here's what I have so far, please tell me where im going wrong
 

using System;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("please enter a number");
            int num = Convert.ToInt32(Console.ReadLine());
            num = (byte)(num >> 2);
            byte check = 5;
            bool result = (byte)(check && num);
            Console.WriteLine(result);
        }
    }
}
 

Link to comment
Share on other sites

Link to post
Share on other sites

HAL9000: AMD Ryzen 9 3900x | Noctua NH-D15 chromax.black | 32 GB Corsair Vengeance LPX DDR4 3200 MHz | Asus X570 Prime Pro | ASUS TUF 3080 Ti | 1 TB Samsung 970 Evo Plus + 1 TB Crucial MX500 + 6 TB WD RED | Corsair HX1000 | be quiet Pure Base 500DX | LG 34UM95 34" 3440x1440

Hydrogen server: Intel i3-10100 | Cryorig M9i | 64 GB Crucial Ballistix 3200MHz DDR4 | Gigabyte B560M-DS3H | 33 TB of storage | Fractal Design Define R5 | unRAID 6.9.2

Carbon server: Fujitsu PRIMERGY RX100 S7p | Xeon E3-1230 v2 | 16 GB DDR3 ECC | 60 GB Corsair SSD & 250 GB Samsung 850 Pro | Intel i340-T4 | ESXi 6.5.1

Big Mac cluster: 2x Raspberry Pi 2 Model B | 1x Raspberry Pi 3 Model B | 2x Raspberry Pi 3 Model B+

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, slitherin96 said:

jee do they make us do this just to torment us

No. They’re helping your problem solving skills. 

Link to comment
Share on other sites

Link to post
Share on other sites

57 minutes ago, fpo said:

No. They’re helping your problem solving skills. 

thing is the lecture was only about bitwise operations and they wanted the result as a boolean statement

Link to comment
Share on other sites

Link to post
Share on other sites

38 minutes ago, slitherin96 said:

thing is the lecture was only about bitwise operations and they wanted the result as a boolean statement

Well that’s what I figured. With programming there’s many different solutions to any problem. I wouldn’t have gone your route at all. I would give you my solution but I can’t do your homework for you. 


If I understand it properly, (potential answer. Don’t open till after you solve it for yourself) 

Spoiler

 

Basically I’d make an array and then do if array size minus 3 is five return true. 

Which after reviewing the question would mean I’d have to put the number into an array. 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Or convert to string and use string operations would be my way, regardless practically of the language. Something like:
 



If (_input.ToString().IndexOf("5") == 2)

{

     //do whatever you need here

}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×