C program to find prime numbers between given range


#include<stdio.h>
void main()
{
int i, prime, upper, lower, n;

printf("\n\n\t ENTER THE LOWER LIMIT => ");
scanf("%d", &lower);
printf("\n\n\t ENTER THE UPPER LIMIT => ");
scanf("%d", &upper);
printf("\n\n\t PRIME NUMBERS ARE");
for(n=lower+1; n<upper; n++)
{
prime = 1;
for(i=2; i<n; i++)
if(n%i == 0)
{
prime = 0;
break;
}
if(prime)
printf("\n\n\t\t\t%d", n);
}
}

Output :-

ENTER THE LOWER LIMIT => 0
ENTER THE UPPER LIMIT =>  10

PRIME NUMBERS ARE

1

2

3

5

7

2 comments :

code to check whether a no is Armstrong no or not



#include<stdio.h>
int main(){
    int num,r,sum=0,temp;

    printf("Enter a number: ");
    scanf("%d",&num);

    temp=num;
    while(num!=0){
         r=num%10;
         num=num/10;
         sum=sum+(r*r*r);
    }
    if(sum==temp)
         printf("%d  It is an Armstrong number",temp);
    else
         printf("%d It is not an Armstrong number",temp);
    return 0;
}

Output:-

Enter a number: 153
It is an Armstrong number

0 comments :

Finding duplicate element from 1-100 numbers JAVA



import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class FindDuplicateNumber {

public static void main(String args[]){
int a[]={1,2,3,4,5,6,7,8,9,5};

System.out.println("Using arrays =>"+usingArrayDup(a));
}

// Finding duplicates using double arrays
private static int usingArrayDup(int[] a) {
//Finding duplicate number using two arrays
for(int i=0;i<a.length;i++){
for(int j=i+1;j<a.length;j++){
if(a[i]==a[j])
return a[i];
}
}
return 0;
}//Double arrays
}

Output:-

Using arrays =>5

2 comments :

Find missing number from array 0-100



public class MissingNumberInArray {

public static void main(String args[]){

int a[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
int asum = 0,sum = 0;
for(int i=1;i<=a.length;i++)
sum=sum+i;
for(int i=0;i<a.length;i++)
asum=asum+a[i];
int missnum=sum-asum;
System.out.println("Missing Number"+missnum);
}
}

Output:-

Missing Number =>20

0 comments :

find out longest palindrome in a given string JAVA


public class LongestPalindrom {
    public static void main(String args[]) {
        String str = " hello . you uoy nitin nitin era woh..";
        char[] a = str.toCharArray();
        int low = Integer.MAX_VALUE;
        int upper = Integer.MIN_VALUE;
        for (int i = 0; i < str.length(); i++) {
            int start = i;
            int end = i;
            while (start >= 0 && end < str.length()) {
                if (a[start] == a[end]) {
                    if (end - start > upper - low) {
                        upper = end;
                        low = start;
                    }
                    end++;
                    start--;
                } else {
                    break;
                }

            }

        }
        for (int i = 0; i < str.length(); i++) {
            int start = i;
            int end = i + 1;
            while (start >= 0 && end < str.length()) {
                if (a[start] == a[end]) {
                    if (end - start > upper - low) {
                        upper = end;
                        low = start;
                    }
                    end++;
                    start--;
                } else {
                    break;
                }
            }

        }
        for (int i = low; i <= upper; i++) {
            System.out.print(a[i]);
        }
    }
}

Output:-

LongestPalindrom
nitin nitin

0 comments :

Find all permutations of String using Recursion


# include <stdio.h>
# include <string.h>

/* Function to swap values at two pointers */
void swap (char *x, char *y)
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
 
/* Function to print permutations of string */
void permute(char *a, int i, int n)
{
   int j;
   if (i == n)
     printf("%s\n", a);
   else
   {
        for (j = i; j <= n; j++)
       {
          swap((a+i), (a+j));
          permute(a, i+1, n);
          swap((a+i), (a+j)); //backtrack
       }
   }
}

int main()
{
   char a[] = "ABC";
   int l=strlen(a);
   printf("String length => %d",l);
   permute(a, 0, l-1);
   getchar();
   return 0;
}

Output:-

String length => 3
ABC
ACB
BAC
BCA
CBA
CAB

0 comments :

Find permutations using Recursion


#include <stdio.h>

void print(const int *v, const int size)
{
  if (v != 0)   {
     int i;
    for(i = 0; i < size; i++)     {
      printf("%d", v[i] );
    }
    printf("\n");
  }
} // print

void visit(int *Value, int N, int k)
{
  static level = -1;
  int i;
  level = level+1; Value[k] = level;

  if (level == N)
    print(Value, N);
  else
   
    for(i = 0; i < N; i++)
      if (Value[i] == 0)
        visit(Value, N, i);

  level = level-1; Value[k] = 0;
}

main()
{
  const int N = 4;
  int Value[N];
  int i;
  for (i = 0; i < N; i++)
  {
    Value[i] = 0;
  }
  visit(Value, N, 0);
}

Output:-

1234
1243
1324
1423
1342
1432
2134
2143
3124
4123
3142
4132
2314
2413
3214
4213
3412
4312
2341
2431
3241
4231
3421
4321

0 comments :

Remove any given character from a String

public class RemoveChar4mString {

    public static void main (String[] args)
    {
        System.out.println (removeChar4mString ("My blog name is Sourcecode-kk",  'o'));
    }

    public static String removeChar4mString (String str, char charToBeRemoved)
    {
        if (str == null)
            return "";

        StringBuilder strBuild = new StringBuilder ();

        for (int i = 0; i < str.length (); i++)
        {
            char ch = str.charAt (i);
            if (ch == charToBeRemoved)
                continue;
            strBuild.append (ch);
        }
        return strBuild.toString ();
    }
}

output:-

My blg name is Surcecde-kk

this code removes all "o" character from given string "My Blog name is Sourcecode-kk"


0 comments :

convert number to words in php



<?php

$one= array(
        0                   => ' ',
        1                   => 'one',
        2                   => 'two',
        3                   => 'three',
        4                   => 'four',
        5                   => 'five',
        6                   => 'six',
        7                   => 'seven',
        8                   => 'eight',
        9                   => 'nine',
        10                  => 'ten',
        11                  => 'eleven',
        12                  => 'twelve',
        13                  => 'thirteen',
        14                  => 'fourteen',
        15                  => 'fifteen',
        16                  => 'sixteen',
        17                  => 'seventeen',
        18                  => 'eighteen',
        19                  => 'nineteen'
        );
       
    $ten = array (
        0    =>    '',
        1    =>    '',
        2    =>    'twenty',
        3    =>    'thirty',
        4    =>    'fourty',
        5    =>    'fifty',
        6    =>    'sixty',
        7    =>    'seventy',
        8    =>    'eighty',
        9    =>    'ninety'
        );
   
function num2word($n)
{
    echo "Enter no between 1 to 999999999 \n";
    if($n<=0 && $n>999999999)
        echo "please enter valid no";
    else
 {
                  pw((($n/10000000)%100)," crore ");
                  pw((($n/100000)%100)," lakh ");
                  pw((($n/1000)%100)," thousand ");
                  pw((($n/100)%10)," hundred ");
                  pw(($n%100)," ");
 }   
}       

function pw($n,$ch)
{
 global $one;
 global $ten;

 echo ($n>19) ? ($ten[$n/10]." ".$one[$n%10]) : $one[$n];
 if($n) echo $ch;
 }

echo "\n". num2word(123456789);       
echo "\n". num2word(123477);
echo "\n". num2word(1000);
echo "\n". num2word(999);

?>       

Output :-

twelve crore thirty four lakhs fifty six thousand seven hundreds eighty nine

one lakhs twenty three thousand four hundreds seventy seven

one thousand

nine hundreds ninety nine

0 comments :