Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
#include<stdio.h>
void main()
{
     float basic, da, hra, tax, pf, gross, net;
     char name[50];
     clrscr();
     printf("\n\n\t ENTER YOUR NAME...:");
     scanf("%s", &name);
     printf("\n\t ENTER THE BASIC SALARY...:");
     scanf("%f", &basic);
     pf = 0.08 * basic;
     if (basic < 5000)
     {
          da = 0.3 * basic;
          hra = 0.08 * basic;
     }
     else if ((basic >= 5000) && (basic < 10000))
     {
          da = 0.4 * basic;
          hra = 0.1 * basic;
     }
     else
     {
          da = 0.5 * basic;
          hra = 0.2 * basic;
     }
     gross = basic + da + hra;
     net = gross - tax + pf;
     printf("\n\n\t THE GROSS SALARY IS...: %f", gross);
     printf("\n\n\t THE NET SALARY IS...: %f", net);
     getch();
}
Posted

Cretae it as a console application, and replace printf with Console.WriteLine and scanf with Console.ReadLine plus the appropriate conversion method to change between a string and the required datatype. You will need to change the syntax of the last two lines from "%f" to "{0}", and change the datatype of name from char[50] to string

To convert a string to a double:
C#
double d = double.Parse(myStringValue);
I think you can guess the other datatypes!

Most of the code will work unchanged - it is only the I/O stuff you will need to modify, pretty much.
 
Share this answer
 
Well clrscr(); is the equivalent of Console.Clear();. printf equates to Console.Write(); (but if you want it to run to new lines, use Console.WriteLine();. getch(); is similar to Console.ReadKey(); and scanf(); can be thought of as Console.ReadLine();.
 
Share this answer
 
Comments
rockpune 7-Mar-12 9:15am    
i want in windows form
Pete O'Hanlon 7-Mar-12 9:18am    
Then use some labels and some textboxes. The actual if/else logic should remain fairly unchanged.
I guess you are looking for the C# Versions of printf and scanf?

Take a look at System.Console WriteLine and Read might help you.

C#
Console.WriteLine("Enter your name");
string name = Console.ReadString();
// ...
 
Share this answer
 
Comments
rockpune 7-Mar-12 9:17am    
in want to write only this part pf = 0.08 * basic;
if (basic < 5000)
{
da = 0.3 * basic;
hra = 0.08 * basic;
}
else if ((basic >= 5000) && (basic < 10000))
{
da = 0.4 * basic;
hra = 0.1 * basic;
}
else
{
da = 0.5 * basic;
hra = 0.2 * basic;
}
gross = basic + da + hra;
net = gross - tax + pf;
rockpune 7-Mar-12 9:18am    
am entering this values in textbox in C# windows form how can i write
Andy411 7-Mar-12 9:21am    
double basic = double.Parse(textBoxBasic.Text);
C#
using System;

static void Main()
{
     float basic;
     float da;
     float hra;
     float tax;
     float pf;
     float gross;
     float net;
     string name = string.Empty;
     Console.Clear();
     Console.Write("\n\n\t ENTER YOUR NAME...:");
     Console.ReadLine("%s", name);
     Console.Write("\n\t ENTER THE BASIC SALARY...:");
     Console.ReadLine("%f", basic);
     pf = 0.08 * basic;
     if (basic < 5000)
     {
          da = 0.3 * basic;
          hra = 0.08 * basic;
     }
     else if ((basic >= 5000) && (basic < 10000))
     {
          da = 0.4 * basic;
          hra = 0.1 * basic;
     }
     else
     {
          da = 0.5 * basic;
          hra = 0.2 * basic;
     }
     gross = basic + da + hra;
     net = gross - tax + pf;
     Console.Write("\n\n\t THE GROSS SALARY IS...: {0:f}", gross);
     Console.Write("\n\n\t THE NET SALARY IS...: {0:f}", net);
     Console.ReadKey(true).KeyChar;
}
 
Share this answer
 
v2
Comments
rockpune 7-Mar-12 9:21am    
i have to enter these values in textboxes plz tell me how can i do this
Herman<T>.Instance 7-Mar-12 9:23am    
you only asked to write the code in c# You never mentioned that you wanted a winform. Maybe Improve your question.
Hi rockpune,

Here is a example you can run. Just create a new WindowsForms project and replace Program.cs with the following code.

C#
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            // Create a Form with the needed controls (just for example, you'd better create it with the designer)
            Form form = new Form();
            Label labelName = new Label();
            labelName.Text = "Enter your name:";
            labelName.Dock = DockStyle.Top;
            Label labelBasic = new Label();
            labelBasic.Text = "Enter the basic salary";
            labelBasic.Dock = DockStyle.Top;
            TextBox textboxName = new TextBox();
            textboxName.Dock = DockStyle.Top;
            TextBox textboxBasic = new TextBox();
            textboxBasic.Dock = DockStyle.Top;
            Label labelGrossSalary = new Label();
            labelGrossSalary.Dock = DockStyle.Top;
            Label labelNetSalary = new Label();
            labelNetSalary.Dock = DockStyle.Top;
            Button buttonCalculate = new Button();
            buttonCalculate.Text = "Calculate";
            buttonCalculate.Dock = DockStyle.Top;
            // if we click the "Calculate" button do your maths...
            buttonCalculate.Click += delegate(object sender, EventArgs e)
            {
                // read inputs
                string strName = textboxName.Text; // we do nothing with this name?
                float basic;
                // because maybe the entered basic salary is not a valid number we use TryParse here
                if (float.TryParse(textboxBasic.Text, out basic)) // think about different cultures (decimal seperator)! 
                {
                    float net, gross;
                    Calculate(basic, out net, out gross);

                    // show results
                    labelGrossSalary.Text = gross.ToString();
                    labelNetSalary.Text = net.ToString();
                }
                else
                {
                    MessageBox.Show(textboxBasic.Text + " is not a valid number");
                }
            };
            form.Controls.AddRange(new Control[] { labelGrossSalary, labelNetSalary, buttonCalculate, textboxBasic, labelBasic, textboxName, labelName });

            Application.Run(form);
        }

        static void buttonCalculate_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        static void Calculate(float basic, out float net, out float gross)
        {
            float da;
            float hra;
            float tax = 0f; // no tax given in your example!
            float pf;

            pf = 0.08f * basic;
            if (basic < 5000)
            {
                da = 0.3f * basic;
                hra = 0.08f * basic;
            }
            else if ((basic >= 5000) && (basic < 10000))
            {
                da = 0.4f * basic;
                hra = 0.1f * basic;
            }
            else
            {
                da = 0.5f * basic;
                hra = 0.2f * basic;
            }

            // assign output variables
            gross = basic + da + hra;
            net = gross - tax + pf;
        }
    }
}


Btw: you can tag your question with "forms" and you can tell us you want a form solution first... anyway this code seems to be homework, if I wasn't so bored today I wouldn't have created an example for such a "bad" question
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900