c program for number of times while loop condition is tested

#include
int main()
{
int i=0,j=0;
while (i < printf("%dhello\n",i+1))
i++;

do
j++;
while (j <= printf("%dhello\n",j));

}

output:

1hello
2hello
3hello
4hello
5hello
6hello
7hello
8hello
1hello
2hello
3hello
4hello
5hello
6hello
7hello
8hello

Advertisements

C program for printing the kth element in the given array after sorting

#include

int main()
{
int arr[10], len, i, j, temp, n;
printf(“Enter the size of array”);
scanf(“%d”, &len);
printf(“\n Enter the array elements”);
for (i = 0; i < len; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < len; i++)
{
for (j = 0; j arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j+1] = temp;
}
}
}
printf(“The new array is:”);
for (i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
printf("\n Enter Which kth Number You want");
scanf("%d", &n);
printf("\n The %d th kth number is: %d", n, arr[n – 1]);
return 0;
}

Task3 Array copying into another Array

public class Copy1Array2another {

/**
* @param args
*/
public static void main(String[] args) {

int[] oldArray={1,3,4}, newArray;
// Let’s pretend we filled up oldArray and initialized newArray here
// Now, copy them over
newArray=new int[2];
System.arraycopy(oldArray, 0, newArray, 0, 2);
System.out.println(“displaying old array”);
for(int i=0;i<3;i++)
{
System.out.println(oldArray[i]);
}
System.out.println(“displaying new array”);
for(int i=0;i<2;i++)
{
System.out.println(newArray[i]);
}
}

}

Task2 Remove an element with in an Array

import java.util.*;

public class ArrayEx {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] intArray = new int[] {1, 6, 0, 4, 2, 8, 7, 5, 9, 3};

int[] arrayCopy = new int[intArray.length];
System.out.println(“entered”);
System.arraycopy(intArray, 0, arrayCopy, 0,intArray.length);
Arrays.sort(arrayCopy);
for (int i = 0; i < 4; i++)
System.out.println(arrayCopy[i]);
int[] x=Arrays.copyOf(arrayCopy, 4);
for (int i = 0; i < 4; i++)
System.out.println(“x=”+x[i]);
// String s=”hai”;
StringBuffer buf=new StringBuffer(“raju”);
String rev=buf.reverse().toString();
String yourString=”hi hello hi hi”,naga;
System.out.println(yourString);
naga = yourString.replaceAll(“\\s+”, ” “);
System.out.println(naga);
System.out.println(rev);
}

}

printing the specified format of numbers

              1                                  1
              2    3                             2   2
              4    5    6                       3    3    3
Condition: without using inner loops
part1:
#include<stdio.h>
void sum(int k)
{
    printf(“%d”,k);
}
int main()
{
    int i,j;
    for(i=1;i<=3;i++)
    {
      j=1;
      a:
        if(j++<=i)
        {
           // printf(“hi”);
            sum(i);
            goto a;
        }
        printf(“\n”);
    }
    return 0;
}
Part2-
#include<stdio.h>
void sum(int k)
{
    printf(“%d”,k);
}
int main()
{
    int i,j,l=1;
    for(i=1;i<=3;i++)
    {
      j=1;
      a:
        if(j++<=i)
        {
           // printf(“hi”);
            sum(l);
        l++;
            goto a;
        }
        printf(“\n”);
    }
    return 0;
}