1. //Assignment 2
/*Name-Ankit gupta
Branch-CSE
ROLL NO.-16 */
Thisprogram iswrittenbyAnkitGupta.
/* problem 1 : Write a program to find sum for first n numbers.*/
import java.util.Scanner;
class Sum{
public static void main(String args[]){
int sum=0;
System.out.println("input no.");
Scanner ak=new Scanner(System.in);
int n= ak.nextInt();
for(int i=0;i<=n;i++)
{
sum=sum+i;
}
System.out.println("sum: "+sum);
}
}
2. //Assignment 2
/*Name-Ankit gupta
Branch-CSE
ROLL NO.-16 */
Thisprogram iswrittenbyAnkitGupta.
/* problem2 : Write a Java Program to find the maximum of n numbers using do-while
loop.*/
import java.util.Scanner;
class Sum{
public static void main(String args[]){
System.out.println("enter the no. you want to insert: ");
Scanner ak= new Scanner(System.in);
int n=ak.nextInt();
int nu[]=new int[n];
int i=0;
do
{
nu[i]=ak.nextInt();
i++;
}while( i<n);
int max=nu[0];
int min=nu[n-1];
i=0;
int j;
if (n==0){
j=i;
max=nu[i];
}
else
{
do
{
j=n-1;
do{
if(nu[i]>nu[j])
max=nu[i];
else
max= nu[j];
j--;
}while(j>i);
i++;
}while(i<n);}
System.out.println(max);
}
}
4. //Assignment 2
/*Name-Ankit gupta
Branch-CSE
ROLL NO.-16 */
Thisprogram iswrittenbyAnkitGupta.
/* problem3: .Write a JAVA program to find all Armstrong numbers in the range of 0
and 999*/
class Armstrong{
public static void main( String args[])
{
int i=0;
do{ int n=I,k=0;
if(n>=0||n<=9)
k=1;
if(n>=10||n<=99)
k=2;
if(n>=100||n<=999)
k=3;
int j=n%10; int sum=0;
sum+=Math.pow(j,k);
n=n/10; j=n%10;
sum+=Math.pow(j,k);
n=n/10; j=n%10;
sum+=Math.pow(j,k);
if(sum==i)
System.out.println(sum);
i++;
}while(i<1000)}
}
}
5. //Assignment 2
/*Name-Ankit gupta
Branch-CSE
ROLL NO.-16 */
Thisprogram iswrittenbyAnkitGupta.
/* problem4: A common typing error */
import java.util.Scanner;
class Correction
{
public static void main(String args[])
{
String str="";
String s1="qwertyuiop[]asdfghjkl;'zxcvbnm,./ ";
System.out.println("Enter String");
Scanner ak= new Scanner(System.in);
String s4= ak.nextLine();
s4.toUpperCase();
s1.toUpperCase();
for (int i=0;i<s4.length();i++)
{
for(int j=0;j<s1.length();j++)
{
if(s4.charAt(i)==s1.charAt(0)||s4.charAt(i)==s1.charAt(12)||s4.charAt(i)==s1.charAt(2
3)||s4.charAt(i)==s1.charAt(33))
{
str+=s4.charAt(i);
break;
}
if(s4.charAt(i)==s1.charAt(j))
{
str=str+s1.charAt(j-1);
}
}
}
System.out.println(str.toUpperCase());
}
}