Posts

Showing posts from August, 2016

java program to calculate area and volume using constructor

/*constructor is a special type of method in which the name of the method is same as the class and there is no return type constructor are used to initialize the value of data.*/ //Area and volume of cylinder using constructor. import java.io.*; import java.util.*; class Vol{ double radius; double height; Vol(double r,double h){ radius = r; height = h; } double vol(){ return 3.14*radius*radius*height; } } class Area{     double radius;     double height;     Area(double r,double h){         radius=r;         height=h;     }     double area(){         return (2*3.14*radius*height) + (2*3.14*radius*radius);     } } class Area_Vol_Cylin_Const{ public static void main(String args[]){ Vol cylinder=new Vol(7,10); System.out.println("volume of cylinder is "+cylinder.vol()); Area area=new Area(7,10); System.out.println("Area is "+area.area()); } } OUTPUT- volume of cylinder is 1538.6000000000001 Area is 747.32

c++ program for fibonacci using recursion

#include<iostream> using namespace std; long fact(int num); int main() { int num; cout<<"Enter number for factorial "; cin>> num; cout<<"Factorial of "<< num <<endl<<fact(num); } long fact(int num) { if(num==0) return 1; else { return (num*fact(num-1)); } } OUTPUT- Enter number for factorial 5 Factorial of 5 120

c++ creating class person with two private variable name and age and two public functions

//create class person with two private variable name and age and two public fn void get data and void put data #include<iostream> using namespace std; class person { char name[50]; int age; public: void getdata(); void putdata(); }; void person::getdata() { cout<<"Enter the name : "; cin>>name; cout<<"Enter the age"; cin>>age; } void person::putdata() { cout<<"Name : "<<name; cout<<endl<<"age : "<<age; } int main() { person m; m.getdata(); m.putdata(); return 0; } OUTPUT- Enter the name : bum Enter the age21 Name : bum age : 21

c++ program to create an item with two private num int and float cost with two public functions

/*WAP to create a class item with two private variable  number -int  cost -float  and two public function void getdata() and void putdata()  */ #include<iostream> #include<stdio.h> using namespace std; class item { int number; float cost; public: void getdata(); void putdata(); }; void item::getdata() { cout<<"Enter the number : "; cin>>number; cout<<"Enter the cost"; cin>>cost; } void item::putdata() { cout<<"Number : "<<number; cout<<endl<<"Cost : "<<cost; } int main() { item m; m.getdata(); m.putdata(); return 0; } OUTPUT- Enter the number : 9 Enter the cost65.5 Number : 9 Cost : 65.5

c++ program for factorial of a number using recursion

#include<iostream> using namespace std; long fact(int num); int main() { int num; cout<<"Enter number for factorial "; cin>> num; cout<<"Factorial of "<< num <<endl<<fact(num); } long fact(int num) { if(num==0) return 1; else { return (num*fact(num-1)); } } output- Enter number for factorial 5 factorial of 5 120

call by reference in c++

/*In call by reference we pass (&)address as argument or parameter in definition of function and while calling the function we pass only variables of the original value.   */ #include<iostream> using namespace std; void swap(int &,int &); int main(){ int a,b; cout<<"Enter A and B "<<endl; cin>>a>>b; cout<<"Before swapping "; cout<<" A "<<a<<" B "<<b; swap(a,b); cout<<" After swapping "; cout<<" A "<<a<<" B "<<b; } void swap(int &a,int &b) { int temp; temp=a; a=b; b=temp; } output- Enter A and B 9 5 Before swapping  A 9 B 5 After swapping  A 5 B 9 https://www.youtube.com/watch?v=UPhHCWezk_g

call by address in c++

/*In call by address we pass pointer(*) in the argument in the difinition and while calling the function we pass address of the variable*/ #include<iostream> using namespace std; void swap(int *,int *); int main() {     int a,b;     cout<<"Enter A and B "<<endl;     cin>>a>>b;     cout<<"Before swapping ";     cout<<" A "<<a<<" B "<<b<<endl;     swap(&a,&b);//here we are passing address of a and b thats why we are using & operator with a and b.     cout<<"After swapping ";     cout<<" A "<<a<<" B "<<b; } void swap(int *x,int *y) {     int temp;     temp=*x;     *x=*y;     *y=temp; } output- Enter A and B  9 6 Before swapping  A 9 B 6 After swapping  A 6 B 9 https://www.youtube.com/watch?v=-LBxtVoFYc8

java program for searching an element in the array and displaying index number as well

package arraysearch; import java.io.*; import java.util.*; public class Arraysearch {         public static void main(String[] args) {             int num,i,num2,flag=0;                 System.out.println("Enter the size of array");                 Scanner sc=new Scanner(System.in);                 num=sc.nextInt();                 System.out.println("Enter the element in the array one by one");                 int array[]=new int[50];                 for(i=0;i<num;i++)                 {                     array[i]=sc.nextInt();                 }                 System.out.println("The number entered in the arrray is");                 for(i=0;i<num;i++)                 {                     System.out.println(array[i]);                 }                 System.out.println("Enter the element to search");                 num2=sc.nextInt();                 for(i=0;i<num;i++){                                  

java program for sorting array using selection sort

package arraysort; import java.io.*; import java.util.*; public class Arraysort {     public static void main(String[] args) {           int num,i,j,temp;         System.out.println("Enter the size of array");         Scanner sc=new Scanner(System.in);         num=sc.nextInt();         System.out.println("Enter elements in array one by one");         int array[]=new int [50];         for(i=0;i<num;i++)         {             array[i]=sc.nextInt();         }         System.out.println("The numbers entered in array is");         for(i=0;i<num;i++){             System.out.println(array[i]);         }         for(i=0;i<=num-1;i++)         {             for(j=1+i;j<=num-1;j++){         if(array[i]>array[j])         {             temp=array[i];             array[i]=array[j];             array[j]=temp;         }                 }                 }                 System.out.println("The sorted array is"

java program to find smallest element in array

package arraysmall; import java.io.*; import java.util.*; public class Arraysmall {     public static void main(String[] args) {           int num,i,min=0;                 System.out.println("Enter the size of array");                 Scanner sc=new Scanner(System.in);                 num=sc.nextInt();                 System.out.println("Enter elements in the array one by one");                 int array[]=new int [50];                               for(i=0;i<num;i++)                 {                     array[i]=sc.nextInt();                 }                 System.out.println(" The entered element in the array is");                 for(i=0;i<num;i++)                 {                     System.out.println(array[i]);                                           }                 System.out.println("The smallest element in the array is");                 min=array[0];                 for(i=0;i<num;i++)      

Java program to find the largest of element in the array

package array_largest; import java.util.*; public class Array_largest {       public static void main(String[] args) {             int num,i,max = 0;         System.out.println("Enter the size of the array");         Scanner sc= new Scanner(System.in);         num=sc.nextInt();        System.out.println("Enter the element in the array one by one");        int array[]=new int [100];        for(i=0;i<num;i++)        {            array[i]=sc.nextInt();        }        System.out.println("The entered element in the array is");        for(i=0;i<num;i++)        {            System.out.println(array[i]);              }        System.out.println("The largest value in array is ");        max=array[0];        for(i=0;i<num;i++)        {                      if(max<array[i])            {            max=array[i];                                }        }System.out.println(max);         }     } OUT- Ent

java program for printing the sum of the elements inn the array

package array_sum; import java.io.*; import java.util.Scanner;     public static void main(String[] args) {           int num,i,sum=0;         System.out.println("Enter the size of array");         Scanner sc=new Scanner (System.in);         num=sc.nextInt();         int array[]=new int [100];         System.out.println("Enter the elements in the array one by one");         for(i=0;i<num;i++)         {             array[i]=sc.nextInt();         }         System.out.println("The entered element in the array is");         for(i=0;i<num;i++)         {             System.out.println(array[i]);         }         System.out.println("The sum of the array is");         for(i=0;i<num;i++)         {             sum=sum+array[i];         }         System.out.println(sum);     }   } OUT-Enter the size of array 5 Enter the elements in the array one by one 89 45 12 45 86 The entered element in the array

java program for creating an array and displaying its elements

import java.io.*; import java.util.Scanner; /**  *  * @author mithi  */ public class Arrayelement {       public static void main(String[] args) {         int num,i;     System.out.println("Enter the limit of array");     Scanner sc=new Scanner(System.in);     num=sc.nextInt();     int array_name[]=new int [100];     System.out.println("Enter the element in the array one by one");     for(i=0;i<num;i++)     {         array_name[i]=sc.nextInt();     }         System.out.println("Entered array is ");     for(i=0;i<num;i++)         System.out.println(array_name[i]);     }       } out- Enter the limit of array 5 Enter the element in the array one by one 45 12 89 32 78 Entered array is  45 12 89 32 78 BUILD SUCCESSFUL (total time: 12 seconds)

c programming for bubble sort

#include<stdio.h> void selection_sort(int arr[],int num); int main() { int arr[100],num,i; printf("Enter the size of array\n"); scanf("%d",&num); printf("\nEnter the elements in array one by one\n"); for(i=0;i<num;i++) { scanf("%d",&arr[i]); } selection_sort(arr,num); printf("The sorted array is\n"); for(i=0;i<num;i++) { printf("%d",arr[i]); } } void selection_sort(int arr[],int num) { int i,j,temp,min; for(i=0;i<=num-1;i++) { min=i; for(j=i+1;j<=num-1;j++) /*here in j=i+1 we are adding i to avoid error as we don't need to check for already sorted number as previously sorted number is kept in the same array */ { if(arr[j]<arr[i]) min=j; } temp=arr[i]; arr[i]=arr[min]; arr[min]=temp; } } OUTPUT- Enter the size of array 5 Enter the elements in array one by one 5 4 3 2 1 The sorted array is

c programming for bubble sort

#include<stdio.h> void bubble_sort(int [],int num); int main() { int arr[100],num,i; printf("\n Enter the size of the array\n"); scanf("%d",&num); printf("\n Enter the elements in the array one by one\n"); for(i=0;i<num;i++) { scanf("%d",&arr[i]); } bubble_sort(arr,num); //output for(i=0;i<num;i++) { printf("%d",arr[i]); } } void bubble_sort(int arr[],int num) { int i,j,temp; for(i=1;i<=num-1;i++)//i is for passes, if the size of the array is 4 we need to go for n-1 indices only { for(j=0;j<=num-i-1;j++)// here in j<=num-i-1 we are subtracting i as in each pass one element is sorted so to avoid comaring with that we are sutracting i means with pass { if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } } OUTPUT-  Enter the size of the array 4  Enter the elements in the array one by one 9 8 6 4 4689

Java program to find fibonacci series

import java.io.*; import java.util.*; class fibonacci { public static void main(String args[]) { int num,num1=0,num2=1,num3; System.out.println("Enter the limit\n"); Scanner sc= new Scanner(System.in); num=sc.nextInt(); System.out.println(num1); System.out.println(num2); for(int i=2;i<num;i++) //already printed two numbers 0 and 1 that'swhy our i initiiialize from 2 skipping first two iteration { num3=num1+num2; num1=num2;     num2=num3; System.out.println(num3); } } } OUTPUT- Enter the limit 5 0 1 1 2 3

java program to find sum of series

import java.io.*; import java.util.*; class sum_series{ public static void main(String args[]) { int num1,num2=0; System.out.println("\nEnter the limit\n"); Scanner sc=new Scanner(System.in); num1=sc.nextInt(); for(int i=0;i<=num1;i++) { num2=num2+i; } System.out.println(num2); } } OUTPUT- Enter the limit 5 15

java program for to find largest among three numbers

import java.io.*; import java.util.*; class largest_number{ public static void main(String args[]){ int num1,num2,num3; System.out.println("Enter three numbers one by one"); Scanner sc=new Scanner(System.in); num1=sc.nextInt(); num2=sc.nextInt(); num3=sc.nextInt(); if(num1>num2 && num1>num3) System.out.println("The greater number is\n"+num1); if(num2>num1 && num2>num3) System.out.println("The greater number is\n"+num2); if(num3>num1 && num3>num2) System.out.println("The greater number is\n"+num3); } } OUTPUT- Enter three numbers one by one 6 9 8 The greater number is 9

C++ program to compare two strings are same or not without using string comparision function

//using for loop #include<iostream> using namespace std; int main( ) {     char str1[80], str2[80];     cout<<"Enter first string: ";    cin>>str1;     cout<<"Enter second string: ";     cin>>str2;     int i;     for (i = 0; str1[i] == str2[i] && str1[i]!='\0' && str2[i]!='\0'; i++);     if(str1[i] - str2[i] == 0)         cout << "Strings are equal";     else         cout << "Strings are not equal";     return 0; } Enter first string: bum Enter second string: bum Strings are equal //using while loop /* This program compare two strings entered by the user and displays the results whether they are  same or not*/ #include<iostream> using namespace std; int main() {  char str1[80], str2[80];  int i=0;    cout<<"Enter first string: ";  cin>>str1;//input-first string    cout<<"

c++ program to sort predefined numbers

#include<iostream> using namespace std; //int sort_given_num(int arr[]); int main() { int i,j,temp; int arr[8]={12,10,14,18,2,20,7,30}; for(i=0;i<8;i++) { for(j=1;j<8;j++) if(arr[j]<arr[i]); { temp=arr[i]; arr[j]=arr[i]; arr[i]=temp; } } for(i=0;i<8;i++) cout<<arr[i]<<endl; } OUT: 12 10 14 18 2 20 7 30

c++ program to search a number in array

#include<iostream> using namespace std; void search_array(int arr[],int num); int main() { int num,arr[100]; cout<<"Enter the size of the array\n"; cin>>num; cout<<"Enter elements in the array\n"; for(int i=0;i<=num;i++) { cin>>arr[i]; } search_array(arr,num); } void search_array(int arr[],int num) { int i,number,flag=0; cout<<"Enter the number to search in the array\n"; cin>>number; for(i=0;i<=num;i++) { if(arr[i]==number) flag=1; } if(flag==1) cout<<"Found in index "<<i;//i to display index number of array else cout<<"Not found"; } OUT: Enter the size of the array 2 Enter elements in the array 45 47 12 Enter the number to search in the array 12 Found in index 3

c++ program to check whether string is palindrome or not, using library function

#include<iostream> #include<cstring> using namespace std; int main(){     char string1[20];     int i, length;     int flag = 0;       cout << "Enter a string: ";     cin >> string1;       length = strlen(string1);//using library function       for(i=0;i < length ;i++){         if(string1[i] != string1[length-i-1]){             flag = 1;             break;   } }       if (flag==1){         cout << string1 << " is not a palindrome" << endl;     }       else {         cout << string1 << " is a palindrome" << endl;     }       return 0; } OUT: Enter a string: wow wow is a palindrome

c program to find prime factors of a number

#include<stdio.h> int prime_number(int num); int main() { int num; printf("\n Enter a number for prime factor"); scanf("%d",&num); prime_number(num); } int prime_number(int num) { int i=2; while(num!=1) { if(num%i==0) { while(num%i==0) { printf("%d\t",i); num=num/i; } } i++; } } OUT:  Enter a number for prime factor420 2 2 3 5 7 To understand go here  https://www.youtube.com/watch?v=Xg89vIL9aog

C program for prime or not

#include<stdio.h> int main() { int num,flag=0; printf("Enter a number\t"); scanf("%d",&num); //if the number is divisible by half of its number that is if n=6 then it is divided by 3 like n/3 and n is divided by 1,2,3 and if remainder is zero number is not prime for(int i=2;i<=num/2;i++) { if(num%i==0) { flag=1; break; } } if (flag==1) printf("Not Prime\n"); else printf("\n Prime\n"); } out: Enter a number 4 Not Prime

C++ program to find max and min in the array using function

#include<iostream> using namespace std; int arr_max_min(int arr[],int num); int main() { int  num,i,arr[100],result; cout<<"Enter the size of the array\n"; cin>>num; cout<<"Enter numbers in the array one by one \n"; for(i=0;i<num;i++) { cin>>arr[i]; } result=arr_max_min(arr,num); } int arr_max_min(int arr[],int num) //displaying output in the function declartion part { int max,min,i; max=arr[0]; for(i=0;i<num;i++) { if(arr[i]>max) max=arr[i]; } //return max; as we are already printing the output so function can not return anything cout<<"The maximuum in the array is \t"<<max; for(i=0;i<num;i++) { min=arr[0]; if(arr[i]<min) min=arr[i]; //return min; } cout<<"\nThe minimum in the array is\t"<<min; } OUTPUT: Enter the size of the array 3 Enter numbers in the array one by one  9  6 3 The maximuum in the array is 9 The minimu

C++ program to find whether a number is even or odd

#include<iostream> using namespace std; int odd_even(int num); int main() { int num,flag; cout<<"Enter a number to check for odd or even\n "; cin>>num; flag=odd_even(num); if(flag==1) cout<<"Even"; else cout<<"odd"; } int odd_even(int num) { if(num%2==0) return 1; else return 0; } OUTPUT: Enter a number to check for odd or even  3 odd

C++ program to find area of circle using function

#include<iostream> using namespace std; float area_circle(int radius); int main() { float radius,area; cout<<"Enter the radius of the circle to find the area\n"; cin>>radius; area=area_circle(radius); cout<<"Area of the circle is \n"<<area; } float area_circle (int radius) { float area; area=3.14*radius*radius; return area; } OUTPUT: Enter the radius of the circle to find the area 2 Area of the circle is  12.56

C++ program to find add of two number using function

#include<iostream> using namespace std; int addtwo(int num1, int num2); int main() { int num1,num2,num3; cout<<"\n Enter first and second number \n"; cin>>num1>>num2; num3=addtwo(num1,num2); cout<<"\n The sum of num1 and num2 \t"<<num3; } int addtwo(int num1,int num2) {int num3; num3=num1+num2; return num3; } OUTPUT:  Enter first and second number  9 6  The sum of num1 and num2 15

C++ program to find largest of three number using function

#include<iostream> using namespace std; int largest (int num1,int num2,int num3); int main() { int num1,num2,num3,large; cout<<"Enter three numbers one by one \n"; cin>>num1>>num2>>num3; large=largest(num1,num2,num3); cout<<"The largest number in num1,num2,num3 is \t"<<large; } int largest(int num1,int num2,int num3) { if(num1>num2 && num1>num3) return num1; if(num2>num1 && num2>num3) return num2; else return num3; } OUTPUT: Enter three numbers one by one 9 6 7 The largest number in num1,num2,num3 is 9

C++ program to find minimum and maximum element in array

without using function #include<iostream> using namespace std; int main() { int a[100],n,i,min,max; cout<<"Enter the size of array\n"; cin>>n; cout<<"Enter the elements one by one\n"; for(i=0;i<n;i++) cin>>a[i]; min=a[0]; for(i=0;i<n;i++) { if(a[i]<min) { min=a[i]; } } cout<<"min in the array is "<<min<<"\n"; max=a[0]; for(i=0;i<n;i++) if(a[i]>max) { max=a[i]; } cout<<"max in the array is "<<max; } OUTPUT:- Enter the size of array 3 Enter the elements one by one 5 8 4 min in the array is 4 max in the array is 8