Posts

Showing posts from 2019

C program to find the frequency of element in an array

Image
/* C program to find the frequency of each element of array */ #include <stdio.h> int main() {     int arr[100], freq[100];     int size, i, j, count;     /* Input size of array */     printf("\nEnter size of array: ");     scanf("%d", &size);     /* Input elements in array */     printf("\nEnter elements in array: ");     for(i=0; i<size; i++)         {         scanf("%d", &arr[i]);         /* Initially initialize frequencies to -1 */         freq[i] = -1;         }     for(i=0; i<size; i++)     {     count = 1;         for(j=i+1; j<size; j++)         {         /* If duplicate element is found */             if(arr[i]==arr[j])             {                 count++;                 /* Make sure not to count frequency of same element again */                 freq[j] = 0;             }         }     /* If frequency of current element is not counted */     if(freq[i] != 0)         {             freq[i] = count;         }

C program that contains a string XOR each character in this string with 0 ,127

Write a C program that contains a string (char pointer) with a value \Hello World’. The program should XOR each character in this string with 0 and displays the result. Program: #include<stdlib.h> main() { char str[]="Hello World"; char str1[11]; int i,len; len=strlen(str); for(i=0;i<len;i++) { str1[i]=str[i]^0; printf("%c",str1[i]); } printf("\n"); } Q.2 Write a C program that contains a string (char pointer) with a value \Hello World’. The program should AND or and XOR each character in this string with 127 and display the result. PROGRAM: #include <stdio.h> #include<stdlib.h> void main() { char str[]="Hello World"; char str1[11]; char str2[11]=str[]; int i,len; len = strlen(str); for(i=0;i<len;i++) { str1[i] = str[i]&127; printf("%c",str1[i]); } printf("\n"); for(i=0;i<len;i++) { str3[i] = str2[i]^127; printf("%c",str3[i]); } pri

JAVA Program for Twin Prime Number

Image
Twin Prime Number A twin prime is a prime number that is either 2 less or 2 more than another prime number.  Example- (5,7),  (11,13),  (17,19). In other words, a twin prime is a prime that has a prime gap of two. Sometimes the term twin prime is used for a pair of twin primes; an alternative name for this is prime twin or prime pair. Java Program : class TwinPrimes {     public static void main(String args[]) {         String primeNo = "";         int j = 0;         int LastPrime = 1;         System.out.println("Twin Primes are:");         for (int i = 1; i < 100; i++) {             for (j = 2; j < i; j++) {                if (i % j == 0) {                break;                }             }             if (i == j) {                primeNo += i + " ";                if ((i - LastPrime) == 2) {                System.out.println("("+(i-2)+","+i+")");                }                LastPrime = i;             }

Python Program to check two String are Anagram or not

Anagram An anagram is a word or phrase that's formed by rearranging the letters of another word or phrase. For example, the letters that make up “A decimal point” can be turned into the anagram “I'm a dot in place.” ... “Dormitory” turns into the anagram “dirty room,” Examples of anagram "restful" = "fluster" "funeral" = "real fun" "adultery" = "true lady" "customers" = "store scum" "forty five" = "over fifty" Python Program for Anagram String s1=input("Enter the First String: ") s2=input("Enter the Second String: ") def anagramCheck(s1, s2):           if(sorted(s1)== sorted(s2)):         print("The strings are anagrams.")      else:         print("The strings aren't anagrams.") anagramCheck(s1,s2)

Display Browser information using javascript

Q. Write a program using Java script for Web Page to display browsers information. Ans. <!DOCTYPE> <html> <head>     <title>Browsers information</title>   <script language=javascript>   function showBrowserinfo()   {     document.write("<b>Web Page to display browsers information</b> <br><br>");     document.write("Name "+navigator.appName+"<br>");     document.write("Version "+navigator.appVersion  +"<br>");     document.write("Codename " +navigator.appCodeName  +"<br>");     document.write("Cookie enable"+navigator.cookieEnabled  +"<br>");     document.write("Java Enable"+navigator.javaEnabled  +"<br>");     document.write("Mime type"+navigator.mimeTypes  +"<br>");     document.write("Platform"+navigator.platform  +"<br>");    

Armstrong Number in Javascript

Image
Q. Write a program in javascript to check given number is Armstrong or not ?  Ans.  An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 153 is an Armstrong number example -  153 = 1**3 + 5**3 +  3**3 = 1+125+27 => 153 Armsrong Number program in javascript <!doctype html> <html> <head> <script> function armstr() { var arm=0,a,b,c,d,num; num=Number(document.getElementById("no_input").value); temp=num; while(temp>0) { a=temp%10; temp=parseInt(temp/10); // convert float into Integer arm=arm+a*a*a; } if(arm==num) { alert("Armstrong number"); } else { alert("Not Armstrong number"); } } </script> </head> <body> Enter any Number: <input id="no_input"> <button onclick="armstr()">Check</button></br></br> </body> </html>

Change String into Lowercase

C program for Change String into Lowercase #include<stdio.h> #include<conio.h> void low_str (char[]); int main() {   char string[100];   printf("Enter a string to convert it into Lower case :   ");   gets(string);   low_str(string);   printf("Entered String  converted into lower case is: \"%s\"\n", string);   return 0; } //function for string convert into lower string void low_str(char s[]) {   int c=0;   while(s[c]!='\0')   {     if(s[c]>='A' && s[c]<='Z')     {       s[c]=s[c]+32;     }   c++;   } }

Factor of a number

C Program to find the factor of a number   #include<stdio.h> int main() {     int  n, i;     printf("\nEnter the number to find the factors of :  ");     scanf("%d",&n);     printf("\n\n\nFactors of %d are: \n", n);     for(i = 1; i <= n/2; i++)     {         if(n%i == 0)             printf("\t\t%d\n", i);     }     return 0; } output: Enter the number to find the factors of :  128 Factors of 128 are:                 1                 2                 4                 8                 16                 32                 64

Palindrome Number

Java Program whether the number is Palindrome or not. Implementation :- import java.util.Scanner; class Palindm {     public static void main(String[] args)     {         int rm, rv=0,act;         System.out.println("Enter a number: ");         Scanner s = new Scanner(System.in);         int num = s.nextInt();         act=num;         while(num > 0)         {             rm = num%10;             rv = rv*10 + rm;             num = num/10;         }         if(rv==act)         {             System.out.println(act+" is a Palindrome Number.");         }         else             System.out.println(act+" is not a Palindrome Number.");     } } D:\AllPrograms\1232>javac Palindm.java D:\AllPrograms\1232>java Palindm Enter a number: 121 121 is a Palindrome Number. D:\AllPrograms\1232>java Palindm Enter a number: 432 432 is not a Palindrome Number. D:\AllPrograms\1232>java Palindm Enter a number: 65656 65656 is a Palindrome Number.

Java Program to Check Prime or Not

To check the entered number by user is prime or not have to know about the prime number. Prime Number :- Prime Number is divisible by 1 or itself. So in below program we check from 2 to the half of the number  and divide it with integer i every iteration and count it division by counter c. If counter is c is then it is prime number otherwise number is not Prime. Implementation :- import java.util.Scanner; class PrimeNum {     public static void main(String[] args)     {         System.out.println("Enter a number: ");         Scanner sc = new Scanner(System.in);         int num = sc.nextInt();         int c=0,i;         for(i=2; i<=num/2; i++)         {             if(num%i==0)             {                 c++;                 break;             }                }         if(c==0)            {                System.out.println(num+" is a prime number");            }         else         {              System.out.println(num+" is not a prime number&

Table of number

Java Program For table of a number entered by User Implementation :- import java.util.Scanner; public class Table {     public static void main(String[] args)     {         System.out.println("Enter a number: ");         Scanner sc = new Scanner(System.in);         int num = sc.nextInt();         for(int i=1; i<=10; i++)            {                System.out.println(num+" x "+i+ " = "+i*num);            }     } }  Output:     D:\AllPrograms\1232>javac Table.java D:\AllPrograms\1232>java Table Enter a number: 34 34 x 1 = 34 34 x 2 = 68 34 x 3 = 102 34 x 4 = 136 34 x 5 = 170 34 x 6 = 204 34 x 7 = 238 34 x 8 = 272 34 x 9 = 306 34 x 10 = 340  

To check the given charecter is vowel or Consonant

Java Program To check the given charecter is vowel or Consonant  Implementation :- import java.util.Scanner; class VowelConst1 {     public static void main(String[] args)     {         System.out.println("Enter a Character: ");         Scanner sc = new Scanner(System.in);         char ch = sc.next().charAt(0);         if(ch =='a' || ch =='A'|| ch=='e' || ch =='E' || ch =='i' || ch =='I' || ch =='o' || ch =='O' || ch =='u' || ch =='U')             {                 System.out.println("Character is Vowel");             }         else            {                System.out.println("Character is consonant");            }     } }  Output:  Enter a Character:  a  Character is Vowel

Factorial of a number entered by User

Factorial :- The factorial (represent with symbol: ! ) multiply all whole numbers from our chosen number down to 1. The general formula of factorial can be written in fully expanded form as :- n! = n x (n-1) x (n-2) x ...x 3 x 2 x 1. or in partially expanded form as n! = n  x (n-1)!. Examples :- 1! = 1 3! = 3 x 2 x 1 = 6 4! = 4 × 3 × 2 × 1 = 24 7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040 0! = 1 n! = n x (n-1)! 1! = 1 x (1-1)! 1! = 1 x (0)! 0!=must be equal to 1 then only it give the result as 1. 1! = 1 Implimentatin of factorial program:- n =int(input("Enter the number to find factorial: ")) fact=1 if(n<0):     print("Factorial is not possible for negative number! ") elif(n==0 or n==1):     print("The factorial of ",n," is ",fact) else:     for i in range(1,n + 1):         fact = fact*i     print("The factorial of ",n," is ",fact)

Find large between two number

Factorial Program Find  large  between two number enter by User  Input Format: The first line of input contains two numbers separated by a space Output Format: Print the larger number x, y = tuple(list(map(int, input("Enter the two number to find the largest. ").split(' ')))) print(max(x,y))                       OR x,y = input("Enter the two number to find the largest. ").split(" ") x = int(x) y = int(y) if(x>y):     print(x) else:     print(y)

Loops, List and Sum program in Python

Here is the program for loops, list and sum in python Question: The first line of the input contains a number N representing the number of elements in array A. The second line of the input contains N numbers separated by a space. (after the last elements, there is no space) Output Format: Print the resultant array elements separated by a space. (no space after the last element) Implimentation-: n=int(input()) arr=list(map(int,input().split(" "))) for i in range(n):   print(arr[i]+arr[-i-1], end = " ") OR N = int(input()) A = [int(i) for i in input().split(" ")] B = [] for i in range(len(A)-1, -1,-1):     B.append(A[i]) C = [] for i in range(len(B)):     C.append(A[i]+B[i]) for i in range(len(C)):     if(i==len(C)-1):         print(C[i])     else:         print(C[i],end=" ")

HTML code for Resume

CODE <!DOCTYPE html> <html>   <head>     <title>Sample page</title>   </head>   <body bgcolor="lightblue">         <h1 align="center"> !!! Resume !!!</h1><hr>     <div>     <h1>Personal Detail</h1><hr>     <img src="Image\IMG1.jpg" align="right" height="120px" width="100px">     <font size="4" style="font-family: verdana">     <table width="40%">         <tr>             <td><b>Name:</b></td>             <td>Pankaj</td>         </tr>         <tr>             <td><b>Address:</b></td>             <td><p>Gandhi Hostel, REC Ambedkar Nagar</p></td>         </tr>         <tr>             <td><b>Mobile Number:</b></td>             <td>+

C program for DFA accept binary string which decimal equivalent is divisible by 5

Image
DFA accept binary string which decimal equivalent is divisible by 5     Binary string decimal equivalent(a) We need that the number of a's will be only multiple of 5, mod(5)=0 e.g 0mod(5)=0,5mod(5)=0,10mod(5)=0 #include <stdio.h> #define TOTAL_STATES 5 #define FINAL_STATES 1 #define ALPHABET_CHARCTERS 2 #define UNKNOWN_SYMBOL_ERR 0 #define NOT_REACHED_FINAL_STATE 1 #define REACHED_FINAL_STATE 2 enum DFA_STATES{q0,q1,q2,q3,q4}; enum input{a,b}; int Accepted_states[FINAL_STATES]={q0}; char alphabet[ALPHABET_CHARCTERS]={'0','1'}; int Transition_Table[TOTAL_STATES][ALPHABET_CHARCTERS]; int Current_state=q0; void DefineDFA() {     Transition_Table[q0][0] = q0;     Transition_Table[q0][1] = q1;     Transition_Table[q1][0] = q2;     Transition_Table[q1][1] = q3;     Transition_Table[q2][0] = q4;     Transition_Table[q2][1] = q0;     Transition_Table[q3][0] = q1;     Transition_Table[q3][1] = q2;     Transition_Table[q4][0] = q3;     Transition_Table[

C program for DFA

DFA which accept the string (ab)*b (String ending with b). #include <stdio.h> #define TOTAL_STATES 4 #define FINAL_STATES 1 #define ALPHABET_CHARCTERS 2 #define UNKNOWN_SYMBOL_ERR 0 #define NOT_REACHED_FINAL_STATE 1 #define REACHED_FINAL_STATE 2 enum DFA_STATES{q0,q1,q2,q3}; enum input{a,b}; int Accepted_states[FINAL_STATES]={q2}; char alphabet[ALPHABET_CHARCTERS]={'a','b'}; int Transition_Table[TOTAL_STATES][ALPHABET_CHARCTERS]; int Current_state=q0; void DefineDFA() {     Transition_Table[q0][a] = q1;     Transition_Table[q0][b] = q2;     Transition_Table[q1][a] = q3;     Transition_Table[q1][b] = q0;     Transition_Table[q2][a] = q3;     Transition_Table[q2][b] = q3;     Transition_Table[q3][a] = q3;     Transition_Table[q3][b] = q3; } int DFA(char current_symbol) { int i,pos;     for(pos=0;pos<ALPHABET_CHARCTERS; pos++)         if(current_symbol==alphabet[pos])             break;//stops if symb

Quick Sort Algorithm

Image
Quick Sort: Quick Sort algorithm is based on divide-and-conquer approach, recursively processing small and small parts of array list at each level. Here is the three-step divide-and-conquer process for sorting a typical subarray A[p....r]. Divide:  Partition(rearrange) the array A[p ...r] into two sub-arrays A[p ...q-1] and A[q+1 ...r] such that each element of A[p...q-1] is less than and or equal to A[q], which is, in turn less than and or equal to A[q+1 ..r]. Compute the index q as part of this partitioning procedure. Conquer: Sort the two subarray A[p ...q-1] and A[q+1 ..r] by recursive calls of quicksort. Combine: Subarrays are sorted in place, no work is needed to combine them.   Quick Sort algorithm chooses an element as a pivot. And partitions the given array around choose the pivot element. There are many ways to choose a pivot element for partition. Always choose the first element as a pivot. Always choose the last element as a pivot. Choose a random element a

Developing First Program in C

Prerequisite 1. Editor 2. Compiler 3. Run-time environment (OS). Editor + Compiler are availble as IDE (Integrated Development Environment). Such as Turbo C/C++, Dev  C++, codeblocks.  Ex: #include <stdio.h> #include <conio.h> void main() {     clrscr();     printf("Welcome to C\n");     getch(); } #include: #include is a directive that calls the preprocessor to include the header file with our program.  Header file :                       Header file contains the declaration of the predefined function.   <stdio.h>   --> standard input output header file. This header file contain standard input/output related predefine function. Ex: printf(), scanf(), putc(), puts(), gets(), getc().... etc. <conio.h> -->   console input output header file. This header file contains console screen related input/output  related predefine. Ex: clrscr(), clreol(), insline(), gotoxy(), getch(), getche(), cprintf(), cscanf().... etc.  main(

Introduction of C

                          Basics of C Q-1 What is C?. Ans- C is a computer Programming language developed by Dennis Ritchie in 1972 at AT & T laboratory the USA. Q-2 What is Computer? Ans- A computer is an electronic machine that takes some data as input through the input device and after processing the data produced some information as output on the output device. Programming Language:- A language in that we can write the program is called a programming language. Q-2 What is Program? A program is a set of instruction. Q-2 What is Instruction? Instruction is a command given to the computer to perform some task. Q-2 What is need of programming language? The programming language is used to develop the software. The software is used to provide communication between human and machine. In other word, languages are the medium of communication between human and machine. NOTE:- Whatever code we write in the program which is the form of a high-level language ( Human

Pointers in C and C++

Image
Pointers in C and C++ A pointer is a variable that stores the address of another variable. Unlike other variables that hold values of a certain type, pointer holds the address of a variable. For example, an integer variable holds (or you can say stores) an integer value, however an integer pointer holds the address of a integer variable.  Pointers store address of variables or a memory location.   Syntax: datatype *var_name; Ex: int *ptr;    To access address of a variable to a pointer, we use the unary operator & (ampersand) that returns the address of that variable. For example &x gives us address of variable x. #include <stdio.h>   int main() {     int x;       // Prints address of x     printf("%p", &x);       return 0; } EX: #include <stdio.h> int main() {    int num = 10;    printf("Value of variable num is: %d", num);    /* To print the address of a variable we use %p     * format specifier and ampersand (&) sign just     *

Manipulating Strings in C++

Introduction A string is a sequence of character. We have used null terminated <char> arrays (C-strings or C-style strings) to store and manipulate strings. ANSI C++ provides a class called string. Commonly Used String Constructors String();    // For creating an empty string. String(const char *str);    // For creating a string object from a null-terminated string. String(const string &str);    // For creating a string object from other string object Creating String Objects string s1, s3;    // Using constructor with no arguments. string s2(“xyz”);    // Using one-argument constructor. s1 = s2;        // Assigning string objects s3 = “abc” + s2;    // Concatenating strings cin >> s1;        // Reading from keyboard (one word) cout << s2;        // Display the content of s2 getline(cin, s1)    // Reading from keyboard a line of text s3 += s1;        // s3 = s3 + s1; s3 += “abc”;        // s3 = s3 + “abc”; Manipulating String Objects string s1(“12345”); string s