Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

2/6/2020 Prepared by:- Raj Kumar Jain

QUERY FOR LOOPS PROGRAMS

Q.6 Write a program to input 100 characters count and print total upper case, total lower
case, total digits and total special characters.

import java.util.*;
class Prog6
{
public static void main ( )
{
Scanner sc = new Scanner (System.in) ;
int i , uc=0, lc=0, d=0 , sc=0;
char ch;
for (i=1 ; i<=100 ; i++)
{
System.out.println(“Enter character ”);
ch=sc.next().charAt(0); or
if (ch>=’A’ && ch<=’Z’) if (ch>=65 && ch<=90)
uc++;
else if (ch>=’a’ && ch<=’z’) else if (ch>=97 && ch<=122)
lc ++;
else if (ch>=’0’ && ch<=’9’) else if (ch>=48 && ch<=57)
d++;
else
sc+ +;
}
System.out.println(“Total Upper Case ”+ uc);
System.out.println(“Total Lower Case ”+lc);
System.out.println(“Total Digit ”+d);
System.out.println(“Total Special Characters “+sc);
}
}

1
2/6/2020 Prepared by:- Raj Kumar Jain

Q.7 Write a program to input 55 numbers count and print total single digits, double digits
and total three digits numbers.

import java.util.*;
class Prog7
{
public static void main ( )
{
Scanner sc = new Scanner (System.in) ;
int i , n , c=0, d=0, e=0 ;
for (i=1 ; i<=55 ; i++)
{
System.out.println(“Enter no ”);
n=sc.nextInt();
if (n>=0 && n<=9)
c++;
else if (n>=10 && n<=99)
d++;
else if (n>=100 && n<=999)
e++;
}
System.out.println(“Total Single Digits nos ”+ c);
System.out.println(“Total Double Digits nos”+d);
System.out.println(“Total Three Digits nos”+e);
}
}

2
2/6/2020 Prepared by:- Raj Kumar Jain
Q.8 Write a program to input 25 numbers print greatest numbers.

import java.util.*;
class Prog8
{
public static void main ( )
{
Scanner sc = new Scanner (System.in) ;
int i , n , g=0 ;
for (i=1 ; i<=25 ; i++)
{
System.out.println(“Enter no ”);
n=sc.nextInt();
if (n > g )
g=n ;
}
System.out.println(“Greatest no=”+ g);
}
}

Q.9 Write a program to input 50 numbers print smallest numbers.

import java.util.*;
class Prog9
{
public static void main ( )
{
Scanner sc = new Scanner (System.in) ;
int i , n , s ;
System.out.println(“Enter no ”);
n=sc.nextInt();
s=n;
for (i=1 ; i<=49 ; i++)
{
System.out.println(“Enter no ”);
n=sc.nextInt();
if (n < s )
s=n ;
}
System.out.println(“Smallest no=”+ s);
}
}
3
2/6/2020 Prepared by:- Raj Kumar Jain

Q.10 Write a program to input cost and quantity of 20 items. Calculate and print total amount
of each item and also print grant total.

import java.util.*;
class Prog10
{
public static void main ( )
{
Scanner sc = new Scanner (System.in) ;
int i , c , q, ta, gt=0 ;
for (i=1 ; i<=20 ; i++)
{
System.out.println(“Enter cost and quantity ”);
c=sc.nextInt();
q=sc.nextInt();
ta = c * q ;
gt = gt + ta ;
System.out.println(“Total Amount=”+ta) ;
}
System.out.println(“Grand Total =”+ gt);
}
}

4
2/6/2020 Prepared by:- Raj Kumar Jain

Q.11 Write a program to input 25 numbers print factorial of even numbers.

import java.util.*;
class Prog11
{
public static void main ( )
{
Scanner sc = new Scanner (System.in) ;
int i , j , f ;
for (i=1 ; i<=25 ; i++)
{
System.out.println(“Enter no ”);
n=sc.nextInt();
f=1;
if (n% 2 = = 0)
{
for (j=1 ; j<=n ; j++)
f = f * j;
System.out.println( f ) ;
}
}
}
}

5
2/6/2020 Prepared by:- Raj Kumar Jain

Q.12 Write a program to input 50 numbers print only prime numbers.


import java.util.*;
class Prog12
{
public static void main ( )
{
Scanner sc = new Scanner (System.in) ;
int i , j , c;
for (i=1 ; i<=50 ; i++)
{
System.out.println(“Enter no”);
n=sc.nextInt();
c=0 ;
{
for (j=1 ; j<=n ; j++)
{
if (n% j = = 0)
c++;
}
if (c = = 2 )
System.out.println( n ) ;
}
}
}

You might also like