0% found this document useful (0 votes)
53 views22 pages

First Program: #Include

The document contains 14 programs written in C and Java. The programs demonstrate different patterns that can be printed using loops, including pyramid, diamond, heart and hollow shapes made from asterisks. Also included are examples of binary search and linear search algorithms to find a target value within a sorted and unsorted array respectively.

Uploaded by

Vinod Malik
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)
53 views22 pages

First Program: #Include

The document contains 14 programs written in C and Java. The programs demonstrate different patterns that can be printed using loops, including pyramid, diamond, heart and hollow shapes made from asterisks. Also included are examples of binary search and linear search algorithms to find a target value within a sorted and unsorted array respectively.

Uploaded by

Vinod Malik
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/ 22

First program

*
***
*****
*******
*********

#include <stdio.h>

int main()
{
int row, c, n, temp;

printf("Enter the number of rows in pyramid of stars you


wish to see ");
scanf("%d",&n);

temp = n;

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


{
for ( c = 1 ; c < temp ; c++ )
printf(" ");

temp--;

for ( c = 1 ; c <= 2*row - 1 ; c++ )


printf("*");

printf("\n");
}

return 0;
}

Second Program

1
232
34543
4567654
567898765

#include<stdio.h>
main()
{
int n, c, d, num = 1, space;

scanf("%d",&n);

space = n - 1;

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


{
num = d;

for ( c = 1 ; c <= space ; c++ )


printf(" ");

space--;

for ( c = 1 ; c <= d ; c++ )


{
printf("%d", num);
num++;
}
num--;
num--;
for ( c = 1 ; c < d ; c++)
{
printf("%d", num);
num--;
}
printf("\n");

return 0;
}

Third Program

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

#include <stdio.h>

#include <conio.h>
void main() {

int i,j;

clrscr();

for (i=0; i<5; i++) {

for (j=0; j<5; j++) {

printf(" * ");

printf("\n");

getch();

Forth Program

*
* *
* * *
* * * *
* * * * *

#include <stdio.h>

#include <conio.h>

void main()

int i,j,k;

clrscr();

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

{
for (j=5; j>=i; j--)

printf(" ");

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

printf("*");

printf("\n");

getch();

Fifth Program

* * * * *
* * * *
* * *
* *
*

#include <stdio.h>

#include <conio.h>

void main() {

int i,j,k,samp=1;

clrscr();

for (i=5; i>=1; i--) {

for (k=samp; k>=0; k--) {

printf(" ");

// only 1 space
}

for (j=i; j>=1; j--) {

printf("*");

samp = samp + 1;

printf("\n");

getch();

Sixth Program

* * * * *
* * * *
* * *
* *
*
#include <stdio.h>
#include <conio.h>
void main() {
int i,j;
clrscr();
for (i=5; i>=1; i--) {
for (j=1; j<=i; j++) {
printf(" * ");
}
printf("\n");
}
getch();
}

Seventh Program

*
* *
* * *
* * * *
* * * * *
#include <stdio.h>

#include <conio.h>

void main() {

int i,j,k,t=0;

clrscr();

for (i=1; i<=5; i++) {

for (k=t; k<5; k++) {

printf(" ");

for (j=0; j< i; j++) {

printf(" * ");

t = t + 1;

printf("\n");

getch();

Eighth Program

*
**
* **
* * **
* * * **
* * **
* **
**
*
#include <stdio.h>
#include <conio.h>
void main() {
int i,j,k,samp=1;
clrscr();
for (i=1; i<=5; i++) {
for (k=samp; k<=5; k++) {
printf(" ");
}
for (j=0; j< i; j++) {
printf("*");
}
samp = samp + 1;
printf("\n");
}
samp = 1;
for (i=4; i>=1; i--) {
for (k=samp; k>=0; k--) {
printf(" ");
}
for (j=i; j>=1; j--) {
printf("*");
}
samp = samp + 1;
printf("\n");
}
getch();
}

Ninth Program

0
1 0 1
2 1 0 1 2
3 2 1 0 1 2 3
4 3 2 1 0 1 2 3 4
5 4 3 2 1 0 1 2 3 4 5

#include <stdio.h>
#include <conio.h>
void main() {
int no,i,y,x=35;
clrscr();
printf("Enter number of rows: ");
scanf("%d," &no);
for (y=0;y<=no;y++) {
goto(x,y+1);
for (i=0-y; i<=y; i++) {
printf(" %3d ", abs(i));
x=x-3;
}
}
getch();
}

Tenth Program

*
***
*****
*******
*********
*******
*****
***
*
#include <stdio.h>

int main()

int i, j, n;

printf("Enter value of n : ");

scanf("%d", &n);

//Prints the upper triangle

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

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

printf(" ");

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


{

printf("*");

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

printf("*");

printf("\n");

//Prints the lower triangle

for(i=n; i>=1; i--)

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

printf(" ");

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

printf("*");

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

printf("*");

printf("\n");

}
return 0;

Eleventh Program

Write a C program to print hollow diamond star pattern using for loop.
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********

#include <stdio.h>

int main()

int i, j, n;

printf("Enter value of n : ");

scanf("%d", &n);

//Loop for printing upper half part of the pattern

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

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

printf("*");

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

printf(" ");

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

printf(" ");

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

printf("*");

printf("\n");

//Loop for printing lower half part of the pattern

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

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

printf("*");

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

printf(" ");

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


{

printf(" ");

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

printf("*");

printf("\n");

return 0;

Twelfth Program

Write a C program to print heart star pattern using for loop. The pattern should look like

***** *****
******* *******
********* *********
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
#include <stdio.h>

int main()

int i, j, n;

printf("Enter value of n : ");

scanf("%d", &n);

for(i=n/2; i<=n; i+=2)

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

printf(" ");

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

printf("*");

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

printf(" ");

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


{

printf("*");

printf("\n");

for(i=n; i>=1; i--)

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

printf(" ");

for(j=1; j<=(i*2)-1; j++)

printf("*");

printf("\n");

return 0;

Thirteen Program (Binary Search)

import java.util.Scanner;

class BinarySearch
{
public static void main(String args[])
{
int c, first, last, middle, n, search, array[];

Scanner in = new Scanner(System.in);


System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];

System.out.println("Enter " + n + " integers");

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


array[c] = in.nextInt();

System.out.println("Enter value to find");


search = in.nextInt();

first = 0;
last = n - 1;
middle = (first + last)/2;

while( first <= last )


{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
System.out.println(search + " found at location " +
(middle + 1) + ".");
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if ( first > last )
System.out.println(search + " is not present in the
list.\n");
}
}
Fourteen Program (Linear Search)

import java.util.Scanner;

class LinearSearch
{
public static void main(String args[])
{
int c, n, search, array[];

Scanner in = new Scanner(System.in);


System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];

System.out.println("Enter " + n + " integers");

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


array[c] = in.nextInt();

System.out.println("Enter value to find");


search = in.nextInt();

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


{
if (array[c] == search) /* Searching element is
present */
{
System.out.println(search + " is present at location
" + (c + 1) + ".");
break;
}
}
if (c == n) /* Searching element is absent */
System.out.println(search + " is not present in
array.");
}
}
Fifteen Program (Bubble Sort)

import java.util.Scanner;

class BubbleSort {
public static void main(String []args) {
int n, c, d, swap;
Scanner in = new Scanner(System.in);

System.out.println("Input number of integers to sort");


n = in.nextInt();

int array[] = new int[n];

System.out.println("Enter " + n + " integers");

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


array[c] = in.nextInt();

for (c = 0; c < ( n - 1 ); c++) {


for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d+1]) /* For descending order use
< */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

System.out.println("Sorted list of numbers");

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


System.out.println(array[c]);
}
}
Sixteen Program (Find all substring of a String)

import java.util.Scanner;

class SubstringsOfAString
{
public static void main(String args[])
{
String string, sub;
int i, c, length;

Scanner in = new Scanner(System.in);


System.out.println("Enter a string to print it's all
substrings");
string = in.nextLine();

length = string.length();

System.out.println("Substrings of \""+string+"\"
are :-");

for( c = 0 ; c < length ; c++ )


{
for( i = 1 ; i <= length - c ; i++ )
{
sub = string.substring(c, c+i);
System.out.println(sub);
}
}
}
}
Seventeen Program ( Add two Matrics )

import java.util.Scanner;

class AddTwoMatrix
{
public static void main(String args[])
{
int m, n, c, d;
Scanner in = new Scanner(System.in);

System.out.println("Enter the number of rows and columns


of matrix");
m = in.nextInt();
n = in.nextInt();

int first[][] = new int[m][n];


int second[][] = new int[m][n];
int sum[][] = new int[m][n];

System.out.println("Enter the elements of first


matrix");

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
first[c][d] = in.nextInt();
System.out.println("Enter the elements of second
matrix");

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
second[c][d] = in.nextInt();

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];
//replace '+' with '-' to subtract matrices

System.out.println("Sum of entered matrices:-");

for ( c = 0 ; c < m ; c++ )


{
for ( d = 0 ; d < n ; d++ )
System.out.print(sum[c][d]+"\t");

System.out.println();
}
}
}

Eighteen Program (Selection Sort)

import java.util.Scanner;

public class SelectionSort

public static void sort( int arr[] ){

int N = arr.length;

int i, j, pos, temp;

for (i = 0; i < N-1; i++)

pos = i;

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

{
if (arr[j] < arr[pos])

pos = j;

temp = arr[i];

arr[i] = arr[pos];

arr[pos]= temp;

public static void main(String[] args)

Scanner scan = new Scanner( System.in );

System.out.println("Selection Sort Test\n");

int n, i;

System.out.println("Enter number of integer elements");

n = scan.nextInt();

int arr[] = new int[ n ];

System.out.println("\nEnter "+ n +" integer elements");

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

arr[i] = scan.nextInt();

sort(arr);

System.out.println("\nElements after sorting ");

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

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

System.out.println();

}
}

You might also like