0% found this document useful (0 votes)
25 views13 pages

Comp Programs

comp prog class 12

Uploaded by

yash98u6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views13 pages

Comp Programs

comp prog class 12

Uploaded by

yash98u6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Name :- YASH SINGH

CLASS :- XI – D

SUBJECT:- COMPUTER
ACKNOWLEDGMENT
I would like to express my special
thanks of gratitude to my
Computer teacher Siddharth sir as
well as our principal Manjit batra
who gave me the golden
opportunity to do this wonderful
project. Secondly, I would also like
to thank my parents and friends
who helped me a lot in finalizing
this project within the limited time
frame. Lastly, I like to thank all
my supporters who have
motivated me to fulfill their
project before the timeline.

INDEX
Sn. Topic Pg. no. remarks
1 Prg no 1 1
2 Prg no 2 2
3 Prg no 3 3
4 Prg no 4 4
5 Prg no 5 5
6 Prg no 6 6
7 Prg no 7 7
8 Prg no 8 8
9 Prg no 9 9
10 Prg no 10 10
Q-1 write a program to enter terms and make
Fibonacci series out of it.
import java.util.*;

class Fibonacci
{

int fibo(int n)

if(n<=1)

return (n);

else

return (fibo(n-1)+fibo(n-2));

public static void main(String args[])

int p;

Scanner sc=new Scanner(System.in);

Fibonacci obj1=new Fibonacci();

System.out.println("Enter no of terms");

p=sc.nextInt();

int i;

System.out.println("Fibonacci Series is ");

for(i=0; i<=p; i++)

System.out.print(obj1.fibo(i)+"\t");

Q-2 write a program to convert a decimal no. to binary


no.
import java.util.*;
class Conversion

void dectobin(int n)

if(n>0)

int k=n%2;

System.out.print(k);

dectobin(n/2);

public static void main(String args[])

Conversion obj1=new Conversion();

Scanner sc=new Scanner(System.in);

System.out.println("Enter a number");

int p= sc.nextInt();

System.out.println("Binary number is");

obj1.dectobin(p);

Q-3 Write a program to make an object and


call it by call by reference.
class CallByRef
{

int a, b;

static void swap(CallByRef obj)

int c;

c=obj.a;

obj.a=obj.b;

obj.b=c;

System.out.println("value of a and b inside method call");

System.out.println("a="+obj.a+ " b="+obj.b)

public static void main(String args[])

CallByRef obj1=new CallByRef();

obj1.a=3;

obj1.b=5;

System.out.println("Value of a and b before method call");

System.out.println("a="+obj1.a+ " b="+obj1.b);

swap(obj1);

System.out.println("Value of a and b after method call");

System.out.println("a="+obj1.a+ " b="+obj1.b);

Q-4 Write a program to input values from user and do a


selection sort.
import java.util.*;
class SelectionSort

{ public static void main(String args[])

{ Scanner sc=new Scanner(System.in);

int n;

System.out.println("Enter size of array");

n=sc.nextInt();

int arr[]=new int[n];

int i,j, pos, temp, small;

System.out.println("Enter the elements");

for(i=0; i<n; i++)

arr[i]=sc.nextInt();

pos=i;

small=arr[pos];

for(j=i+1; j<n; j++)

if(arr[j]<small)

small=arr[j];

pos=j;

temp=arr[i];

arr[i]=small;

arr[pos]=temp;

} System.out.println("Sorted Array is");

for(i=0; i<n; i++)

{ System.out.print(arr[i]+" \t");

} }

Q-5 Write a program to enter values from user and


arrange them in ascending order using insertion sort.
class InsertionSort
{

public static void main(String args[])

int arr[]={3,0,1,2,7,4};

int i, j, key;

for(j=1; j<arr.length; j++)

key=arr[j];

i=j-1;

while(i>-1 && arr[i]>key)

arr[i+1]=arr[i];

i--;

arr[i+1]=key;

System.out.println("Sorted array is");

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

System.out.print(arr[i]+"\t");

Q-6 Write a program to matrix from user and give the


sum of both matrix by adding them.
class TwoDarray1
{ public static void main(String args[])

{ int mat1[][]={{3,5,1},{7,4,3},{0,3,1}};

int mat2[][]={{1,4,3},{0,2,5},{4,3,2}};

System.out.println("Matrix-1 is");

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

{ System.out.print(mat1[i][j]+" ");

System.out.println();

System.out.println("Matrix-2 is");

for(int i=0; i<3; i++)

for(int j=0; j<3; j++)

System.out.print(mat2[i][j]+" ");

System.out.println();

} System.out.println("Sum of two matrix is");

System.out.println("Matrix-2 is");

for(int i=0; i<3; i++)

{ for(int j=0; j<3; j++)

int c=mat1[i][j]+mat2[i][j];

System.out.print(c+" ");

c=0;

System.out.println();

Q-7 Write a program to enter a sentence and check


whether it has palindrome words or not.
import java.util.*;
class StringToken2

public static void main(String args[])

Scanner sc=new Scanner(System.in);

String str, s1;

System.out.println("Enter a sentence");

str=sc.nextLine();

StringBuffer s2;

StringTokenizer token1=new StringTokenizer(str);

while(token1.hasMoreTokens())

s1=token1.nextToken();

s2=new StringBuffer(s1);

s2=s2.reverse();

if(s1.equals(String.valueOf(s2)))

System.out.println(s1);

Q-8 Write a program to do a HCF recursion.


import java.util.*;
class HCFRecursion

int gcd(int p, int q)

if(q==0)

return (p);

else

return (gcd(q,p%q));

public static void main(String args[])

int m, n;

Scanner sc=new Scanner(System.in);

System.out.println("Enter two number");

m=sc.nextInt();

n=sc.nextInt();

HCFRecursion obj1=new HCFRecursion();

System.out.println("HCF of two nos="+obj1.gcd(m,n));

Q -9 Write a program to check whether the no entered


is palindromic prime or not.
import java.util.*;

class PalindromicPrime

public static void main(String args[])

Scanner sc=new Scanner(System.in);

int n;

for(n=10; n<=1000; n++)

int x=n;

int i, k=0, c=0, p;

for(i=1; i<=n; i++)

if(n%i==0)

c++;

while(x>0)

p=x%10;

k=k*10+p;

x/=10;

if(c==2 && k==n)

System.out.println(n+"\t");

Q-10 write a program to enter number from the


user and check whether it’s a happy no. or not.
class GFG {

static boolean isHappynumber(int n) {

if (n == 1 || n == 7)

return true;

int sum = n, x = n;

while(sum > 9) {

sum = 0;

while (x > 0) { int d = x%10;

sum += d*d;

x/=10;

if (sum == 1)

return true;

x = sum;

if(sum == 7)

return true;

return false;

public static void main(String[] args)

int n = 13;

if (isHappynumber(n))

System.out.println(n + " is a Happy number");

else

System.out.println(n +" is not a Happy number");

You might also like