Program To Find A Given Number Is Odd or Even: C Program Java Program
Program To Find A Given Number Is Odd or Even: C Program Java Program
Program To Find A Given Number Is Odd or Even: C Program Java Program
1
Program to check prime or not
C PROGRAM JAVA PROGRAM
#include<stdio.h> class PrimeExample{
main() public static void main(String args[]){
{ int i,m=0,flag=0;
int n, c = 2; int n=17;//it is the number to be checked
m=n/2;
printf("Enter a number to check if it is for(i=2;i<=m;i++){
prime\n"); if(n%i==0){
scanf("%d",&n); System.out.println("Number is not prime");
flag=1;
for ( c = 2 ; c <= n - 1 ; c++ ) break;
{ }
if ( n%c == 0 ) }
{ if(flag==0)
printf("%d is not prime.\n", n); System.out.println("Number is prime");
break; }
} }
}
if ( c == n )
printf("%d is prime.\n", n);
return 0;
}
2
Find the factorial of number WITHOUT RECURRSION
C PROGRAM JAVA PROGRAM
#include<stdio.h> class FactorialExample{
#include<conio.h> public static void main(String args[]){
void main(){ int i,fact=1;
int number=5;//It is the number to calculate
int i=1,f=1,num; factorial
printf("Enter a number: "); for(i=1;i<=number;i++){
scanf("%d",&num); fact=fact*i;
while(i<=num){ }
f=f*i; System.out.println("Factorial of "+number+"
i++; is: "+fact);
} }
printf("Factorial of %d is: %d",num,f); }
getch();
Sample output:
Enter a number: 5
Factorial of 5 is: 120
3
getch();
}
4
break; }
} if (counter ==2)
if ( c == i ) {
{ primeNumbers = primeNumbers + i + " ";
printf("%d\n",i); }
count++; }
} System.out.println("Prime numbers from 1
i++; to n are :");
} getch(); } System.out.println(primeNumbers);
}}
5
Program to reverse a string without inbuilt functions
C PROGRAM JAVA PROGRAM
#include<stdio.h> public class Reverse
{
#define MAX 10 // Maximum size of static int i,c=0,res;
string
static void stringreverse(String s)
/* Reverse the String using Recursion */ {
char* ReverseString(char *str) char ch[]=new char[s.length()];
{ for(i=0;i < s.length();i++)
static int i=0; ch[i]=s.charAt(i);
static char rev[MAX]; for(i=s.length()-1;i>=0;i--)
System.out.print(ch[i]);
if(*str) }
{
ReverseString(str+1); public static void main (String args[])
rev[i++] = *str; {
} System.out.println("Original String is : ");
System.out.println(" you all placed in IBM
return rev;// Return index of array every time ");
} Reverse.stringreverse(" you all placed in
IBM ");
/* Main method */ }
int main() }
{
char str[MAX];
gets(str);
return 0;
}
flag = 0;
}
}
6
Program to Check whether a given Character is present in a String
C PROGRAM JAVA PROGRAM
#include <stdio.h> import java.util.Scanner;
#include <string.h>
public class AsciiValue {
int main()
{ static public void main(String[] args) {
char a, word[50]; int answer = 0;
int i, freq = 0, flag = 0; String searchString = "You are placed in IBM";
char searchChar;
printf("Enter character: "); Scanner scan = new Scanner(System.in);
scanf("%c", &a); System.out.println("Enter a character to
printf("Now enter the word: "); search in a string : ");
scanf("%s", word); searchChar = scan.next().charAt(0);
printf("Positions of '%c' in %s are: ", a, word);
for (i = 0; i < strlen(word); i++) for (int i = 0; i < searchString.length(); i++){
{ if (searchString.charAt(i) == searchChar){
if (word[i] == a) answer += 1;
{ }
flag = 1; }
printf("%d ", i + 1);
freq++; if ( answer == 0 )
} System.out.println("Character not found ");
} else
if (flag) System.out.println("Characterfound");
{
printf("\nCharacter '%c' occured for %d
times.\n", a, freq); System.out.println("Number of occurences of
} " + args[0] + " is " + answer);
else
{ scan.close();
printf("None\n"); }
} }
return 0;
}
7
Swapping two variables WITHOUT USING 3rd variable
C PROGRAM JAVA PROGRAM
#include <stdio.h> import java.util.Scanner;
int main() class SwapNumbers
{ { public static void main(String args[])
int x = 10, y = 5; { int x, y;
System.out.println("Enter x and y");
// Code to swap 'x' and 'y' Scanner in = new Scanner(System.in);
x = x + y; // x now becomes 15 x = in.nextInt();
y = x - y; // y becomes 10 y = in.nextInt();
x = x - y; // x becomes 5 System.out.println("Before Swapping\nx =
"+x+"\ny = "+y);
printf("After Swapping: x = %d, y = %d", x, y); x = x + y;
y = x - y;
return 0; x = x - y;
} System.out.println("After Swapping\nx =
"+x+"\ny = "+y);
}
8
strcpy(b,a); int length = original.length();
strrev(b);
for(int i=length-1; i >= 0; i-- )
if (strcmp(a,b) == 0) reverse=reverse+original.charAt(i);
printf("Entered string is a palindrome.\n");
else if(original.equals(reverse))
printf("Entered string is not a
System.out.println("Entered string
palindrome.\n");
is a palindrome.");
getch(); else
}
System.out.println("Entered string
is not a palindrome.");
}}
Program to find the Armstrong Number
C PROGRAM JAVA PROGRAM
#include <stdio.h> class ArmstrongExample{
#include <conio.h> public static void main(String[]
args) {
int power(int, int); int c=0,a,temp;
int n=153;//It is the number to
int main() check armstrong
{ temp=n;
int n, sum = 0, temp, remainder, digits = 0; while(n>0)
{
a=n%10;
printf("Input an integer\n"); n=n/10;
scanf("%d", &n); c=c+(a*a*a);
}
temp = n; if(temp==c)
// Count number of digits System.out.println("armstrong
number");
while (temp != 0) { else
digits++; System.out.println("Not
temp = temp/10; armstrong number");
} }
temp = n; }
while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}
if (n == sum)
printf("%d is an Armstrong number.\n", n);
else
printf("%d is not an Armstrong number.\n",
n);
return 0;
}
9
for (c = 1; c <= r; c++)
p = p*n;
return p;
}
// Unsorted pair found (Equal values allowed) // Unsorted pair found (Equal values
if (arr[n-1] <= arr[n-2]) allowed)
return 0; if (arr[n-1] <= arr[n-2])
return 0;
// Last pair was sorted
// Keep on checking // Last pair was sorted
return arraySortedOrNot(arr, n-1); // Keep on checking
} return arraySortedOrNot(arr, n-1);
}
10