Friday, October 20, 2017

Decimal to Hexadecimal and Octal in C++

Decimal to Hexadecimal and Octal in C++.
This program will display decimal number into Hexadecimal and Octal.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
  int number=100;
  cout << "Hexadecimal of 100 is: ";
  cout << hex << number << endl;
  cout << "Octal of 100 is: ";
  cout << oct << number << endl;
  return 0;
}

Output of program

Hexadecimal of 100 is: 64
Octal of 100 is: 144


Formatted output in C++


Using formatted output in C++ program

#include <iostream>
using namespace std;
int main()
{
   cout.precision(5) ;
   cout.width(10);
   cout << 12.345678 << "\n"; // output 12.346
 
   cout.fill('-');
   cout.width(10);
   cout << 12.345678 << "\n"; // output ----12.346
 
   cout.width(10);
   cout << "Hello!" << "\n"; // Output ----Hello!
   cout.width(10);
 
   cout.setf(ios::left); // left justify
   cout << 12.345678; // Output 12.346----
   return 0;
}

Output of program

    12.346
----12.346
----Hello!
12.346----


* * * * *

Sunday, October 15, 2017

Read one word from a file.



Following C++ program will read a word from data.txt file stored in the same location where source program is stored.

#include <fstream>
#include <iostream>
using namespace std;
int main () {   
   char word[20];

   ifstream fin;    //open file to read.
   fin.open("data.txt");
 
   cout << "Reading a word from a file...\n";
   
   fin >> word;
   cout << word;    //write data to screen.
 
   fin.close();     //close the opened file.
   return 0;
}

Output of program

Reading a word from a file...
hello



Tuesday, October 10, 2017

FTP server client simulation using socket program

This JAVA socket program will download webpage (HTML file) from server to client. Downloaded file will be opened into web browser.

SERVER side program

import java.io.*;
import java.net.*;

public class FTPServer {
  public static void main(String[] args) throws IOException {
    //server will start at port number 12345.
    ServerSocket servsock = new ServerSocket(12345);

    //server will send index1.htm file to client.
    File myFile = new File("D:\\index1.htm");
    System.out.println("Waiting for Client Request...");
    while (true) {
      Socket sock = servsock.accept();
      byte[] mybytearray = new byte[(int) myFile.length()];
      BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
      bis.read(mybytearray, 0, mybytearray.length);
      OutputStream os = sock.getOutputStream();
      os.write(mybytearray, 0, mybytearray.length);
      os.flush();
      sock.close();
    }
  }
}

SERVER side Output:

Waiting for Client Request...



CLIENT side program

import java.awt.Desktop;
import java.io.*;
import java.net.*;

public class FTPClient {
  public static void main(String[] argv) throws Exception {

    //socket will be created local IP: 127.0.0.1 and port 12345.
    Socket sock = new Socket("127.0.0.1", 12345);
    byte[] mybytearray = new byte[1024];
   
    InputStream is = sock.getInputStream();
    FileOutputStream fos = new FileOutputStream("D:\\index2.htm");
    BufferedOutputStream bos = new BufferedOutputStream(fos);
   
    int bytesRead = is.read(mybytearray, 0, mybytearray.length);
    bos.write(mybytearray, 0, bytesRead);
   
    System.out.println("File downloaded...");
   
    Following code will open index2.htm file in browser.

    String url="D:\\index2.htm";
    File htmlFile=new File(url);
    Desktop.getDesktop().browse(htmlFile.toURI());

    bos.close();
    sock.close();
  }
}

CLIENT side output:

File downloaded...
BUILD SUCCESSFUL (total time: 1 second)



Thursday, September 21, 2017

2G 3G 4G 5G Technology

1G, 2G, 3G, 4G અને 5G ટેક્નોલોજી

  • 1જી (અથવા 1-જી) એ વાયરલેસ ટેલિફોન ટેકનોલોજીની પ્રથમ પેઢી (મોબાઇલ ટેલિકોમ્યુનિકેશન)ની ટેક્નોલોજી છે. તે એનોલોગ ટેલિકમ્યુનિકેશન પર કામ કરે છે અને 1980 ના દાયકામાં પ્રચલિત હતી. 
  • 2 જી (અથવા 2-જી) ટેક્નોલોજી એ 1જી  કરતા ત્રણ વધારાના લાભો પૂરા પાડે છે. 
    • તેમાં ફોન પરની વાતચીત ડિજિટલ એન્ક્રિપ્ટ કરવામાં આવે છે  જે વધારે સુરક્ષિત છે.
    • 2 જી સિસ્ટમો પર ઘણાં વધુ મોબાઇલ ફોન એક સાથે વાપરી શકાય છે, તેથી સ્પેક્ટ્રમ પર વધુ કાર્યક્ષમ છે;
    • 2 જી મોબાઇલમાં ડેટા સેવાઓ વાપરી શકાય છે. જેમ કે એસએમએસ (શૉર્ટ મેસેજ સર્વિસ) મોકલી શકાય છે. 2 જી થી ટેક્સ્ટ સંદેશાઓ, ચિત્ર સંદેશાઓ અને એમએમએસ (મલ્ટિમિડીયા સંદેશ સેવા) જેવી સેવાઓ પૂરી પડી શકાય છે.
  • 3G ટેકનોલોજી ઓછામાં ઓછા 200 kbit / s ની માહિતી ટ્રાન્સફર રેટ પૂરી પાડે છે.
  • 4જી ટેક્નોલોજી વૉઇસની સાથે સાથે, મોબાઇલ બ્રોડબેન્ડ ઇન્ટરનેટ એક્સેસ, વાયરલેસ મોડેમ સાથેના લેપટોપ્સ અને સ્માર્ટફોન્સમાં સુવિધાઓ પૂરી પાડે છે. 4જી આધારિત કેટલીક એપ્લિકેશન્સ આ પ્રમાણે છે: મોબાઇલ વેબ ઍક્સેસ, ગેમિંગ, આઇપી ટેલિફોની, મોબાઇલ ટીવી અને  વિડીયો કોન્ફરન્સિંગ.
  • 5જી એ પાંચમી પેઢીની નવીનતમ મોબાઇલ વાયરલેસ સ્ટાન્ડર્ડ છે. 5જી એ 4જી કરતાં વધુ ઝડપી હશે, 10,000 એમબીપીએસની સૈદ્ધાંતિક ડાઉનલોડ સ્પીડ સાથે તમામ સક્ષમ ઉપકરણોમાં ઉચ્ચ ઉત્પાદકતા માટે પરવાનગી આપશે.
* * * * *

Friday, September 1, 2017

Program to check Armstrong number

Example of Armstrong number

153 = 1*1*1 + 5*5*5 + 3*3*3

An Armstrong number of three digits is the integer number such that the sum of the cubes of its all digits is equal to the number itself.


Program to check Armstrong number

#include <stdio.h>
int getPower(int, int);

int main()
{
   int num, tmp, total = 0, remainder, no_of_digits = 0;

   printf("Enter integer number: ");
   scanf("%d", &num);
   tmp = num;
   // Count number of digits in given number
   while (tmp != 0) 
   {
      no_of_digits++;
      tmp = tmp/10;
   }
  //Logic to get total as per Armstrong formula.
   tmp = num;
   while (tmp != 0) 
   {
      remainder = tmp%10;
      total = total + getPower(remainder, no_of_digits);
      tmp = tmp/10;
   }

   if (total == num)
      printf("%d is Armstrong number.", num);
   else
      printf("%d is not Armstrong number.", num);

   return 0;
}

int getPower(int n1, int r1) {
   int i, power=1;

   for (i=1; i<=r1; i++) 
      power = power * n1;

   return power;   
}

Output-1
Enter integer number: 153
153 is Armstrong number.

Output-2
Enter integer number: 125
125 is not Armstrong number.