Posts

File Handling Operation In C Language

CSZONE #include<stdio.h> #include<stdlib.h> #include<string.h> FILE *f; int insert(); int read(); int count(); int main(){     int ch;        system("clear");     printf("\n\t\tFile Handling");        printf("\n\t\tMenu");     printf("\n\t\t1.Write ");     printf("\n\t\t2.Read");     printf("\n\t\t3 Count ");         printf("\n\t\tEnter Your Choice : ");     scanf("%d",&ch);         switch(ch){                 case 1 : insert();                 break;         case 2 : read();                 break;         case 3 : count();                 break;         default: printf("\n\t\tYou Have Entered Wrong Choice.. Try Again...");                 exit(0);     }     return 0;     } int insert(){         char ch[500];         f = fopen("File.c","w");         if(f==NULL){         printf("\n\t\tFile Opening Error.

he challenge here is to read n lines of input until you reach EOF, then number and print all n lines of content. Hint: Java's Scanner.hasNext() method is helpful for this problem.

CSZONE Sample Input Hello world I am a file Read me until end-of-file. Sample Output 1 Hello world 2 I am a file 3 Read me until end-of-file. import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution {     public static void main(String[] args) {         /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */         Scanner scan = new Scanner(System.in);         int i=1;         while(scan.hasNext()){             System.out.println(i + " " + scan.nextLine());             i++;         }     } }

There are three lines of output: For the first line, sum the lengths of A and B. For the second line, write Yes if A is lexicographically greater than B otherwise print No instead. For the third line, capitalize the first letter in both A and B print them on a single line, separated by a space.

CSZONE import java.io.*; import java.util.*; public class Solution { public static String capitalize(String str){ if(str == null ){ return str; }else{ return str.substring(0,1).toUpperCase()+ str.substring(1).toLowerCase(); } } public static void main(String[] args) { Scanner sc=new Scanner(System.in); String A=sc.next(); String B=sc.next(); /* Enter your code here. Print output to STDOUT. */ System.out.println( A.length()+B.length()); if(A.compareTo(B)>0){ System.out.println("Yes"); }else{ System.out.println("No"); } System.out.println(capitalize(A)+" "+capitalize(B)); } }

In this challenge, we test your knowledge of using if-else conditional statements to automate decision-making processes. An if-else statement has the following logical flow:

CSZONE import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int N = scanner.nextInt(); if(N%2 !=0){ System.out.print("Weird"); }else if(N%2==0 && N >=2 && N<=5){ System.out.print("Not Weird"); }else if(N%2==0 && N >=6 && N<=20){ System.out.print("Weird"); }else if(N%2==0 && N>=20){ System.out.print("Not Weird"); } } }

Task Given the meal price (base cost of a meal), tip percent (the percentage of the meal price being added as tip), and tax percent (the percentage of the meal price being added as tax) for a meal, find and print the meal's total cost.

CSZONE import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { // Complete the solve function below. static void solve(double meal_cost, int tip_percent, int tax_percent) { double total = meal_cost+meal_cost*((tip_percent/100.0)+(tax_percent/100.0)); System.out.print((int) Math.round(total)); } private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) { double meal_cost = scanner.nextDouble(); int tip_percent = scanner.nextInt(); int tax_percent = scanner.nextInt(); solve(meal_cost, tip_percent, tax_percent); scanner.close(); } }

Ques WAP for exception handling to check user age is eligible to vote or not in java?

Image
import java.util.Scanner;   class AgeException extends Exception {     public AgeException(String str) {   System.out.println(str);   } } public class AgeExcDemo {   public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Enter Your Age :: "); int age = s.nextInt();   try { if(age < 18)  throw new AgeException("You Are Not Eligible To Vote"); else System.out.println("You Are Eligible  To Vote");   }   catch (AgeException a) { System.out.println(a);   }  } } Download Code  //Output :

C Program For Memory Allocation With Calloc And Free

Image
#include<stdio.h> #include<stdlib.h> #include<conio.h> #include<windows.h> void main(){ int* ptr; int n,i; system("cls"); printf("\n\t\tProgram Of Calloc With Free "); printf("\n\t\tEnter Number of Elements : "); scanf("%d",&n); ptr=(int*) calloc(n,sizeof(int)); if(ptr==NULL){ printf("\n\t\tMemory Not Allocated "); exit(0); }else{ printf("\n\t\tMemory Is Allocated"); printf("\n\t\tGetting elements....."); for (i=0;i<n;i++){ printf("\n\t\t%d : ",i+1); scanf("%d",&ptr[i]); } printf("\n\t\tPrinting Elements\n"); for(i=0;i<n;i++){ printf("\n\t\t%d",ptr[i]); } } free(ptr); printf("\n\n\t\tMemory Free In Calloc\n"); getch(); } //Output: