tag:blogger.com,1999:blog-29649590299921479712024-03-14T11:46:20.556+05:30 InstanceOfJavaInstance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.saideshhttp://www.blogger.com/profile/03671387577197113199noreply@blogger.comBlogger532125tag:blogger.com,1999:blog-2964959029992147971.post-47884914046373641552023-01-26T00:22:00.002+05:302023-01-26T00:22:35.096+05:30java remove duplicates from string array<p>we can remove duplicates from a string array using a combination of a Set and an array. A Set is a collection that does not allow duplicate elements, so by adding the elements of the array to a Set, any duplicates will be automatically removed.</p><p>Here is an example of how to remove duplicates from a string array:</p><p><span style="color: #274e13;"><b>String[] array = {"a", "b", "c", "a", "d", "b"};</b></span></p><p><span style="color: #274e13;"><b>Set<String> set = new HashSet<>(Arrays.asList(array));</b></span></p><p><span style="color: #274e13;"><b>String[] uniqueArray = set.toArray(new String[set.size()]);</b></span></p><div><br /></div><div><div>In this example, we first create a string array with duplicate values. We then create a new HashSet and pass the array to its constructor, which automatically removes any duplicates. We then use the toArray() method of the Set to convert it back to an array, which now only contains unique values.</div><div><br /></div><div>It's important to note that the order of the elements in the array is not guaranteed to be preserved after removing duplicates using this method. If you want to maintain the order of the elements in the array, you can use a List or a LinkedHashSet instead of a HashSet.</div><div><br /></div><div>Removing duplicates from a string array in Java can be achieved by converting the array to a Set, which automatically removes any duplicates, and then converting it back to an array. This method is efficient and easy to implement, and it can be used to remove duplicates from any type of array.</div></div><div><br /></div><div><br /></div><div>In Java 8, can use the Stream API to remove duplicates from a string array. The Stream API provides a functional and declarative way of processing collections of data, including arrays. Here is an example of how to use the Stream API to remove duplicates from a string array:</div><div><br /></div><div><b><span style="color: #274e13;"><br /></span></b></div><div><div><b><span style="color: #274e13;">String[] array = {"a", "b", "c", "a", "d", "b"};</span></b></div><div><b><span style="color: #274e13;"><br /></span></b></div><div><b><span style="color: #274e13;">String[] uniqueArray = Arrays.stream(array)</span></b></div><div><b><span style="color: #274e13;"> .distinct()</span></b></div><div><b><span style="color: #274e13;"> .toArray(String[]::new);</span></b></div></div><div><br /></div><div><div>In this example, we first create a string array with duplicate values. We then create a stream of the array using the Arrays.stream() method, and then use the distinct() method to remove the duplicates. Finally, we use the toArray() method to convert the stream back to an array, which now only contains unique values.</div><div><br /></div><div>It's important to note that the order of the elements in the array is not guaranteed to be preserved after removing duplicates using this method. If you want to maintain the order of the elements in the array, you can use LinkedHashSet instead of the distinct() method, and then convert it back to the array.</div><div><br /></div><div>In summary, in Java 8, you can use the Stream API to remove duplicates from a string array by creating a stream of the array and using the distinct() method to remove the duplicates. This method is efficient and easy to implement, and it can be used to remove duplicates from any type of array, it's a functional and declarative way of processing collections of data.</div></div><div><br /></div><div><div>in both cases, the order of the elements in the array is not guaranteed to be preserved after removing duplicates, if you want to maintain the order you can use a List or a LinkedHashSet instead of a HashSet or use the distinct() method.</div><div><br /></div><div>Java 8 provides multiple ways to remove duplicates from a string array using the Stream API, you can use the distinct() method or a collection such as a Set to remove duplicates, both methods are efficient and easy to implement.</div></div>Instanceofjavahttp://www.blogger.com/profile/00477939351667667851noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-65276259723257303092023-01-26T00:01:00.004+05:302023-01-26T00:04:45.882+05:30sum of n numbers in c using for loop<p> <b style="color: #660000;">Here is example of a C program that uses a for loop to calculate the sum of 'n' numbers:</b></p><div><div class="program-div"><div class="dp-highlighter"><ol class="dp-j"><li class="alt">#include <stdio.h></li><li class="alt"><br /></li><li class="alt">int main() {</li><li class="alt"> int n, i, num, sum = 0;</li><li class="alt"><br /></li><li class="alt"> printf("Enter the value of n: ");</li><li class="alt"> scanf("%d", &n);</li><li class="alt"><br /></li><li class="alt"> for (i = 1; i <= n; i++) {</li><li class="alt"> printf("Enter the number: ");</li><li class="alt"> scanf("%d", &num);</li><li class="alt"> sum += num;</li><li class="alt"> }</li><li class="alt"><br /></li><li class="alt"> printf("The sum of %d numbers is %d\n", n, sum);</li><li class="alt"><br /></li><li class="alt"> return 0;</li><li class="alt">}</li><li><br /></li></ol></div></div></div><p>In this program, first, the user is prompted to enter the value of 'n' (the number of numbers to be added). Then, a for loop is used to iterate 'n' times and prompt the user to enter a number in each iteration. The variable 'sum' is initialized to 0 and it's being used to keep the sum of all numbers entered by the user. In each iteration, the value of 'num' is added to 'sum' using the += operator. Finally, the program prints the sum of 'n' numbers.</p><p>In this example, the for loop starts with i=1, and it will run as long as i <= n, with i being incremented by 1 in each iteration.</p><p>It's important to note that, if you want to input the n numbers at once, you can use an array and use a for loop to iterate over the array and add the numbers to the sum variable.</p><p>This C program uses a for loop to calculate the sum of 'n' numbers by prompting the user to enter a number in each iteration of the loop, adding it to a running sum, and finally printing the total sum at the end. This is a simple and efficient way to calculate the sum of multiple numbers.</p>Instanceofjavahttp://www.blogger.com/profile/00477939351667667851noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-27540577975126610292023-01-22T00:16:00.002+05:302023-01-22T00:18:05.824+05:30what happens when a constructor is defined for an interface?<p></p><ul style="text-align: left;"><li>Interfaces in Java do not have constructors. An interface is a blueprint for a class and it cannot be instantiated. An interface defines a set of methods and variables that a class must implement, but it does not contain any implementation for those methods.</li><li>Java does not allow constructors to be defined in an interface, because the purpose of an interface is to define a set of methods that can be implemented by a class, and not to provide an implementation for those methods. Constructors are used to initialize an object, but since an interface cannot be instantiated, there is no object to initialize.</li><li>If you try to define a constructor in an interface, the compiler will throw an error: "Interface methods cannot have a body."</li><li>However, you can have a default method in an interface, which is a method with a defined body, but it is not a constructor.</li><li>In summary, constructors are not allowed in interfaces, because interfaces are used to define a set of methods that can be implemented by a class, and not to provide an implementation for those methods.</li><li> In Java 8 and later versions, the concept of a default method has been introduced. A default method is a method that has a defined body and can be used to provide a default implementation for a method in an interface, but it's not a constructor.</li></ul><p></p><div><ul style="text-align: left;"><li> If you try to define a constructor in an interface, it will result in a compilation error.</li><li>Interfaces in Java do not have constructors. An interface is a blueprint for a class and it cannot be instantiated. An interface defines a set of methods and variables that a class must implement, but it does not contain any implementation for those methods.</li><li><br /></li><li>When the compiler encounters a constructor definition in an interface, it will throw an error because constructors are not allowed in interfaces. The error message will typically be similar to "Interface methods cannot have a body" or "Illegal combination of modifiers: 'constructor' and 'interface'".</li></ul><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjtk21V7yNHzO2eBqRgFdeAInefyQKJlGQCERKLh4d_cVH8-dH_9hYzO9_BdIHM_EtMwwvnYcWqTh2E9hZ6D7_bidrskJyKAtHjJkPFqK141FCcCtgwzSGW1BI582FdesxO85m8itH20XQIv4YIQKf6uI6-GcOP_RrEPmoJ1OMx0hYwj-H4KsZklvBuag/s766/interface%20constructor.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="199" data-original-width="766" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjtk21V7yNHzO2eBqRgFdeAInefyQKJlGQCERKLh4d_cVH8-dH_9hYzO9_BdIHM_EtMwwvnYcWqTh2E9hZ6D7_bidrskJyKAtHjJkPFqK141FCcCtgwzSGW1BI582FdesxO85m8itH20XQIv4YIQKf6uI6-GcOP_RrEPmoJ1OMx0hYwj-H4KsZklvBuag/s16000/interface%20constructor.png" /></a></div><br /><div><br /></div></div>Instanceofjavahttp://www.blogger.com/profile/00477939351667667851noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-59177014126405720212023-01-22T00:06:00.005+05:302023-01-22T00:06:57.517+05:30c program for addition, subtraction, multiplication and division using switch<p> C program for addition, subtraction, multiplication and division using switch</p><p><br /></p><div><div class="program-div"><div class="dp-highlighter"><ol class="dp-j"><li class="alt">#include <stdio.h></li><li class="alt"></li><li class="alt"><br /></li><li class="alt">int main() {</li><li class="alt"> int num1, num2, choice;</li><li class="alt"> float result;</li><li class="alt"> printf("Enter two numbers: ");</li><li class="alt"> scanf("%d %d", &num1, &num2);</li><li class="alt"> printf("Enter your choice: \n1 for addition\n2 for subtraction\n3 for multiplication\n4 for division\n");</li><li class="alt"> scanf("%d", &choice);</li><li class="alt"><br /></li><li class="alt"> switch(choice) {</li><li class="alt"> case 1:</li><li class="alt"> result = num1 + num2;</li><li class="alt"> printf("Addition: %.2f\n", result);</li><li class="alt"> break;</li><li class="alt"> case 2:</li><li class="alt"> result = num1 - num2;</li><li class="alt"> printf("Subtraction: %.2f\n", result);</li><li class="alt"> break;</li><li class="alt"> case 3:</li><li class="alt"> result = num1 * num2;</li><li class="alt"> printf("Multiplication: %.2f\n", result);</li><li class="alt"> break;</li><li class="alt"> case 4:</li><li class="alt"> result = (float)num1 / num2;</li><li class="alt"> printf("Division: %.2f\n", result);</li><li class="alt"> break;</li><li class="alt"> default:</li><li class="alt"> printf("Invalid choice!\n");</li><li class="alt"> }</li><li class="alt"> return 0;</li><li class="alt">}</li><li></li><li></li></ol></div></div></div><p><br /></p><p></p><ul style="text-align: left;"><li>the user is prompted to enter two numbers and then a choice for the operation to be performed. The choice is taken as an integer input and is used in the switch statement. </li><li>Depending on the user's choice, the program performs the corresponding operation using the two numbers as input.</li><li>It's worth noting that, in the division operation, if the second number is zero, it will cause a runtime error because division by zero is not defined. </li><li>I have used type casting to convert the int variables to float so as to get the decimal value. Also, I have used the format specifier %.2f to print the result with 2 decimal places.</li><li>the user is prompted to enter two numbers and then a choice for the operation to be performed. The choice is taken as an integer input using the scanf function and is used in the switch statement. Depending on the user's choice, the program performs the corresponding operation using the two numbers as input.</li></ul><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqVQ2_lzu0gY-pZcECyRjz-bzW12xeYFUewlr4s0NMzS_y7BBPYig3cDfrc4ZGXFRg0yWHFdKpDJ3FwNT60p-breUyn2GtME6EnXgHRpP9ibS--7ESkXzo4UcZ7RElVu1uVbAj8sL996puW-rNIpCcbI6tjR6c0zveWLgy17YjPlJ-Px892POwiJDm-A/s1330/switch%20case%20in%20c%20for%20addition.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="790" data-original-width="1330" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhqVQ2_lzu0gY-pZcECyRjz-bzW12xeYFUewlr4s0NMzS_y7BBPYig3cDfrc4ZGXFRg0yWHFdKpDJ3FwNT60p-breUyn2GtME6EnXgHRpP9ibS--7ESkXzo4UcZ7RElVu1uVbAj8sL996puW-rNIpCcbI6tjR6c0zveWLgy17YjPlJ-Px892POwiJDm-A/s16000/switch%20case%20in%20c%20for%20addition.png" /></a></div><div><br /></div><p></p><ul><li></li><li>The switch statement checks for the value of the variable choice. Depending on the value, the program enters the corresponding case and performs the operation. The break statement is used at the end of each case to exit the switch statement and prevent the program from executing the next case.</li><li>In the case of the division operation, I have used type casting to convert the int variables to float so as to get the decimal value. Also, I have used the format specifier %.2f to print the result with 2 decimal places.</li><li><br /></li><li>In the default case, if the user enters any value other than 1, 2, 3, or 4, the program displays an "Invalid choice!" message. This is to handle the scenario when the user enters an unexpected value.</li></ul>Instanceofjavahttp://www.blogger.com/profile/00477939351667667851noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-23300247340857455712023-01-21T23:56:00.004+05:302023-01-21T23:58:29.145+05:30c program addition subtraction, multiplication and division using function<p> c program for addition subtraction, multiplication and division using function</p><div><div class="program-div"><div class="dp-highlighter"><ol class="dp-j"><li class="alt">#include <stdio.h></li><li class="alt"></li><li class="alt"><br /></li><li class="alt">int add(int a, int b) {</li><li class="alt"> return a + b;</li><li class="alt">}</li><li class="alt"><br /></li><li class="alt">int subtract(int a, int b) {</li><li class="alt"> return a - b;</li><li class="alt">}</li><li class="alt"><br /></li><li class="alt">int multiply(int a, int b) {</li><li class="alt"> return a * b;</li><li class="alt">}</li><li class="alt"><br /></li><li class="alt">float divide(int a, int b) {</li><li class="alt"> return (float)a / b;</li><li class="alt">}</li><li class="alt"><br /></li><li class="alt">int main() {</li><li class="alt"> int a = 10, b = 5;</li><li class="alt"> int add_result = add(a, b);</li><li class="alt"> int subtract_result = subtract(a, b);</li><li class="alt"> int multiply_result = multiply(a, b);</li><li class="alt"> float divide_result = divide(a, b);</li><li class="alt"><br /></li><li class="alt"> printf("Addition: %d + %d = %d\n", a, b, add_result);</li><li class="alt"> printf("Subtraction: %d - %d = %d\n", a, b, subtract_result);</li><li class="alt"> printf("Multiplication: %d * %d = %d\n", a, b, multiply_result);</li><li class="alt"> printf("Division: %d / %d = %f\n", a, b, divide_result);</li><li class="alt"><br /></li><li class="alt"> return 0;</li><li class="alt">}</li><li><br /></li></ol></div></div></div><p><br /></p><p></p><ul style="text-align: left;"><li>In this program, four functions are defined for addition, subtraction, multiplication, and division respectively. Each function takes two integer arguments a and b, performs the corresponding operation, and returns the result. </li><li>In the main function, variables a and b are initialized with the values 10 and 5 respectively.</li><li>The functions are called and the results are stored in variables add_result, subtract_result, multiply_result, and divide_result. </li><li>These functions take two integer arguments and return the result of the operation. In the main() function, two integers are taken as input, and then the functions are called with these inputs to perform the operations and display the results.</li><li>In the division operation, if the second number is zero, it will cause a runtime error because division by zero is not defined. </li><li>It's also important to consider the case where the division operation results in decimal values, In that case, you should use float or double instead of int.</li></ul><p></p>Instanceofjavahttp://www.blogger.com/profile/00477939351667667851noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-27113896825578816392023-01-21T23:37:00.002+05:302023-01-21T23:37:28.680+05:30semicolon in java<p> a semicolon (;) is used to separate statements in a program. Each statement in a Java program must end with a semicolon, just like in many other programming languages.</p><p>Here's an example of semicolon in a Java program:</p><p><b><span style="color: #274e13;">int x = 5; // Declare a variable and assign a value</span></b></p><p><b><span style="color: #274e13;">System.out.println(x); // Print the value of the variable</span></b></p><p><b><span style="color: #274e13;">x++; // Increment the value of the variable</span></b></p><p><b><span style="color: #274e13;">System.out.println(x); // Print the new value of the variable</span></b></p><p>In this example, there are three statements: one for declaring a variable and assigning a value, one for printing the value of the variable, and one for incrementing the value of the variable. Each statement ends with a semicolon, which separates them from each other.</p><p>It's worth noting that, in Java, a semicolon is not always necessary. For example, if you are defining a class or method, you don't need to use a semicolon at the end of the definition. Also, you don't need to use a semicolon after a block statement (statements enclosed in curly braces {}) that is part of a control statement such as if-else, while, for, etc. The end of the block statement is represented by the closing curly brace.</p><p>In the case of a single-line if statement or a single-line loop, you can also omit the curly braces {} and write the statement directly after the if or while keyword, respectively.</p><p><b><span style="color: #274e13;">if(x==5) System.out.println("x is 5");</span></b></p><p><b><span style="color: #274e13;">while(x<10) x++;</span></b></p><p>In summary, a semicolon is used in Java to separate statements and mark the end of a statement. However, it's not always required and depends on the context of the statement or block.</p><p>It's worth noting that in Java, a semicolon is also used in certain control statements such as the "for" loop and the "enhanced for" (or "for-each") loop.</p><p>In a "for" loop, the initialization, condition, and increment/decrement statements are separated by semicolons. </p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBDvaUISVdI_Q_vbshljwVDW8ndAOIOyqaNcFh7YVHVyQz40MaeilAZtR4Tbs70QP6VNmSnb8HfI67Lm_LN352rjXfJOzL_mQXhzjC1l6jDmDAIwX5bnxkfzGN1jJTRF7d1Q42_A2Jp0TRk12CdVA_aP23Lk2EwNs2WqZFwVDEiP8n84ee-YIfUQ5EpA/s957/semicolon%20in%20java.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="95" data-original-width="957" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBDvaUISVdI_Q_vbshljwVDW8ndAOIOyqaNcFh7YVHVyQz40MaeilAZtR4Tbs70QP6VNmSnb8HfI67Lm_LN352rjXfJOzL_mQXhzjC1l6jDmDAIwX5bnxkfzGN1jJTRF7d1Q42_A2Jp0TRk12CdVA_aP23Lk2EwNs2WqZFwVDEiP8n84ee-YIfUQ5EpA/s16000/semicolon%20in%20java.png" /></a></div><br /><p>In this example, the initialization statement "int i = 0" is executed once before the loop starts, the condition "i < 10" is checked before each iteration, and the increment statement "i++" is executed after each iteration. Each of these statements is separated by a semicolon.</p><p>In the case of the "enhanced for" loop, the semicolon is used to separate the loop variable and the array or collection that is being iterated over.</p>Instanceofjavahttp://www.blogger.com/profile/00477939351667667851noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-70653167716319948222023-01-21T23:19:00.009+05:302023-01-21T23:20:19.935+05:30write a ‘java’ program to display characters from ‘a’ to ‘z’<p> <b style="color: #660000;">Here is a simple program that prints characters from a to Z</b></p><div><div class="program-div"><div class="dp-highlighter"><ol class="dp-j"><li class="alt"></li><li class="alt">public class DisplayAlphabets {</li><li class="alt"> public static void main(String[] args) {</li><li class="alt"> for (char c = 'a'; c <= 'z'; c++) {</li><li class="alt"> System.out.print(c + " ");</li><li class="alt"> }</li><li class="alt"> }</li><li class="alt">}</li><li></li></ol></div></div>
<br />
</div><div><div>This program uses a for loop to iterate through the characters from 'a' to 'z'. The loop variable "c" is initialized to 'a' and incremented by 1 on each iteration until it reaches 'z'. Inside the loop, the current character is printed using the System.out.print() method. The " " at the end of the print statement will print the character followed by a space.</div><div><br /></div><div>Alternatively, we can use the Character.toChars(int) method to convert the int value to a char and use the for loop to iterate from 97 to 122.</div></div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2mMubDGfZBJ-3O3Kgt_YZ2YglpVyjhBl_gBqyNdplnQugtY4TC4uQM22LvLVhxS1LNIXuEPnlCxHQeimPDdPgJwP7iR8L-_SUdXgfqA-BocbhnvnqtYwb48otA5_dt5Kxeg8yYYoqaUlPiCz4fzk-iMZze1HWQ6MwlIrFAsAm59-sAX3xv9U-Lh2URA/s1147/java%20program%20to%20print%20characters%20from%20a%20to%20z.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="java program to print characters from a to z" border="0" data-original-height="201" data-original-width="1147" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2mMubDGfZBJ-3O3Kgt_YZ2YglpVyjhBl_gBqyNdplnQugtY4TC4uQM22LvLVhxS1LNIXuEPnlCxHQeimPDdPgJwP7iR8L-_SUdXgfqA-BocbhnvnqtYwb48otA5_dt5Kxeg8yYYoqaUlPiCz4fzk-iMZze1HWQ6MwlIrFAsAm59-sAX3xv9U-Lh2URA/s16000/java%20program%20to%20print%20characters%20from%20a%20to%20z.png" /></a></div><div><br /></div><div><br /></div><div><div>This will also display the same output as the first example: "a b c d e f g h i j k l m n o p q r s t u v w x y z".</div><div>Note that, in both examples, the alphabets will be printed in lowercase. If we want to print uppercase alphabets, you can use 'A' to 'Z' in the for loop or use 65 to 90 in the Character.toChars(int) method.</div><div><br /></div></div><div>We can also use the java.util.stream.IntStream to display characters from 'a' to 'z'.</div><div> Here is an example of how you can use IntStream to display characters from 'a' to 'z':</div><div><br /></div><div><div><b><span style="color: #274e13;">public class DisplayAlphabets {</span></b></div><div><b><span style="color: #274e13;"> public static void main(String[] args) {</span></b></div><div><b><span style="color: #274e13;"> IntStream.rangeClosed('a', 'z').mapToObj(c -> (char) c).forEach(System.out::print);</span></b></div><div><b><span style="color: #274e13;"> }</span></b></div><div><b><span style="color: #274e13;">}</span></b></div></div><div><br /></div><div><div>This program uses the IntStream.rangeClosed() method to create a stream of ints from the ASCII value of 'a' to the ASCII value of 'z'. Then it uses the mapToObj() method to convert the ints to chars, and finally the forEach() method to print each character.</div><div><br /></div><div>we can also use the java.util.stream.IntStream to display characters from 'A' to 'Z' using the same approach. Here is an example of how you can use IntStream to display characters from 'A' to 'Z':</div><div><br /></div><div><br /></div><div><b><span style="color: #274e13;">public class DisplayAlphabets {</span></b></div><div><b><span style="color: #274e13;"> public static void main(String[] args) {</span></b></div><div><b><span style="color: #274e13;"> IntStream.rangeClosed('A', 'Z').mapToObj(c -> (char) c).forEach(System.out::print);</span></b></div><div><b><span style="color: #274e13;"> }</span></b></div><div><b><span style="color: #274e13;">}</span></b></div><div><br /></div><div>This will print the uppercase alphabets : "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".</div><div><br /></div><div>It's worth noting that, in all examples the alphabets will be displayed in a single line, if you want to display them in separate lines, you can modify the System.out.print() method to System.out.println().</div></div>Instanceofjavahttp://www.blogger.com/profile/00477939351667667851noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-13659906801660237832023-01-21T22:54:00.006+05:302023-01-21T23:00:09.617+05:30How to print array in java<p> To print an array in Java, you can use a for loop to iterate through the array and print each element. Here is an example of how to print an array of integers:</p><p><b style="color: #660000;">Here is a simple program that explains how to print an array of integers in java</b></p><div><div class="program-div"><div class="dp-highlighter"><ol class="dp-j"><li class="alt">int[] myArray = {1, 2, 3, 4, 5};</li><li class="alt">for (int i = 0; i < myArray.length; i++) {</li><li class="alt"> System.out.print(myArray[i] + " ");</li><li class="alt">}</li></ol></div></div></div><p><b>This will print the following output: "1 2 3 4 5".</b></p><p>Alternatively: Arrays.toString(myArray) and directly print the array.</p><p><b><span style="color: #274e13;">System.out.println(Arrays.toString(myArray));</span></b></p><p>This will also print the array in same format "1 2 3 4 5"</p><p>We can also use the enhanced for loop (also known as the "for-each" loop) to print an array in Java. This type of loop automatically iterates through each element in the array, making it more concise and easier to read than a traditional for loop:</p><div><div class="program-div"><div class="dp-highlighter"><ol class="dp-j"><li class="alt"></li><li class="alt">for (int element : myArray) {</li><li class="alt"> System.out.print(element + " ");</li><li class="alt">}</li></ol></div></div></div><p>This will also print the same output as the previous example "1 2 3 4 5".</p><p>When printing arrays of other types, such as Strings or objects, we can use the same approach. For example, if you have an array of Strings, you can use the enhanced for loop to print each element:</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglBvk2K-2c_374Iid9ot20rTtOBzcJrKuASpkPFDNbLcSADOZgT0d8Cq9dZfUPbzbTXQa1H87aLDXpVyg1YDEXNPsRj6OajGRSiDh30NWvWWaEV6L1rv8i0YE6qesZScmebdh2W15gjGV0bGEradCe1qx2ddTotAwJbEn8uJqOdos3tysSrr6F9COlPg/s888/java%20print%20array.png" style="margin-left: 1em; margin-right: 1em;"><img alt="java print array" border="0" data-original-height="147" data-original-width="888" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglBvk2K-2c_374Iid9ot20rTtOBzcJrKuASpkPFDNbLcSADOZgT0d8Cq9dZfUPbzbTXQa1H87aLDXpVyg1YDEXNPsRj6OajGRSiDh30NWvWWaEV6L1rv8i0YE6qesZScmebdh2W15gjGV0bGEradCe1qx2ddTotAwJbEn8uJqOdos3tysSrr6F9COlPg/s16000/java%20print%20array.png" /></a></div><p>This will print the following output: "Hello World".</p><p>It's worth noting that, the Arrays.toString() method will work for all types of arrays, including arrays of objects, it will call the toString() method of each element in the array.</p><p>In case of the custom class objects, you may need to override the toString() method of the class.</p><p>Another way to print an array in Java is to use the Arrays.deepToString() method if the array is a multi-dimensional array. This method is similar to Arrays.toString(), but it can handle multi-dimensional arrays and it will recursively call itself for each sub-array. Here's an example of how to use Arrays.deepToString() to print a 2D array:</p><div><div class="program-div"><div class="dp-highlighter"><ol class="dp-j"><li class="alt">int[][] my2DArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};</li><li class="alt"></li><li class="alt">System.out.println(Arrays.deepToString(my2DArray))</li></ol></div></div></div><p><b>This will print the following output: "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]".</b></p><p>we can also use the java.util.Arrays.stream() method to print an array in Java 8 and later versions. This method returns a stream of the array's elements, which can then be passed to various stream operations such as forEach(). Here's an example of how to use Arrays.stream() to print an array:</p><p><b>int[] myArray = {1, 2, 3, 4, 5};</b></p><p><b>Arrays.stream(myArray).forEach(System.out::print);</b></p><p><b>This will also print the same output as the previous examples "1 2 3 4 5".</b></p><p>It's worth noting that when you use the Arrays.stream() method to print an array, it does not add any spaces or newlines between the elements, unlike the for loop and enhanced for loop. If we want to add spaces or newlines, you can use the map() method to transform each element into a string, and then use the forEach() method to print each string.</p>Instanceofjavahttp://www.blogger.com/profile/00477939351667667851noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-76399781977743533682023-01-10T15:18:00.000+05:302023-01-10T15:18:22.454+05:30Instance variable in java<p> In Java, an instance variable is a variable that is declared in a class, outside of any method or constructor, and that is associated with a specific instance of the class. </p><p>Each object of the class will have its own copy of the instance variable, which can have different values for different objects.</p><p>Here's an example of a class called Person with an instance variable called name:</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQ4sjBijU6CnDXSIPxbb1sVRnuPeCs1MbqpPQ3hbygGufAnNMwo5MPS68hocYZSlMISfXGDg_f5Fy1v-lLJ0lFXmUsH0hH6BGn2r5nYZHsFy2fWneBJFGGlIc_1C1Cg65VwJkng_cYHgGw-6vxjiuaALVCe3AWH6qqHncpAXGbfLeoJJ9ArMnY4_BT6w/s920/instance%20variable%20in%20java.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="306" data-original-width="920" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQ4sjBijU6CnDXSIPxbb1sVRnuPeCs1MbqpPQ3hbygGufAnNMwo5MPS68hocYZSlMISfXGDg_f5Fy1v-lLJ0lFXmUsH0hH6BGn2r5nYZHsFy2fWneBJFGGlIc_1C1Cg65VwJkng_cYHgGw-6vxjiuaALVCe3AWH6qqHncpAXGbfLeoJJ9ArMnY4_BT6w/s16000/instance%20variable%20in%20java.png" /></a></div><br /><p><br /></p><p>In this example, the name variable is declared as a private instance variable, and is therefore only accessible through the class's public getName and setName methods.</p><p>The class Person has a constructor that accepts a name and assigns it to the instance variable name.</p><p>When an object of the Person class is created, it gets its own copy of the name variable.</p><p><span style="color: #274e13;">Person john = new Person("John");</span></p><p><span style="color: #274e13;">Person jane = new Person("Jane");</span></p><p>In this example, the john object has its own copy of the name variable, which is set to "John", while the jane object has its own copy of the name variable, which is set to "Jane".</p><p>Instance variables are also known as non-static fields. They are created each time when an object of the class is created, and if they are not initialized explicitly they get a default value (null for objects, 0 for numeric types, etc). They can also be accessed through object reference.</p><p>Instance variables are usually used to store the state of an object, and are typically marked as private, to enforce encapsulation and to prevent other classes from directly accessing the data.</p><p><br /></p>Instanceofjavahttp://www.blogger.com/profile/00477939351667667851noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-84492105646440777932023-01-10T15:06:00.003+05:302023-01-10T15:06:45.432+05:30Ascii values in c<p> In C, you can use the int data type to represent ASCII values. Each character in the ASCII table is assigned a unique number between 0 and 127. To get the ASCII value of a character, you can simply cast it to an int. Here's an example:</p><p><b>char c = 'A';</b></p><p><b>int asciiValue = (int)c;</b></p><p><b>printf("The ASCII value of '%c' is %d\n", c, asciiValue);</b></p><p>This will output:</p><p><b>The ASCII value of 'A' is 65</b></p><p>Alternatively you can also use the 'C' library function (int)c to get the ASCII value of a character.</p><p><b>char c = 'A';</b></p><p><b>int asciiValue = (int)c;</b></p><p><b>printf("The ASCII value of '%c' is %d\n", c, asciiValue);</b></p><p><b>will give you the same output.</b></p><p>You can also find the ASCII value of a character with a function like getchar(), that returns an int value of the char type that it reads from the keyboard, or with a function like scanf() that reads a character input.</p><p>Additionally, if you want to find the ASCII value of a string, you can use a for loop to iterate through each character of the string and then get the ASCII value of each character by casting it to an int.</p><p><br /></p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirAEPHqJmETYfj476gMxD70RQVDqeGL7phv5JQzellJ0QMZVUpEvXtoppaJdbphmexWOHGJqN9MJmAl1yphTemGW60h8FMwn9OwFHPJOv80czhL5V56Jb9iqQ-1dDYeJLmoo9en5yr9sRjcWlYE4OL4s5MlYVbaje06pxKBDsNR4_mdy-lgurOY6JZ2w/s952/ascii%20value%20in%20c.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="318" data-original-width="952" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirAEPHqJmETYfj476gMxD70RQVDqeGL7phv5JQzellJ0QMZVUpEvXtoppaJdbphmexWOHGJqN9MJmAl1yphTemGW60h8FMwn9OwFHPJOv80czhL5V56Jb9iqQ-1dDYeJLmoo9en5yr9sRjcWlYE4OL4s5MlYVbaje06pxKBDsNR4_mdy-lgurOY6JZ2w/s16000/ascii%20value%20in%20c.png" /></a></div><p><br /></p>Instanceofjavahttp://www.blogger.com/profile/00477939351667667851noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-46970741658054542102023-01-10T15:01:00.001+05:302023-01-10T15:01:16.272+05:30Java enum constructor<p>In Java, an enumeration is a special kind of class that represents a fixed number of predefined values. The values are defined as enum constants, which are static and final fields of the enumeration.</p><p>When you define an enum, you can also define a constructor for it, which is called when each enum constant is created. The constructor is passed the values of the constant's fields and can use those values to initialize the constant.</p><p>Here's an example of an enumeration called Size with a constructor that takes a single String argument:</p><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtGjiOTjRNuNhfwOfrQlUXJKpBlK6Ru8JHLFNtlUANJwIzsghw7uQ32nuzehLN5_RwAQBMSWb_nJ5ks2s90obPfbYHkHOE2HuRWAheDBe5CLXJyS1cIhvuhhzZ32AcJAH4HHG2L2vhnmzLoxdFbMtxqo7kM-te2Rk8SCLoo3bhls0f4hBU4athXuxQ1A/s967/enum%20constructor%20java.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="381" data-original-width="967" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtGjiOTjRNuNhfwOfrQlUXJKpBlK6Ru8JHLFNtlUANJwIzsghw7uQ32nuzehLN5_RwAQBMSWb_nJ5ks2s90obPfbYHkHOE2HuRWAheDBe5CLXJyS1cIhvuhhzZ32AcJAH4HHG2L2vhnmzLoxdFbMtxqo7kM-te2Rk8SCLoo3bhls0f4hBU4athXuxQ1A/s16000/enum%20constructor%20java.png" /></a></div><br /><p>In this example, the Size enumeration has four constants: SMALL, MEDIUM, LARGE, and EXTRA_LARGE. Each constant has an associated abbreviation, which is passed to the constructor when the constant is created. The constructor sets the value of the abbreviation field to the passed-in value.</p><p>You can call the constructor explicitly or it will be called automatically when enum constants are created, so the following code:</p><p><b>Size s = Size.SMALL;</b></p><p>is equivalent to</p><p><b>Size s = new Size("S");</b></p><p>When you define constructors for your enum, it is a best practice to make them private. So that no other classes can create an instance of your Enum. This way you can only use the predefined values of the Enum and not create any new value on runtime.</p>Instanceofjavahttp://www.blogger.com/profile/00477939351667667851noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-47698112778871983492023-01-10T13:41:00.003+05:302023-01-10T13:41:45.169+05:30jstl if else string comparison<p> In JavaServer Pages (JSP), the JSTL (Java Standard Tag Library) provides a set of tags that you can use to perform common tasks, such as iterating over a collection of data, performing conditional logic, and formatting text. The JSTL if tag is used to perform conditional logic in a JSP page. The if tag evaluates a boolean expression, and if the expression evaluates to true, the content inside the if tag is rendered to the response. If the expression evaluates to false, the content inside the if tag is not rendered.</p><p>JSTL (JavaServer Pages Standard Tag Library) is a collection of tags that provide common functionality when working with JSPs (JavaServer Pages). One of the tags provided by JSTL is the <c:if> tag, which can be used to conditionally include content in a JSP based on a Boolean expression.</p><p>Here's an example of how you might use the JSTL if tag to conditionally render content in a JSP page:</p><p><b><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %></b></p><p><b><c:if test="${someCondition}"></b></p><p><b> <p>This content will be displayed if someCondition is true</p></b></p><p><b></c:if></b></p><div>The JSTL if tag also provides an else branch which will be executed if the test conditions return false</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCVBjRERNhkcwXY5kMqSOX_ytKh-fSOKoQ_c7VMuqvAtDJdcws9fJp6Rpwrpuuxl870JJ5Mo1tniLV1bSBouyihLg_DQHMB-ccg3QqQMsDiZ-rBXP121AYCx0jYYGmAtaQevdTplJGyqlOmFPLD2wRyupBgLYZn808KHujOqMOHy95Bi0GLZQRbGoleQ/s1110/jstl%20if%20else.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="jstl if else" border="0" data-original-height="232" data-original-width="1110" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCVBjRERNhkcwXY5kMqSOX_ytKh-fSOKoQ_c7VMuqvAtDJdcws9fJp6Rpwrpuuxl870JJ5Mo1tniLV1bSBouyihLg_DQHMB-ccg3QqQMsDiZ-rBXP121AYCx0jYYGmAtaQevdTplJGyqlOmFPLD2wRyupBgLYZn808KHujOqMOHy95Bi0GLZQRbGoleQ/s16000/jstl%20if%20else.png" /></a></div><br /><div><br /></div><div>Additionally, you can also use c:choose, c:when, c:otherwise tags for conditions where you have more than one test case.</div><div><br /></div><div><br /></div><div><div><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %></div><div><br /></div><div><c:choose></div><div> <c:when test="${someCondition}"></div><div> <p>This content will be displayed if someCondition is true</p></div><div> </c:when></div><div> <c:when test="${anotherCondition}"></div><div> <p>This content will be displayed if anotherCondition is true</p></div><div> </c:when></div><div> <c:otherwise></div><div> <p>This content will be displayed if neither someCondition nor anotherCondition is true</p></div><div> </c:otherwise></div><div></c:choose></div><div>Please note that to use the above-mentioned tags, you need to include JSTL core library in your project.</div><div><br /></div></div>Instanceofjavahttp://www.blogger.com/profile/00477939351667667851noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-50532803958118624412023-01-10T12:52:00.010+05:302023-01-10T12:53:39.040+05:30C program to print table of 2 using for loop<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlaINw7qf7YPdpUx133YnrdqT9cnAEdtT53Ib8kW28VniLXbsLMvoEapoKlrPjMSEULwQA5hqSp4q0dBU8vcw_cgJPNYx5CTDm5x2UES_cPPKLV4IzcQ48Re0o_aQPI_CwbhFxRHlS8dTrzFa1YvTkwvFSzVU-dgQltGiv1a_x3KrizKj9I2HrnU3H4w/s882/mutliplication%20table.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="531" data-original-width="882" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlaINw7qf7YPdpUx133YnrdqT9cnAEdtT53Ib8kW28VniLXbsLMvoEapoKlrPjMSEULwQA5hqSp4q0dBU8vcw_cgJPNYx5CTDm5x2UES_cPPKLV4IzcQ48Re0o_aQPI_CwbhFxRHlS8dTrzFa1YvTkwvFSzVU-dgQltGiv1a_x3KrizKj9I2HrnU3H4w/s16000/mutliplication%20table.png" /></a></div><p><br /></p><p><b>#include <stdio.h></b></p><p><b>int main() {</b></p><p><b> int i;</b></p><p>The first line includes the standard input/output header file, which is needed for the printf function used later in the program.</p><p>The main function is the entry point for the program. It is the first function that is executed when the program runs.</p><p>The next line declares a variable i of type int. This variable will be used as the loop counter in the for loop.</p><p> <b> for (i = 1; i <= 10; i++) {</b></p><p><b> printf("2 * %d = %d\n", i, 2 * i);</b></p><p><b> }</b></p><p>This is the for loop. It starts with the for keyword, followed by parentheses that contain three statements separated by semicolons:</p><p>The initialization statement i = 1 sets the value of i to 1 before the loop starts.</p><p>The loop condition i <= 10 specifies that the loop will continue as long as the value of i is less than or equal to 10.</p><p>The iteration statement i++ increments the value of i by 1 after each iteration of the loop.</p><p>Inside the loop, the printf function is used to print a string to the console. The string contains two %d format specifiers, which are placeholders for two integer values that will be printed. The first %d is replaced by the value of i, and the second %d is replaced by the result of 2 * i. The \n at the end of the string is a newline character that causes the output to be printed on a new line.</p><p>Finally, the return 0; statement at the end of the main function indicates that the program has completed successfully.</p>Instanceofjavahttp://www.blogger.com/profile/00477939351667667851noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-53117599999880966112023-01-08T16:54:00.005+05:302023-01-08T17:01:12.834+05:30C program to find the largest element in an array<p><b style="color: #660000;">C program that finds the largest element in an array :</b></p><div><div class="program-div"><div class="dp-highlighter"><ol class="dp-j"><li class="alt">#include <stdio.h></li><li class="alt">int main() {</li><li class="alt"> int n, i;</li><li class="alt"> long long int t1 = 0, t2 = 1, nextTerm;</li><li class="alt"><br /></li><li class="alt"> printf("Enter the number of terms: ");</li><li class="alt"> scanf("%d", &n);</li><li class="alt"><br /></li><li class="alt"> printf("Fibonacci Series: ");</li><li class="alt"><br /></li><li class="alt"> for (i = 1; i <= n; ++i) {</li><li class="alt"> printf("%lld, ", t1);</li><li class="alt"> nextTerm = t1 + t2;</li><li class="alt"> t1 = t2;</li><li class="alt"> t2 = nextTerm;</li><li class="alt"> }</li><li class="alt"><br /></li><li class="alt"> return 0;</li><li class="alt">}</li><li><br /></li></ol></div></div></div><ul style="text-align: left;"><li>
This program first reads in n, the number of elements in the array. It then reads in n elements from the user and stores them in the array. </li><li>Finally, it iterates through the array and compares each element to the first element (which is initially set to the largest). </li><li>If it finds a larger element, it updates the value of the first element to be the larger value. At the end, the program prints out the largest element in the array.</li></ul>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcXuxftwfkb8I6WJhUifi6AIo1YZwKskNhC4D5N4Es7_w3oBtGW-zUfzJO9yjEWGZ-UMKZLPmygVHJUlXtDmNlVdKy7yTANOTAZfauW5pCD1Syjx5Q8QFYS8EYvT7vL4kym3d4vl2uUYz8kzo2I12gLqgMyFwPdPG8nft9pA22jH5STxI-1Oz-eLBUXA/s1061/largest%20element%20in%20array.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="456" data-original-width="1061" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcXuxftwfkb8I6WJhUifi6AIo1YZwKskNhC4D5N4Es7_w3oBtGW-zUfzJO9yjEWGZ-UMKZLPmygVHJUlXtDmNlVdKy7yTANOTAZfauW5pCD1Syjx5Q8QFYS8EYvT7vL4kym3d4vl2uUYz8kzo2I12gLqgMyFwPdPG8nft9pA22jH5STxI-1Oz-eLBUXA/s16000/largest%20element%20in%20array.png" /></a></div><br /><div><br /></div><div><ul style="text-align: left;"><li>This program first initializes an array a with 10 elements and assigns the value of the first element to the variable max.</li><li>It then loops through the array and compares each element with max. </li><li>If an element is larger than max, max is updated with the new value. After the loop finishes, max will contain the largest element in the array.</li></ul></div>saideshhttp://www.blogger.com/profile/03671387577197113199noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-21521610887063997972023-01-08T00:12:00.006+05:302023-01-08T00:21:12.661+05:30Fibonacci series program in c<p><b style="color: #660000;">Here is a simple program that prints out the first n numbers in the Fibonacci series in C:</b></p><div><div class="program-div"><div class="dp-highlighter"><ol class="dp-j"><li class="alt">#include <stdio.h></li><li class="alt">int main() {</li><li class="alt"> int n, i;</li><li class="alt"> long long int t1 = 0, t2 = 1, nextTerm;</li><li class="alt"><br /></li><li class="alt"> printf("Enter the number of terms: ");</li><li class="alt"> scanf("%d", &n);</li><li class="alt"><br /></li><li class="alt"> printf("Fibonacci Series: ");</li><li class="alt"><br /></li><li class="alt"> for (i = 1; i <= n; ++i) {</li><li class="alt"> printf("%lld, ", t1);</li><li class="alt"> nextTerm = t1 + t2;</li><li class="alt"> t1 = t2;</li><li class="alt"> t2 = nextTerm;</li><li class="alt"> }</li><li class="alt"><br /></li><li class="alt"> return 0;</li><li class="alt">}</li></ol></div></div></div><div>
<br/><ul style="text-align: left;"><li>This program first prompts the user to enter the number of terms in the Fibonacci series to be printed. It then uses a for loop to iterate through the terms and prints each one. The loop starts at 1, and it continues until the value of i is greater than n. </li><li>On each iteration of the loop, the next term in the series is calculated as the sum of the previous two terms, and the variables t1 and t2 are updated to store the previous two terms. The first two terms of the series are initialized to 0 and 1, respectively.</li><li>This program uses a for loop to iterate through the first 20 terms in the Fibonacci series. The variables t1 and t2 represent the previous two terms, and the variable nextTerm represents the next term in the series. </li><li>The program prints out the value of t1 and then updates the values of t1, t2, and nextTerm for the next iteration of the loop.</li></ul></div><div>Here is a C program that prints the first n numbers of the Fibonacci series:</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhaAdEMQdE-p_6fQI3sEihWQjtjL4MUuHVf946VqBl0XALvl0ufWknTTYOyN2sd8cYwADvUEJaGAD3r-NAj-KUVYJR04HIwI9nmybArD-TLEPoxpkvtXD77__IhsPtIDXGGMbmIsMiokFhpR_826QW2sq-gRTvI7yEzQm9_NOQQXkwiREFEgyNL_728pw/s1012/fibonacci.png" style="margin-left: 1em; margin-right: 1em;"><img alt="fibonacci series in c" border="0" data-original-height="596" data-original-width="1012" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhaAdEMQdE-p_6fQI3sEihWQjtjL4MUuHVf946VqBl0XALvl0ufWknTTYOyN2sd8cYwADvUEJaGAD3r-NAj-KUVYJR04HIwI9nmybArD-TLEPoxpkvtXD77__IhsPtIDXGGMbmIsMiokFhpR_826QW2sq-gRTvI7yEzQm9_NOQQXkwiREFEgyNL_728pw/s16000/fibonacci.png" title="fibonacci series in c" /></a></div><br /><div><div>Enter the number of terms: 12</div><div>First 12 terms of Fibonacci series are:</div><div>0</div><div>1</div><div>1</div><div>2</div><div>3</div><div>5</div><div>8</div><div>13</div><div>21</div><div>34</div><div>55</div><div>89</div></div><div><ul style="text-align: left;"><li>This program prompts the user to enter a positive integer n and then generates the first n terms of the Fibonacci series using a loop. with the first two terms being 0 and 1. The series starts with 0 and 1 and looks like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...</li><li>This program prompts the user to enter the number of terms in the Fibonacci series they want to generate, then uses a loop to compute and print out each term. The first two terms of the series are hard-coded as 0 and 1, and the remaining terms are computed by adding the previous two terms together.</li><li>To understand how this program works, you can read the following explanation:</li><li>The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. In this program, the first two numbers of the Fibonacci series are initialized to 0 and 1.</li><li>The for loop is used to iterate over the numbers of the series. The loop starts from 0 and ends at n-1. At each iteration, the next number in the series is calculated by adding the first and second numbers. The if statement is used to handle the case where the current iteration is 0 or 1, in which case the next number is simply the current iteration.</li><li>After the next number is calculated, the first and second numbers are updated for the next iteration. Finally, the next number is printed to the screen.</li><li>I hope this helps! Let me know if you have any questions.</li></ul></div>saideshhttp://www.blogger.com/profile/03671387577197113199noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-55439122068752688722021-07-15T18:57:00.004+05:302021-07-15T18:59:29.553+05:30Introduction to C language<p><b><span style="font-family: "times new roman" , "serif"; font-size: 12pt;">Introduction to C</span></b></p><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><br /></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><b><span style="font-family: "times new roman" , "serif"; font-size: 12pt;"> C </span></b><span style="font-family: "times new roman" , "serif"; font-size: 12pt;">is a general purpose programming language. Programming language is the means to communicate with the system to get our thing done.<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><br /></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , "serif"; font-size: 12pt;"><b>Need for a programming language:</b><o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><br /></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , "serif"; font-size: 12pt;"> We all of us know system can only understand binary representation i.e. in the form of 0's and 1's.<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><br /></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , "serif"; font-size: 12pt;">Suppose we have to program which adds two numbers and save the result.<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><br /></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , "serif"; font-size: 12pt;">Steps involved:<o:p></o:p></span></div><ul type="disc"><li class="MsoNormal"><span style="font-family: "times new roman" , "serif"; font-size: 12pt;">First we should write the values in the memory.<o:p></o:p></span></li><li class="MsoNormal"><span style="font-family: "times new roman" , "serif"; font-size: 12pt;">Move those values into the registers.(registers are also a kind of memory where the CPU can perform operation on the values stored in the registers.)<o:p></o:p></span></li><li class="MsoNormal"><span style="font-family: "times new roman" , "serif"; font-size: 12pt;">Add the values in the registers.<o:p></o:p></span></li><li class="MsoNormal"><span style="font-family: "times new roman" , "serif"; font-size: 12pt;">Save the result in the register to the memory.<o:p></o:p></span></li></ul><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><br /></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , "serif"; font-size: 12pt;"><b>Machine Language </b>At the beginning machine code is used to write the program. <o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><br /></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , "serif"; font-size: 12pt;">Machine level programming consists of binary or hexadecimal instructions on which an processor can respond directly. </span><span face=""arial" , "sans-serif"" style="background: white; color: #222222; font-size: 10pt;"> </span><span style="font-family: "times new roman" , "serif"; font-size: 12pt;"><o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><br /></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><u><span style="font-family: "times new roman" , "serif"; font-size: 12pt;">Structure of machine language instruction.<o:p></o:p></span></u></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , "serif"; font-size: 12pt;">Opcode Operand <o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><br /></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , "serif"; font-size: 12pt;">Opcode represents the operation to be performed by the system on the operands.<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><br /></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , "serif"; font-size: 12pt;">In machine code:<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-size: 12pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: "Times New Roman";">Memory</span><span style="font-family: "times new roman" , "serif"; font-size: 12pt;"> opcode operand <o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , "serif"; font-size: 12pt;">location <o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , "serif"; font-size: 12pt;">0000: 55 e4 10 // 55 represent move instruction, move 10 to location e4 <o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , "serif"; font-size: 12pt;">0001: 89 e5 20<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , "serif"; font-size: 12pt;">0003: 83 e4 e5<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , "serif"; font-size: 12pt;">0006: 54 c4 e4 <o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><br /></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-size: 13.5pt;">Its difficult to read and write opcode for every instruction. So instead of numerical value for instruction there should be an alternative, Then comes the assembly code.<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><br /></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><b><span style="font-size: 13.5pt;">Assembly language:<o:p></o:p></span></b></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><br /></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , serif; font-size: 13.5pt;"> Mnemonics are used to represent the opcode.<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , serif; font-size: 13.5pt;">An Assembly code:<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , serif; font-size: 13.5pt;">MOV A, $20 // move value 20 to register A (MOV is a mnemonics)<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , serif; font-size: 13.5pt;">MOV B, $10 // move value 10 to register B<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , serif; font-size: 13.5pt;">ADD A, B // Add value in register in A and B<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , serif; font-size: 13.5pt;">MOV #30, A // move value in register A to address 30<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><br /></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , serif; font-size: 13.5pt;">Assemblers converts the assembly language into binary instructions.<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;">Assembly language is referred to as low level language,</div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="background: white; color: #252525; font-family: "times new roman" , "serif"; font-size: 13.5pt;">Assembler languages are simpler than opcodes. Their syntax is easier to read than machine language but as the harder to remember.</span><span style="font-family: "times new roman" , serif; font-size: 13.5pt;"> So, their is a need for other language which is much more easy to read and write the code.<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , serif; font-size: 13.5pt;">Then comes the C programming language.<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><br /></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><b><span style="font-family: "times new roman" , serif; font-size: 13.5pt;">C language<o:p></o:p></span></b></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><br /></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , serif; font-size: 13.5pt;">In C language:<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , serif; font-size: 13.5pt;"> int main()<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , serif; font-size: 13.5pt;">{<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , serif; font-size: 13.5pt;"> int a,b,c;<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , serif; font-size: 13.5pt;"> a=10;<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , serif; font-size: 13.5pt;"> b=20;<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , serif; font-size: 13.5pt;"> c=a+b;<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><span style="font-family: "times new roman" , serif; font-size: 13.5pt;">}<o:p></o:p></span></div><div class="MsoNormal" style="margin-bottom: 0.0001pt;"><br /></div><div class="MsoNormal"><span style="font-family: "times new roman" , "serif"; font-size: 13.5pt; line-height: 20.7px;">Compiler converts the C language code into assembly language code.<o:p></o:p></span></div><span style="color: #073763;"></span><br /><div class="MsoNormal"><span style="font-family: "times new roman" , "serif"; font-size: 13.5pt; line-height: 20.7px;">C language is easier to write and read. </span></div>saideshhttp://www.blogger.com/profile/03671387577197113199noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-84113718627349967332021-07-14T21:29:00.005+05:302023-01-10T14:47:19.966+05:30Factorial program in python<p></p><div>#write a python program for factorial of a given number.</div><div><div>num = int(input("Enter a number: "))</div><div>factorial = 1</div><div>if num < 0:</div><div> print("factorial not exist for negative numbers")</div><div>elif num == 0:</div><div> print("The factorial of 0 is 1")</div><div>else:</div><div> for i in range(1,num + 1):</div><div> factorial = factorial*i</div><div> print("The factorial of",num,"is",factorial)</div></div><div><br /></div><div><ul style="text-align: left;"><li>We store the number we give in the num variable and by using the if, elif, and else loop we are executing the program. if statement executes when the given condition is true and same for elif also. After the iteration, it finally returns the factorial of the number we are given.</li><li>Normally we write as 12!=12*11*10*9*8*7*6*5*4*3*2*1=479001600</li></ul><div><br /></div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-9gsH8ZODU8A/YO8JjTGsAzI/AAAAAAAACmA/qVMZh_Tw3dMEYCqHb2xu8M6zZ0B_9brwgCNcBGAsYHQ/s582/Untitled.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="390" data-original-width="582" src="https://1.bp.blogspot.com/-9gsH8ZODU8A/YO8JjTGsAzI/AAAAAAAACmA/qVMZh_Tw3dMEYCqHb2xu8M6zZ0B_9brwgCNcBGAsYHQ/s16000/Untitled.png" /></a></div><div><br /></div><div><br /></div></div><p></p>instanceofjavahttp://www.blogger.com/profile/00460436288881020214noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-52599686305049953372021-07-07T21:46:00.001+05:302021-07-07T21:46:19.159+05:30find substring python<p> <span style="color: red;"><b>find substring():</b></span></p><p></p><ul style="text-align: left;"><li>The find substring is used to find the substring from the string.</li><li>Substring is the piece of characters from the string.</li><li>If the string consists of the given string then it returns the index number of the substring. </li><li>The string must be enclosed with single or double-quotes.</li><li>It returns -1 if the substring does not found instead of an error.</li><li>find() has 3 parameters,they are:</li></ul><div><ol style="text-align: left;"><li><b>value: </b>The value to be search</li><li><b>start: </b> from where to search</li><li><b>end: </b>from where to stop searching.</li></ol><div><b style="color: #800180;">Syntax:</b> str.find(value,start,end)</div></div><div><br /></div><h4 style="text-align: left;">#write a python code to demonstrate the find substring function.</h4><div><ol style="text-align: left;"><li>txt="learning is a life time process"</li><li>txt=txt.find("i")</li><li>print(txt)</li><li>s="apple"</li><li>s=s.find("b")</li><li>print(s)</li></ol><div><br /></div></div><div><b><span style="color: red;">Output</span></b>: 5</div><div> -1</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-OENLKumat58/YOXTDewzujI/AAAAAAAACik/HQSEF9XjdcEAnaIvTmOYx7ru2LuXMCFIgCNcBGAsYHQ/s594/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="314" data-original-width="594" src="https://1.bp.blogspot.com/-OENLKumat58/YOXTDewzujI/AAAAAAAACik/HQSEF9XjdcEAnaIvTmOYx7ru2LuXMCFIgCNcBGAsYHQ/s16000/Untitled.png" /></a></div><br /><div><br /></div><div><br /></div><div><br /></div><div><br /></div><div><br /></div><p></p>instanceofjavahttp://www.blogger.com/profile/00460436288881020214noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-18501629430366295892021-07-07T21:10:00.006+05:302021-07-07T21:10:59.606+05:30python replaceall<p></p><ul style="text-align: left;"><li>In python, replace all is similar to replace() function. </li><li>Here we are going to replace all the characters with new characters.</li><li>It also consists of 3 parameters like new value, old value, count.</li><li>The old value is the string that we have already given in string.</li><li>The new value is the that is to be replaced with.</li><li>The count is the number of times that string to be replaced in the string.</li></ul><div><b style="color: #2b00fe;">Syntax: </b>str.replace(old value,new value,count)</div><div><br /></div><h4 style="text-align: left;">#write a python code to demonstrate the replace function.</h4><div><ol style="text-align: left;"><li>s="hello world"</li><li>s=s.replace("hello world","my name is hello")</li><li>print(s)</li></ol><div><b style="color: #ff00fe;">Output: </b> my name is hello</div></div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-trKjDNGFOVI/YOXK0kQptSI/AAAAAAAACiU/u-AE4GvndAk-uzzLMOw37RLFvipTyoi5gCNcBGAsYHQ/s588/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="252" data-original-width="588" src="https://1.bp.blogspot.com/-trKjDNGFOVI/YOXK0kQptSI/AAAAAAAACiU/u-AE4GvndAk-uzzLMOw37RLFvipTyoi5gCNcBGAsYHQ/s16000/Untitled.png" /></a></div><div><br /></div><p></p>instanceofjavahttp://www.blogger.com/profile/00460436288881020214noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-56948458158903078742021-07-07T19:37:00.001+05:302021-07-07T19:37:26.886+05:30format function in python<p></p><ul style="text-align: left;"><li> The format() function is used to format a specific value and insert that value in string placeholders.</li><li> The placeholders are cost{20}, number index{1},empty{}</li><li>The place holder is defined by" curly braces"{}.</li><li>The format function consists of parameters called value.</li><li>It returns the formatted string.</li></ul><b style="color: #ff00fe;">Syntax: </b>str.format(values)<p></p><p></p><ul style="text-align: left;"><li>Inside the placeholders, we can add different formatting types</li><li><span style="color: red; font-weight: bold;">:+ </span><span>indicates positive values.</span></li><li><span><span style="color: red;"><b style="color: black;"><span style="color: red;">:</span></b> - </span>indicates negative values.</span></li><li><span><b><span style="color: red;">: </span> </b>indicates the space before positive and also negative numbers.</span></li><li><span><span style="color: red;"><b>:_</b> </span>indicates as a thousand separator.</span></li><li><span><span style="color: red;"><b>:,</b></span> uses as comma thousand separator.</span></li><li><span><b><span style="color: red;">:= </span> </b> left most position.</span></li><li><span><b><span style="color: red;">:<</span> </b> result in left alignment.</span></li><li><span><span style="color: red;"><b>:></b></span> result in right alignment.</span></li><li><span><span style="color: red;"><b>:^ </b></span> result in the center.</span></li><li><span><span style="color: red;"><b>:%</b> </span> percentage format.</span></li><li><span><span style="color: red;">:b</span> Binary format.</span></li><li><span><span style="color: red;">:c</span> Unicode format.</span></li><li><span><span style="color: red;">:d</span> Decimal format.</span></li><li><span><span style="color: red;">:e</span> format with lower case.</span></li><li><span><span style="color: red;">:E </span> format with upper case.</span></li><li><span><span style="color: red;">:f </span> format with fixpoint number.</span></li><li><span><span style="color: red;">:o </span> format is octal.</span></li><li><span><span style="color: red;">:x </span> format is Hexa.</span></li><li><span><span style="color: red;">:X </span> format is Hexa in uppercase.</span></li><li><span><span style="color: red;">:n</span> number format.</span></li></ul><h4 style="text-align: left;">#write a python code to demonstrate format function.</h4><div><ol style="text-align: left;"><li>s0="my name is {fname},I m {age}".format("fname=abc",age=19)</li><li>s1="my name is{0},I m {1}".format("abc",19)</li><li>s2="my name is {},I m {}".format("abc",19)</li><li>print(s0)</li><li>print(s1)</li><li>print(s2)</li></ol><div><br /></div><div><b style="color: #2b00fe;">Output: </b>my name is abc, I m 19.</div><div> my name is abc, I m 19.</div><div> my name is abc, I m 19.</div></div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-HXJmhBzBFcM/YOW040K2DiI/AAAAAAAACiM/oeKU9Q1HtEoe3QHMpjlwnAJzTaUlF9NLgCNcBGAsYHQ/s662/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="322" data-original-width="662" src="https://1.bp.blogspot.com/-HXJmhBzBFcM/YOW040K2DiI/AAAAAAAACiM/oeKU9Q1HtEoe3QHMpjlwnAJzTaUlF9NLgCNcBGAsYHQ/s16000/Untitled.png" /></a></div><br />instanceofjavahttp://www.blogger.com/profile/00460436288881020214noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-81484495625958639902021-07-07T18:21:00.000+05:302021-07-07T18:21:06.247+05:30string reverse in python<p></p><ul style="text-align: left;"><li>The reverse string in python is not an in-built function.</li><li>By creating the slice operation we can perform the reverse function.</li><li>In this slice operation, we create the slice that starts with the end of the string and moves backward.</li></ul><b>Example: "</b> abcdefg<b>"</b>[::-1]<p></p><p> gfedcba</p><p><b> <span style="color: red;">right to left indexing: </span> [-7] [-6] [-5] [-4] [-3] [-2] [-1]</b></p><p> <b> a b c d e f g </b></p><p><b><span style="color: red;">left to right indexing: </span> [0] [1] [2] [3] [4] [5] [6]</b></p><p></p><ul style="text-align: left;"><li>In the above example, we are having the string "abcdefg " which we want to reverse. so create a slice that starts with the end of the string and moves backward.</li><li>here it starts with the end index that is indicated with -1 and moves backward as 5,4,3,2,1,0.</li><li>-1 index is g</li><li>5 index is f</li><li>4 index is e</li><li>3 index is d</li><li>2 index is c</li><li>1 index is b</li><li>0 index is a</li><li>so the final output is gfedcba.</li></ul><h4 style="text-align: left;">#write a python code to demonstrate string reverse.</h4><div style="text-align: left;"><ol style="text-align: left;"><li>txt="today climate is very cool"[::-1]</li><li>txt1="hello world"[::-1]</li><li>print(txt)</li><li>print(txt1)</li></ol><div><b style="color: red;">Output: </b>looc yrev si etamilc yadot</div><div> dlrow olleh</div><div><br /></div><div><span style="color: red;"><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-5RY2CSQWGBo/YOWg0AmZ6aI/AAAAAAAACh8/tU45QSGUvN03REHxAdB2ox4E2fFbFPPKACNcBGAsYHQ/s602/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="281" data-original-width="602" src="https://1.bp.blogspot.com/-5RY2CSQWGBo/YOWg0AmZ6aI/AAAAAAAACh8/tU45QSGUvN03REHxAdB2ox4E2fFbFPPKACNcBGAsYHQ/s16000/Untitled.png" /></a></div></span></div></div><div><br /></div><p></p>instanceofjavahttp://www.blogger.com/profile/00460436288881020214noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-33076402624725257122021-07-07T16:05:00.003+05:302021-07-07T16:05:53.091+05:30python tolower<p></p><ul style="text-align: left;"><li> In python, to lower is used to convert the uppercase letters to lower case.</li><li>It uses the function called lower().</li><li>It doesn't have any parameters.</li><li>The string must be enclosed with single or double-quotes.</li><li>If the string does not contain any upper case letters then it returns the same string.</li></ul><b style="color: #cc0000;">Syntax: </b>str.lower()<p></p><p><span style="color: #ffa400;">Example:</span> s="HELLO"</p><p> str.lower(s)=hello</p><h4 style="text-align: left;">#write a python code to demonstrate the lower() function.</h4><div><ol style="text-align: left;"><li>s="APPLE IS A FRUIT"</li><li>print(str.lower(s))</li><li>x="My NAme IS aBCba"</li><li>print(str.lower(x))</li></ol><div><b style="color: #2b00fe;">output:</b> apple is a fruit</div></div><div> my name is abcba</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-soyqgHltETw/YOWDR06lXsI/AAAAAAAACh0/dWYrxuH57jsJggqtjSyPZr3Up9qERf4aQCNcBGAsYHQ/s588/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="297" data-original-width="588" src="https://1.bp.blogspot.com/-soyqgHltETw/YOWDR06lXsI/AAAAAAAACh0/dWYrxuH57jsJggqtjSyPZr3Up9qERf4aQCNcBGAsYHQ/s16000/Untitled.png" /></a></div><div><br /></div>instanceofjavahttp://www.blogger.com/profile/00460436288881020214noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-41419272221822366272021-07-07T15:44:00.000+05:302021-07-07T15:44:00.689+05:30remove spaces from string python<p></p><ul style="text-align: left;"><li> In python, we have different functions which remove the spaces between the strings.</li><li>There are three functions that remove the whitespace between the strings, they are:</li><li><b>strip(), rstrip(), lstrip() </b>are the three functions having the same meaning but some differences.</li></ul><b style="color: red;">strip():</b> It removes the whitespace at the beginning and end.<p></p><p><span style="color: #2b00fe;">syntax: </span>str.strip()</p><p><b style="color: red;">lstrip(): </b>It removes the whitespace at beginning of the string.</p><p><span style="color: #2b00fe;">syntax:</span> str.rstrip()</p><p><b style="color: red;">rstrip(): </b>It removes the whitespace at end of the string.</p><p><span style="color: #2b00fe;">syntax: </span>str.rstrip()</p><p><b>Example: write a python to demonstrate the remove space function.</b></p><p></p><ol style="text-align: left;"><li>txt=" hello"</li><li>print(txt)</li><li>print(txt.strip())</li><li>print(txt.rstrip())</li><li>print(txt.lstrip())</li></ol><div><b style="color: #ff00fe;">Output: </b> hello //this is the original output</div><div> hello //after removing both side space</div><div> hello //no space at end returns same </div><div> hello //remove space at the beginning.</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-RvfqJ-RRcTg/YOV9OR46xTI/AAAAAAAAChs/z38YLGor40w-CtGTYu-Zd8eWSKRONCvzgCNcBGAsYHQ/s593/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="305" data-original-width="593" src="https://1.bp.blogspot.com/-RvfqJ-RRcTg/YOV9OR46xTI/AAAAAAAAChs/z38YLGor40w-CtGTYu-Zd8eWSKRONCvzgCNcBGAsYHQ/s16000/Untitled.png" /></a></div><div><br /></div><p></p>instanceofjavahttp://www.blogger.com/profile/00460436288881020214noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-77808850588302782672021-07-07T13:04:00.000+05:302021-07-07T13:04:04.875+05:30replace function in python<p> <span style="color: #2b00fe;"><b>replace function:</b></span></p><p></p>python having different types of in-built functions from them replace() is one of the function.<br /><ul style="text-align: left;"><li>The replace() function name itself saying that "one string is replaced with the other string".</li><li>It consists of three parameters old value, new value, and count.</li></ul><b style="color: #800180;">old value: </b> The value that we want to replace.<p></p><p><b style="color: #800180;">new value:</b> The value we want to replace with is the new value.</p><p><b style="color: #800180;">count:</b> The number of times of the value we want to replace old with the new value.</p><p><b style="color: red;">Syntax: </b>str.replace(old value,new value,count)</p><h4 style="text-align: left;">#write a python code to demonstrate replace function.</h4><div><ol style="text-align: left;"><li>a="my name is java"</li><li>b=" she is a very talented person"</li><li>c="They have no knowledge in python"</li><li>a=a.replace("java","python")</li><li>b=b.replace("talented person","brave girl")</li><li>c=c.replace("no","more",3)</li><li>print(a)</li><li>print(b)</li><li>print(c)</li></ol><div><ul style="text-align: left;"><li>In the above program in line 5, we want to replace no with more and we gave the count as 3 so in the string where we have no that will be replaced with more up to 3 times only.</li><li>so the output will be as shown in below.</li></ul><div><b style="color: #cc0000;">Output: </b> my name is python</div></div></div><div> she is a very brave girl</div><div> They have more kmorewledge in python </div><div><br /></div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-2hy9LIvn70E/YOVXiTKzssI/AAAAAAAAChk/HnMqVxlkTZoCaPT3mbp1OWjeF-xAd0DQgCNcBGAsYHQ/s594/Untitled.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="367" data-original-width="594" src="https://1.bp.blogspot.com/-2hy9LIvn70E/YOVXiTKzssI/AAAAAAAAChk/HnMqVxlkTZoCaPT3mbp1OWjeF-xAd0DQgCNcBGAsYHQ/s16000/Untitled.png" /></a></div><div><br /></div>instanceofjavahttp://www.blogger.com/profile/00460436288881020214noreply@blogger.com0tag:blogger.com,1999:blog-2964959029992147971.post-88009306998937567492021-07-07T10:46:00.004+05:302023-01-25T23:51:22.112+05:30strip function in python<p></p><div><br /></div><div><div>In Python, the str.strip() method is used to remove leading and trailing whitespaces from a string. By default, the strip() method removes any whitespace characters from the beginning and end of the string, such as spaces, tabs, and newlines. </div><div>The strip() function is a string method that removes leading and trailing whitespace characters from a string. By default, it removes spaces, tabs, and newline characters from the beginning and end of a string. </div><div>However, it can also be used to remove any specified characters from the beginning and end of a string.</div><div>Here's an example of strip() method:</div><div><br /></div><div><div>It's important to note that the strip() function returns a new string and does not modify the original string.</div><div>We can also use lstrip() and rstrip() to remove leading and trailing whitespaces respectively.</div><div>strip() function in Python is a useful method for removing unwanted characters from the beginning and end of a string, which helps in cleaning up the data and making it ready for further processing or analysis.</div></div><div><br /></div><div><b><span style="color: #274e13;">original_string = " Hello World "</span></b></div><div><b><span style="color: #274e13;">stripped_string = original_string.strip()</span></b></div><div><b><span style="color: #274e13;">print(stripped_string)</span></b></div><div><br /></div><div>This will print "Hello World" with leading and trailing whitespaces removed.</div><div><br /></div><div>In addition to removing whitespaces, the strip() method can also remove specific characters from the beginning and end of the string. You can pass one or more characters as arguments to the strip() method, and it will remove those characters from the beginning and end of the string. Here's an example:</div><div><br /></div><div><br /></div><div><span style="color: #274e13;">original_string = "!!Hello World!!"</span></div><div><span style="color: #274e13;">stripped_string = original_string.strip('!')</span></div><div><span style="color: #274e13;">print(stripped_string)</span></div><div><br /></div><div>This will print "Hello World" with the leading and trailing '!' characters removed.</div><div><br /></div><div>Alternatively, you can use lstrip() and rstrip() method to remove leading and trailing whitespaces respectively.</div><div><br /></div><div><br /></div><div><span style="color: #274e13;"><b>original_string = " Hello World "</b></span></div><div><span style="color: #274e13;"><b>stripped_string = original_string.lstrip()</b></span></div><div><span style="color: #274e13;"><b>print(stripped_string)</b></span></div><div><br /></div><div>This will print "Hello World " with leading whitespaces removed.</div><div><br /></div><div><br /></div><div><b><span style="color: #274e13;">original_string = " Hello World "</span></b></div><div><b><span style="color: #274e13;">stripped_string = original_string.rstrip()</span></b></div><div><b><span style="color: #274e13;">print(stripped_string)</span></b></div><div><br /></div><div>This will print " Hello World" with trailing whitespaces removed.</div><div><br /></div><div>It's worth noting that, the strip() method returns a new string with the leading and trailing whitespaces removed, it does not modify the original string.</div></div><ul style="text-align: left;"><li>In python, we are having another in-built function called "<b>strip()</b>".</li><li>The strip() function is used to remove the spaces of the starting and end of the string.</li><li>It is having one parameter character used to remove the begging or end character.</li><li>By default, it removes the white spaces at the beginning or end.</li><li>If there is no white space between the beginning or end, the string remains the same and returns the same string.</li><li>When the character we give does not match the start or end, it returns the same string.</li><li>If the character is given at start to remove then the space will be removed and the remaining will remain the same.</li></ul><div><b style="color: red;">syntax:</b> str. strip(char)</div><div><br /></div><h4 style="text-align: left;">#write a python program to demonstrate the strip function()</h4><div><ol style="text-align: left;"><li>txt1=" my name is python"</li><li>txt2="***my name is python***"</li><li>txt3=",,,my name is python"</li><li>txt1=txt1.strip()</li><li>txt2=txt2.strip("*")</li><li>txt3=txt3.strip(".")</li><li>print(txt1)</li><li>print(txt2)</li><li>print(txt3)</li></ol><div><b style="color: #2b00fe;">Output: </b>my name is python</div></div><div> my name is python</div><div> my name is python</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://1.bp.blogspot.com/-GG1A44RhrDg/YOU4LuCLxvI/AAAAAAAAChc/7muHuiAAxVECgsnKdTcRe09DfAv3XlgcQCNcBGAsYHQ/s592/Untitled.png" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="415" data-original-width="592" src="https://1.bp.blogspot.com/-GG1A44RhrDg/YOU4LuCLxvI/AAAAAAAAChc/7muHuiAAxVECgsnKdTcRe09DfAv3XlgcQCNcBGAsYHQ/s16000/Untitled.png" /></a></div><p></p>instanceofjavahttp://www.blogger.com/profile/00460436288881020214noreply@blogger.com0