Sunday, 16 August 2015

WAP in java to accept n from user and to display all odd nos from 1 up to n.

//Odd nos. from 1 up to given range

import java.util.Scanner;

class Range_odd {

public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a no:");
int n=sc.nextInt();

if(n<1)
System.out.println("INVALID INPUT");
else
{
for(int i=1;i<=n;i=+2)
System.out.println(i);
}
}

}

WAP in java to accept n from user and to display all even and odd nos from 1 up to n.

//Even and odd nos. from 1 up to given range

import java.util.Scanner;

class Range_evn_odd {

public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a no:");
int n=sc.nextInt();

if(n<1)
System.out.println("INVALID INPUT");
else
{
System.out.println("Even Nos:");
for(int i=2;i<=n;i=+2)
System.out.println(i);

System.out.println("Odd Nos:");
for(int i=1;i<=n;i=+2)
System.out.println(i);


}
}

}

WAP in java to accept n from user and display all even nos from 1 up to n.

//Even nos. from 1 up to given range

import java.util.Scanner;

class Range_even {

public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a no:");
int n=sc.nextInt();

if(n<1)
System.out.println("INVALID INPUT");
else
{
for(int i=2;i<=n;i=+2)
System.out.println(i);
}
}

}

WAP in java to accept cost price and selling price of an item and determine whether the profit or loss will be there and also determine how much profit or loss incurred.

//Determine whether(and how much) profit or loss is incurred

import java.util.Scanner;

class ProfitOrLoss {

public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Selling Price and Cost Price(respectively):");
float sp=sc.nextFloat();
float cp=sc.nextFloat();

if(sp==cp)
System.out.println("Neither profit nor loss");
else if(sp>cp)
System.out.println("Profit="+(sp-cp));
else
System.out.println("Loss="+(cp-sp));
}


}

If the total selling price of 15 items and the total profit earned on item is input through the keyboard,WAP in java to find cost price of one item.

//Calculate cost price of a product

import java.util.Scanner;


public class Product_cp {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Selling price for 15 items(in total) and enter profit per item:");
float sp_t=sc.nextFloat();
float p=sc.nextFloat();

float cp=(sp_t/15)-p;
System.out.println("Cost price per item:"+cp);
}

}

WAP in java to convert positive no into negative no and vice versa.

//Interchange sign of a no

import java.util.Scanner;


class Pos_Neg {

public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a no:");
int n=sc.nextInt();

System.out.println("New No:"+(n*(-1)));
}


}

In a town,the percentage of men is 52.The percentage of total literacy is 48.If total percentage of literate men is 35 of the total populations,WAP in java to find the total no of illiterate men and women ,if the population of the town is 80000.

//Population=80000,Men=52% of population,Literate people=48% of population, Literate Men=35% of Population
//Program to display count of illiterate men and illiterate women

class Population {

public static void main(String args[])
{
long pop=80000,ilm,ilw;

ilm=(long)((0.52*pop)-(0.35*pop)); //Illiterate men
ilw=(long)((0.52*pop)-ilm); //Illiterate women

System.out.println("Population:"+pop);
System.out.println("Illiterate men:"+ilm);
System.out.println("Illiterate women:"+ilw);
}

}