Program To Find A Given Number Is Odd or Even: C Program Java Program

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Program to find a given number is odd or even

C PROGRAM JAVA PROGRAM


#include <stdio.h> import java.util.Scanner;
#include <conio.h>
void main(){ class OddOrEven
int number; {
clrscr(); public static void main(String args[])
printf("Enter a number"); {
scanf("%d",&number); int x;
if ( number % 2 == 0 ) System.out.println("Enter an integer to
printf("You entered an even number."); check if it is odd or even ");
else Scanner in = new Scanner(System.in);
printf("You entered an odd number."); x = in.nextInt();
getch();
} if ( x % 2 == 0 )
System.out.println("You entered an even
number.");
else
System.out.println("You entered an odd
number.");
}
}

Print ASCII value of a character


C PROGRAM JAVA PROGRAM
#include <stdio.h> import java.util.*;
int main()
{ class AsciiValue {
char c; public static void main(String[] args) {
printf("Enter a character: ");
char asciiChar;
// Reads character input from the user Scanner scan = new Scanner(System.in);
scanf("%c", &c);
//Getting character from user
// %d displays the integer value of a asciiChar = scan.next().charAt(0);
character
// %c displays the actual character int ascii = (int) asciiChar;
System.out.println(ascii);
printf("ASCII value of %c = %d", c, c); scan.close();
return 0; }
} }

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;
}

Find the factorial of number using recurrsion


C PROGRAM JAVA PROGRAM
#include <stdio.h> class FactorialExample2{
#include<conio.h> static int factorial(int n){
if (n == 0)
long int multiplyNumbers(int n); return 1;
else
int main() return(n * factorial(n-1));
{ }
int n; public static void main(String args[]){
printf("Enter a positive integer: "); int i,fact=1;
scanf("%d", &n); int number=4;//It is the number to calculate
printf("Factorial of %d = %ld", n, factorial
multiplyNumbers(n)); fact = factorial(number);
return 0; System.out.println("Factorial of "+number+"
} is: "+fact);
}
long int multiplyNumbers(int n) }
{
if (n >= 1)
return n*multiplyNumbers(n-1);
else
return 1;
}

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

Fibonacci Series Fibonacci


Series c
language
C PROGRAM JAVA PROGRAM
#include<stdio.h> class FibonacciExample1{
#include<conio.h> public static void main(String args[])
void main() {
{ int n1=0,n2=1,n3,i,count=10;
int n, first = 0, second = 1, next, c; System.out.print(n1+" "+n2);//printing 0
and 1
printf("Enter the number of terms\n");
scanf("%d",&n); for(i=2;i<count;++i)//loop starts from 2
because 0 and 1 are already printed
printf("First %d terms of Fibonacci {
series are :-\n",n); n3=n1+n2;
System.out.print(" "+n3);
for ( c = 0 ; c < n ; c++ ) n1=n2;
{ n2=n3;
if ( c <= 1 ) }
next = c;
else }
{ }
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}

3
getch();
}

Program to find the palindrome of a given Number Fibonacci


Series c
language
C PROGRAM JAVA PROGRAM
#include <stdio.h> class PalindromeExample{
#include <conio.h> public static void main(String args[]){
void main() int r,sum=0,temp;
{ int n, reverse = 0, temp; int n=454;//It is the number variable to be
printf("Enter a number to check if it is checked for palindrome
a palindrome or not\n");
scanf("%d",&n); temp=n;
temp = n; while(n>0){
while (temp != 0) r=n%10; //getting remainder
{ sum=(sum*10)+r;
reverse = reverse * 10; n=n/10;
reverse = reverse + temp%10; }
temp = temp/10; if(temp==sum)
} System.out.println("palindrome number
if (n == reverse) ");
printf("%d is a palindrome else
number.\n", n); System.out.println("not palindrome");
else }
printf("%d is not a palindrome }
number.\n", n);
getch();
}

Program to run the series of prime numbers Fibonacci


Series c
language
C PROGRAM JAVA PROGRAM
#include<stdio.h> import java.util.Scanner;
#include<conio.h> class PrimeNumbers2
void main() { public static void main (String[] args)
{ { Scanner scanner = new
int n, i = 3, count, c; Scanner(System.in);
printf("Enter the number of prime int i =0;
numbers required\n"); int num =0;
scanf("%d",&n); String primeNumbers = "";
if ( n >= 1 ) { System.out.println("Enter the value of
printf("First %d prime numbers are n:");
:\n",n); int n = scanner.nextInt();
printf("2\n");} for (i = 1; i <= n; i++)
for ( count = 2 ; count <= n ; ) { int counter=0;
{ for ( c = 2 ; c <= i - 1 ; c++ ) for(num =i; num>=1; num--)
{ { if(i%num==0)
if ( i%c == 0 ) counter = counter + 1;

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);
}}

Find the Highest Frequency Character in a String


C PROGRAM JAVA PROGRAM
#include <stdio.h> public class GFG
#include <string.h> {
static final int ASCII_SIZE = 256;
static char getMaxOccuringChar(String
char string1[100], visited[100]; str)
int count[100] = {0}, flag = 0; {
int count[] = new
void main() int[ASCII_SIZE];
int len = str.length();
{
for (int i=0; i<len; i++)
int i, j = 0, k = 0, l, max, index; count[str.charAt(i)]++;
int max = -1;
printf("Enter a string : "); char result = ' ';
scanf("%[^\n]s", string1); for (int i = 0; i < len; i++) {
if (max < count[str.charAt(i)]) {
max = count[str.charAt(i)];
l = strlen(string1); result = str.charAt(i);
}
for (i = 0; i < l; i++) }
{ return result;
if (i == 0) }
{ public static void main(String[]
args)
visited[j++] = string1[i];
{
count[j - 1]++; String str = "sample string";
} System.out.println("Max occurring
else character is "
{ + getMaxOccuringChar(str));
for (k = 0; k < j; k++) }
}
{
if (string1[i] == visited[k])
{
count[k]++;
flag = 1;
}
}
if (flag == 0)
{
visited[j++] = string1[i];
count[j - 1]++;
}

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);

printf (" %s \n", ReverseString(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);
}

Program to find the palindrome of a given Word


C PROGRAM JAVA PROGRAM
#include <stdio.h> import java.util.*;
#include <string.h>
class Palindrome
#include<conio.h> {
public static void main(String
void main() args[])
{ {
char a[100], b[100]; String original, reverse = "";
Scanner in = new
Scanner(System.in);
printf("Enter the string to check if it is a
palindrome\n"); System.out.println("Enter a string
scanf("%s",&a); to check if it is a palindrome");
original = in.nextLine();

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;
}

int power(int n, int r) {


int c, p = 1;

9
for (c = 1; c <= r; c++)
p = p*n;

return p;
}

Recursive approach to check if an Array is sorted or not without inbuilt function


C PROGRAM JAVA PROGRAM
class CheckSorted
#include<stdio.h> {
#include <conio.h> // Function that returns 0 if a pair
// is found unsorted
int arraySortedOrNot(int arr[], int n) static int arraySortedOrNot(int arr[], int n)
{ {
// Array has one or no element or the // Array has one or no element or the
// rest are already checked and approved. // rest are already checked and approved.
if (n == 1 || n == 0) if (n == 1 || n == 0)
return 1; return 1;

// 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);
}

int main() // main function


{ public static void main (String[] args)
int arr[] = {20, 23, 23, 45, 78, 88}; {
int n = sizeof(arr)/sizeof(arr[0]); int arr[] = {20, 23, 23, 45, 78, 88};
if (arraySortedOrNot(arr, n)) int n = arr.length;
printf("Yes\n"); if (arraySortedOrNot(arr, n)!=0)
else System.out.println("Yes");
printf("No\n"); else
} System.out.println("No");
}
}

10

You might also like