Java bitwise operators

//program to demonstrate bitwise operators


//bitwise operators in java



import java.io.*;
public class Bitwise
{
public static void main(String s[])
{
int x=10,y=2;
System.out.println("Value of x= "+x+" Value of y="+y);
System.out.println("x>>2: "+(x>>2));
System.out.println("x<<2: "+(x<<2));
System.out.println("x>>>1: "+(x>>>1));
System.out.println("x&y: "+(x&y));
System.out.println("x|y: "+(x|y));
System.out.println("x^y: "+(x^y));
System.out.println("~x: "+(~x));
}
}

Output



java bitwise operators
Java bitwise operators


Java - client server connection using socket

//Java program to implement client server connection using socket.

//Socket programming in Java.


Server.java


import java.io.*;
import java.net.*;
class Server
{
ServerSocket ss;
Socket S;
BufferedReader br;
String str;
public Server()
{
try
{
ss=new ServerSocket(1025);
S=ss.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(S.getInputStream()));
System.out.println("Connected!!!");
do
{
str=br.readLine();
System.out.println(str);
}
while(!str.equals("e"));
ss.close();
}
catch(Exception ex)
{
}
}
public static void main(String s[]) throws Exception
{
new Server();
}
}


Client.java


import java.io.*;
import java.net.*;
class Client
{
public static void main(String s[]) throws Exception
{
try
{
Socket s1=new Socket("localhost",1025);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String line;
System.out.println("Enter the string, e for exit");
do
{
line=br.readLine();
PrintWriter pw=new PrintWriter(new OutputStreamWriter(s1.getOutputStream()),true);
pw.println(line);
}
while(!line.equals("Exit"));
s1.close();
}
catch(Exception ex)
{
System.out.println("Cannot Find Server");
}
}
}

Output


socket program - client server in java



C++ - Diagonal elements sum (without pointers)

// C++ program to find the sum of diagonal elements without using pointers.

#include<iostream.h>
#include<conio.h>
void main()
{
          int a[10][10],p[10];
          int m,n,i,j,s1=0,s2=0,k=0;
          cout<<"Enter the order of matrics: ";
          cin>>m>>n;
          cout<<"Enter the elements\n\n";
          for(i=0;i<m;i++)
          {
                   for(j=0;j<n;j++)
                   {
                             cin>>a[i][j];
                   }
          }
          cout<<"\n\nFirst diagonal elements are\n\n";
                    for(i=0;i<m;i++)
          {
       
                   cout<<a[i][k]<<"\t";
                   s1=s1+a[i][k];
                   k++;
          }
          cout<<"\n\nSecond diagonal elements are\n\n";
          k=m-1;
          for(i=0;i<n;i++)
          {
                   cout<<a[i][k]<<"\t";
                   s2=s2+a[i][k];
                   k--;
          }
          cout<<"\n\nSum of first diagonal elements is: ";
          cout<<s1;
          cout<<"\n\nSum of second diagonal elements is: ";
          cout<<s2;
          getch();
}

Output
diagonal elements sum matrix in c++


C++ program to find the sum of diagonal elements of a matrix using pointers

c program to implement swapping

// Swap values in three variables

# include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z,temp;
printf("Enter the value of x,y&z: ");
scanf("%d%d%d",&x,&y,&z);
temp=x;  // swapping starts here
x=y;
y=z;
z=temp;
printf("Values after swapping:\n");
printf("x= %d\ny= %d\nz= %d",x,y,z);
getch();
}

Output

swap 3 variables c program
Swap three variables in c