Showing posts with label Basic Programs. Show all posts
Showing posts with label Basic Programs. Show all posts

Friday, October 18, 2013

Java Program to generate a random number with output

 Code:-


class Test
{

    public static void main(String args[])
    {
       
        double a = Math.random();
        System.out.println(a);
    }
}

Output:-


Java Program to test whether a given year is a leap year or not


Code:-

import java.util.Scanner;
class Leap{
public static void main(String args[]){
System.out.println("Enter year");
Scanner s=new Scanner(System.in);
int a=s.nextInt();
if((a%100)==0){
if((a%400)==0)
System.out.println(a+" is a century year and a leap year too");
else
System.out.println(a+" is a century year");
}
else{
if((a%4)==0)
System.out.println(a+" is leap year ");
else
System.out.println(a+" is not a leap year");
}
}
}

Output:-


Monday, October 14, 2013

Java Program to read text using readline function

import java.io.*;
class Read{
public static void main(String args[]) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("Enter text \n stop to terminate" );
do{
str= br.readLine();
System.out.println(str);
}
while(str.equals("Stop"));
}
}

Friday, September 27, 2013

C++ Program using nested if else

Program

#include<iostream>
using namespace std;
int main(){
int a;
cout<<"Enter the value of a ";
cin>>a;
if(a>0)
    cout<<"A is greater than 0";
else if(a<0)
    cout<<"A is less than 0";
else if(a==0)
    cout<<"A is 0";
else
    cout<<"Wrong Choice";
return 0;
}

Output:-

Saturday, September 21, 2013

Java program to create a frame using Swing

import javax.swing.*;
class P1{
public static void main(String args[]){
JFrame j=new JFrame();
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setSize(300,300);
j.setVisible(true);
}
}

Output:-


Tuesday, September 17, 2013

C++ Program to multiply two matrices with Output

Program


#include<iostream>
using namespace std;
int main(){
int i,j,a[3][3],b[3][3],c[3][3];
cout<<"Enter first matrix";
for(i=0;i<3;i++){
    for(j=0;j<3;j++){
        cin>>a[i][j];
    }
    cout<<endl;
}
cout<<"Enter second matrix";
for(i=0;i<3;i++){
    for(j=0;j<3;j++){
        cin>>b[i][j];
    }
cout<<endl;
}
for(i=0;i<3;i++){
    for(j=0;j<3;j++){
c[i][j]=a[i][j]*b[i][j];
    }
}
for(i=0;i<3;i++){
    for(j=0;j<3;j++){
        cout<<c[i][j]<<" ";
    }
cout<<endl;
}
return 0;
}



Output:-



Saturday, September 7, 2013

C++ Program to find the average of given numbers with output

#include<iostream>
using namespace std;
int main(){
int n,i,a[50];
float average,total=0;
cout<<"Enter the total number of elements(max 50)";
cin>>n;
for(i=0;i<n;i++){
    cout<<"Enter the "<<i+1<<" number:";
    cin>>a[i];
}
for(i=0;i<n;i++){
    total=a[i]+total;
}
average=total/n;
cout<<"The sum of the given numbers is "<<total<<endl;
cout<<"The average is "<<average;
return 0;
}

Output:-


Thursday, August 29, 2013

C++ Program to print floyd triangle

#include<iostream>
using namespace std;
int main(){
int n,a=0,i,j;
cout<<"Enter the number of rows:";
cin>>n;
for(i=0;i<=n;i++){
for(j=0;j<i;j++){
a=a+1;
cout<<" "<<a;
}
cout<<endl;
}
return 0;
}


Output:-

Wednesday, August 28, 2013

C++ Program to find the multiplication table of any number

#include<iostream>
using namespace std;
int main(){
int n,i,j;
cout<<"Enter any number:";
cin>>n;
for(i=1;i<11;i++){
cout<<n<<" * "<<i<<" = "<<(n*i)<<endl;
}
return 0;
}

Output:-

Tuesday, August 27, 2013

C++ Program to print triangle of stars

Downward triangle:-

#include<iostream>
using namespace std;
int main(){
int i,j;
for(i=5;i>0;i--){
for(j=1;j<=i;j++){
cout<<"*";
}
cout<<endl;
}
return 0;
}

output:-


Download the source code file (.cpp file) from dropbox


Upward triangle:-

#include<iostream>
using namespace std;
int main(){
int i,j;
for(i=1;i<6;i++){
for(j=1;j<=i;j++){
cout<<"*";
}
cout<<endl;
}
return 0;
}

Output:-


Download the source code file from dropbox








Ads Inside Post