Monday, December 27, 2010

finding the biggest number from array

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for scannign n numbers and finding the biggest from them
*/

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


int
n = 0 , i =0 , j = 0;

int
no[100];
int
biggest; // for storing the biggest number
printf("How many numbers you want to scan ? ");

scanf("%d",&n);
if
(n < 0 || n>100)
{


// if the user enters invalid no of elemtns then we come here
printf("Invalid number. It must be brtween 1 to 100");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < n ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no[i]);
}



// assign biggest the first element's value
biggest = no[0];
// the logic for finding the biggest number
for(i = 0 ; i < n ;i++)
{

if
(no[i] > biggest)
{


biggest = no[i];
}
}


printf("The biggest number among those numbers is: %d" , biggest);

}


printf("\n");
}

sorting array in ascending order

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for scannign n numbers and sorting them in ascending order
*/

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


int
n = 0 , i =0 , j = 0;

int
no[100];
int
temp; // for swapping the number
printf("How many numbers you want to scan ? ");

scanf("%d",&n);
if
(n < 0 || n>100)
{


printf("Invalid number. It must be brtween 1 to 100");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < n ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no[i]);
}



// the logic for sorting the number
for(i = 0 ; i < n ;i++)
{

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


if
(no[i] < no[j])
{

// if the number at index i is greater than the number at index j then swap the numbers
temp = no[i];

no[i] = no[j];
no[j] = temp;
}
}
}


// logic for displaying the sorted array
for(i = 0 ; i < n ;i++)
{

printf("\nnumber at index %d is %d ", i , no[i]);
}
}


printf("\n");
}

sorting array in descending order

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for scannign n numbers and sorting them in descending order
*/

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


int
n = 0 , i =0 , j = 0;

int
no[100];
int
temp; // for swapping the number
printf("How many numbers you want to scan ? ");

scanf("%d",&n);
if
(n < 0 || n>100)
{


printf("Invalid number. It must be brtween 1 to 100");
}

else

{

// logic for scanning the numbers
for(i = 0 ; i < n ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no[i]);
}



// the logic for sorting the numbers
for(i = 0 ; i < n ;i++)
{

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


if
(no[i] > no[j])
{

// if the number at index i is greater than the number at index j then swap the numbers
temp = no[i];

no[i] = no[j];
no[j] = temp;
}
}
}


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

printf("\nnumber at index %d is %d ", i , no[i]);
}
}


printf("\n");
}

scan n numbers and display them (using array)

/****************************
* Umang B Bhatt *
* bhatt.umang7@gmail.com *
*****************************/

/**
* program for scannign n numbers and displaying them
*/

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


int
n = 0 , i =0 ;

int
no[100];
printf("How many numbers you want to scan ? ");
scanf("%d",&n);

if
(n < 0 || n>100)
{

printf("Invalid number. It must be brtween 1 to 100");
}


else

{

// logic for scanning the numbers
for(i = 0 ; i < n ;i++)
{


printf("Enter element %d: ", i);
scanf("%d",&no[i]);
}



// the logic for displaying the numbers
for(i = 0 ; i < n ;i++)
{


printf("\nnumber at index %d is %d ", i , no[i]);
}
}

printf("\n");
}

pattern

A program that will generate above pattern:








#include<stdio.h>
// this program works well when the value of rows is less than 20
void main()
{


int
n = 5;
int
i = 0 , j = 0 , k = 0;

int
temp = 0 ;
printf("Enter no of rows: ");

scanf("%d",&n);
temp = n;
for
(i = 0; i<n ;i++)
{


// logic for space
for(k = 0 ; k < temp ; k++ )
{


printf(" ");
}

temp--;
// for printing the actuak number
for(j = 0; j <= i ; j++)
{


printf("%3d ", fun(i,j,n));
}

printf("\n");
}
}


int
fun(int a , int b, int n )
{


int
sum = 0 ;
int
i = 0 , j = 0;

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


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


sum++;
if
( i==a && b==j )
{


return
sum;
}
}
}

return
sum;
}

pattern

Question: Write a program that generates output shown in left image.


The code is:











#include<stdio.h>
// this program works well when the value of rows is less than 20
void main()
{


int
n = 5;
int
i = 0 , j = 0;

printf("Enter no of rows: ");
scanf("%d",&n);
for
(i = 0; i<n ;i++)
{


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


printf("%3d ", fun(i,j));
}

printf("\n");
}
}


int
fun(int a , int b )
{

int
sum = 0 ;

int
i = 0 , j = 0;

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


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


sum++;
if
( i==a && b==j )
{


return
sum;
}
}
}

return
sum;
}

Wednesday, November 3, 2010

1234 -> 4123 -> 3412number pattern

/*

Write a program that generated output something like this (here 1234 is input).
Here, keep all the numbers as integers only. Do not convert them to string or character:
1234
2341
3412
4123

note: here 0 will not be inputted at ay place
*/


import java.util.*;

public class
a
{


static
int length(int n)
{


int
len = 0;
while
(n > 0)
{


len++;
n /= 10;
}

return
len;
}


static
int generate(int n)
{

int
temp = (int) Math.pow(10, length(n) - 1);

int
temp2 = n / (int) Math.pow(10, length(n) - 1);

temp = n % temp;
temp = temp * 10;

temp = temp + temp2;
return
temp;
}


public static
void main(String a[])
{

int
n;

Scanner s = new Scanner(System.in);
n = s.nextInt();

int
temp = n;
/* n-1 if you do not want to include the original digit and
n if you want to include original digit
*/

for
(int i = 0; i < length(n) - 1; i++)
{


temp = generate(temp);
System.out.println(temp);
}

}
}