import java.util.Scanner; class Main { public static void main(String[] args) { Scanner myObj = new Scanner(System.in); int n; System.out.println("Enter no :"); n = myObj.nextInt(); int sum = 1,i,count=0,sub; for (i = 3; i <= n; i = i + 2) { count++; if (count == 2) { sub = sum - i; sum = sub; coRead more
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
int n;
System.out.println("Enter no :");
n = myObj.nextInt();
int sum = 1,i,count=0,sub;
for (i = 3; i <= n; i = i + 2)
{
count++;
if (count == 2)
{
sub = sum - i;
sum = sub;
count = 0;
continue;
}
sum = sum + i;
}
System.out.println("Sum is: " + sum);
}
}
import java.io.FileNotFoundException; public class Main { Main(String msg) { msg = "I AM JAVA"; System.out.println(msg); } public static void main(String[] args) throws Exception { try { // Suppose here you are looking for any resource for eg.File // If program is unable to find file throw new FileNRead more
import java.io.FileNotFoundException;
public class Main {
Main(String msg) {
msg = "I AM JAVA";
System.out.println(msg);
}
public static void main(String[] args)
throws Exception {
try {
// Suppose here you are looking for any resource for eg.File
// If program is unable to find file
throw new FileNotFoundException();
} catch (FileNotFoundException e) {
throw new Exception("File not found");
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
// This block will get get executed whether exception occures or
// not.
System.out.println("i will get printed");
}
}
}
CODECHEF PRACTICE PROBLEM WEEK 6 AND 7:
CODECHEF PRACTICE PROBLEM WEEK 6 AND 7 SOLUTION :
CODECHEF PRACTICE PROBLEM WEEK 6 AND 7 SOLUTION :
See lessWrite a program to implement Binary Search Tree.
#include <iostream> using namespace std; class BST { int data; BST *left, *right; public: BST(); BST(int); BST* Insert(BST*, int); void Inorder(BST*); }; BST ::BST() : data(0) , left(NULL) , right(NULL) { } BST ::BST(int value) { data = value; left = right = NULL; } BST* BST ::Insert(BST* rootRead more
#include <iostream> using namespace std; class BST { int data; BST *left, *right; public: BST(); BST(int); BST* Insert(BST*, int); void Inorder(BST*); }; BST ::BST() : data(0) , left(NULL) , right(NULL) { } BST ::BST(int value) { data = value; left = right = NULL; } BST* BST ::Insert(BST* root, int value) { if (!root) { return new BST(value); } if (value > root->data) { root->right = Insert(root->right, value); } else { root->left = Insert(root->left, value); } return root; } void BST ::Inorder(BST* root) { if (!root) { return; } Inorder(root->left); cout << root->data << endl; Inorder(root->right); } int main() { BST b, *root = NULL; root = b.Insert(root, 50); b.Insert(root, 30); b.Insert(root, 20); b.Insert(root, 40); b.Insert(root, 70); b.Insert(root, 60); b.Insert(root, 80); b.Inorder(root); return 0; }See lessJava Retrieve the odd numbers till given input number. add and subtract it consecutively and return the result. Input:9 Output:1+3-5+7-9=-3
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner myObj = new Scanner(System.in); int n; System.out.println("Enter no :"); n = myObj.nextInt(); int sum = 1,i,count=0,sub; for (i = 3; i <= n; i = i + 2) { count++; if (count == 2) { sub = sum - i; sum = sub; coRead more
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner myObj = new Scanner(System.in); int n; System.out.println("Enter no :"); n = myObj.nextInt(); int sum = 1,i,count=0,sub; for (i = 3; i <= n; i = i + 2) { count++; if (count == 2) { sub = sum - i; sum = sub; count = 0; continue; } sum = sum + i; } System.out.println("Sum is: " + sum); } }See lessJava Create a class with a main( ) that throws an object of class Exception inside a try block. Give …
import java.io.FileNotFoundException; public class Main { Main(String msg) { msg = "I AM JAVA"; System.out.println(msg); } public static void main(String[] args) throws Exception { try { // Suppose here you are looking for any resource for eg.File // If program is unable to find file throw new FileNRead more
import java.io.FileNotFoundException; public class Main { Main(String msg) { msg = "I AM JAVA"; System.out.println(msg); } public static void main(String[] args) throws Exception { try { // Suppose here you are looking for any resource for eg.File // If program is unable to find file throw new FileNotFoundException(); } catch (FileNotFoundException e) { throw new Exception("File not found"); } catch (Exception e) { System.out.println(e.getMessage()); } finally { // This block will get get executed whether exception occures or // not. System.out.println("i will get printed"); } } }See lessC Program for Depth First Binary Tree Search using Recursion.
#include <stdio.h> #include <stdlib.h> struct node { int a; struct node *left; struct node *right; }; void generate(struct node **, int); void DFS(struct node *); void delete(struct node **); int main() { struct node *head = NULL; int choice = 0, num, flag = 0, key; do { printf("\nEnterRead more
#include <stdio.h> #include <stdlib.h> struct node { int a; struct node *left; struct node *right; }; void generate(struct node **, int); void DFS(struct node *); void delete(struct node **); int main() { struct node *head = NULL; int choice = 0, num, flag = 0, key; do { printf("\nEnter your choice:\n1. Insert\n2. Perform DFS Traversal\n3. Exit\nChoice: "); scanf("%d", &choice); switch(choice) { case 1: printf("Enter element to insert: "); scanf("%d", &num); generate(&head, num); break; case 2: DFS(head); break; case 3: delete(&head); printf("Memory Cleared\nPROGRAM TERMINATED\n"); break; default: printf("Not a valid input, try again\n"); } } while (choice != 3); return 0; } void generate(struct node **head, int num) { struct node *temp = *head, *prev = *head; if (*head == NULL) { *head = (struct node *)malloc(sizeof(struct node)); (*head)->a = num; (*head)->left = (*head)->right = NULL; } else { while (temp != NULL) { if (num > temp->a) { prev = temp; temp = temp->right; } else { prev = temp; temp = temp->left; } } temp = (struct node *)malloc(sizeof(struct node)); temp->a = num; if (num >= prev->a) { prev->right = temp; } else { prev->left = temp; } } } void DFS(struct node *head) { if (head) { if (head->left) { DFS(head->left); } if (head->right) { DFS(head->right); } printf("%d ", head->a); } } void delete(struct node **head) { if (*head != NULL) { if ((*head)->left) { delete(&(*head)->left); } if ((*head)->right) { delete(&(*head)->right); } free(*head); } }See lessC Program to Find the Largest value in a Tree using Inorder Traversal.
#include <stdio.h> #include <stdlib.h> struct node { int info; struct node *left, *right; }; struct node *createnode(int key) { struct node *newnode = (struct node*)malloc(sizeof(struct node)); newnode->info = key; newnode->left = NULL; newnode->right = NULL; return(newnode); }Read more
#include <stdio.h> #include <stdlib.h> struct node { int info; struct node *left, *right; }; struct node *createnode(int key) { struct node *newnode = (struct node*)malloc(sizeof(struct node)); newnode->info = key; newnode->left = NULL; newnode->right = NULL; return(newnode); } void inorder(struct node *root) { if(root != NULL) { inorder(root->left); printf(" %d ",root->info); inorder(root->right); } } void largest(struct node *root) { while (root != NULL && root->right != NULL) { root = root->right; } printf("\nLargest value is %d\n", root->info); } /* * Main Function */ int main() { struct node *newnode = createnode(25); newnode->left = createnode(17); newnode->right = createnode(35); newnode->left->left = createnode(13); newnode->left->right = createnode(19); newnode->right->left = createnode(27); newnode->right->right = createnode(55); /* Sample Tree 1: * 25 * / \ * 17 35 * / \ / \ * 13 19 27 55 */ printf("Inorder traversal of tree 1 :"); inorder(newnode); largest(newnode); /* Creating second Tree. */ struct node *node = createnode(1); node->right = createnode(2); node->right->right = createnode(3); node->right->right->right = createnode(4); node->right->right->right->right = createnode(5); /* Sample Tree 2: Right Skewed Tree (Unbalanced). * 1 * \ * 2 * \ * 3 * \ * 4 * \ * 5 */ printf("\nInorder traversal of tree 2 :"); inorder(node); largest(node); struct node *root = createnode(15); printf("\nInorder traversal of tree 3 :"); inorder(root); largest(root); return 0; }See less