EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

Left shift the elements in an array

int arr[]={10, 20, 30, 40, 50}

           0 1 2 3 4

output: 20 30 40 50 10

————————————————

Find a given number is odd or even without modulus or divide

——————————————————-

Move all the leading zeros to the tail end of an array

int arr[]={7, 0, 3, 0, 5}

output: 7 3 5 0 0

————————————-

class Leftshift

{

public static void main(String args[])

{

int arr[]={10,20,30,40,50};

int temp=arr[0];//10

int i=0;

while(i<arr.length-1)//4<4 20 30 40 50 10

{

arr[i]=arr[i+1];//arr[3]=arr[4]

i++;

}

arr[i]=temp;//arr[4]=temp

System.out.println(“Left shift elements=” +Arrays.toString(arr));}}

———————–

0–>0000 3–>0011 8–>1000

1–>0001 1–>0001 1–>0001

2–>0010

3–>0011 res-0001 0000

4–>0100

5–>0101 (odd number &1)–>result combination of 1

6–>0110 (even numb &1)–>result zero

int n,k;

System.out.println(“enter any number:”);//15

Scanner s=new Scanner(System.in);

n=s.nextInt();

k=n&1;

if(k==0)

System.out.println(“even”);

else

System.out.println(“odd”);

————————————

class Movezero

{

public static void main(String args[])

{

int arr[]={7,0,3,0,5};

int res[]=new int[arr.length];//res[7 3 5 0 0]

int cc=0;//cc=0

for(int i=0;i<arr.length;i++)//5<5

{

 if(arr[i]!=0)//5!=0(t)

  {

   res[cc]=arr[i];//res[2]=arr[4]

   cc++;//cc=3

  }

}

 for(int i=cc;i<arr.length;i++)//i=4 4<5

 {

 res[i]=0;

 }

for(int num:res)

{

System.out.println(num+” “);

}

}}

Java Left shift the elements in an array

Leave a Reply

Your email address will not be published. Required fields are marked *

EduLearn - Online Education Platform

Welcome to EduLearn!

Start learning today with our wide range of courses taught by industry experts. Gain new skills, advance your career, or explore new interests.

Browse Courses

Popular Courses

[Course Image]

Introduction to Programming

Learn the fundamentals of programming with Python in this beginner-friendly course.

12 Hours Beginner
[Course Image]

Data Science Essentials

Master the basics of data analysis, visualization, and machine learning.

20 Hours Intermediate
[Course Image]

Web Development Bootcamp

Build modern websites with HTML, CSS, JavaScript and popular frameworks.

30 Hours Beginner
[Course Image]

Digital Marketing Fundamentals

Learn SEO, social media marketing, email campaigns and analytics.

15 Hours Beginner
Educational Website Footer