Posts

Showing posts from February, 2021

How to add days to date in Java?

Image
import java.util.Date; import java.util.Calendar; import java.text.DateFormat; import java.text.SimpleDateFormat; public class Test{ public static void main(String[]args){ final Date[]dates=new Date[7]; Calendar cal = Calendar.getInstance(); DateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy"); cal.set(Calendar.MONTH, 2); cal.set(Calendar.YEAR, 2021); for(byte day = 26, i = 0; dates.length > i; day++, i++){ cal.set(Calendar.DAY_OF_MONTH, day); dates[i] = cal.getTime(); } for(Date dt : dates){ System.out.println(fmt.format(dt)); } } } Download

Palindrome example with C programming

Image
#include<stdio.h> char lower(char c){ if('A' <= c && 'Z' >= c) return (c + 32); return c; } int alpha(char c){ c = lower(c); return (('a' <= c && 'z' >= c)); } int palindrome(char *str){ char *head = str, *tail = str; while(*tail) tail ++; tail --; while(head <= tail){ if(!alpha(*head)){ head ++; continue; } if(!alpha(*tail)){ tail --; continue; } if(lower(*head) != lower(*tail)) break; head ++; tail --; } return (tail < head); } void main(){ char sentence[200]; printf("Enter a message: "); fflush(stdin); gets(sentence); if(palindrome(sentence)){ printf("Palindrome\n"); return; } printf("Not a palindrome\n"); } Download

How to generate random colors using Java Program?

Image
import java.awt.Color; public class Randomize{ public static void main(String[]args){ System.out.println(randomColor()); System.out.println(randomColor()); } public static Color randomColor(){ return new Color( randomInRange(0,255), randomInRange(0,255), randomInRange(0,255) ); } public static int randomInRange(int a, int b){ return (int)(a + (Math.random()*(b-a))); } } Download

How to compute retail price with Java Swing?

Image
import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.SwingUtilities; import java.awt.Container; import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class RetailPrice{ public static void main(String[]args){ SwingUtilities.invokeLater(createGUI()); } private static Runnable createGUI(){ return new Runnable(){ @Override public void run(){ JFrame f = new JFrame("Retail Price Calculator"); Container pane = f.getContentPane(); pane.setLayout(new FlowLayout()); JTextField wholesaleTF = new JTextField(10); pane.add(new JLabel("Wholesale $")); pane.add(wholesaleTF); JTextField markupTF = new JTextField(2); pane.add(new JLabel("Markup %")); pane.add(markupTF); JTextField retailTF = new JTextField(10); pane.add(new JLabel("Retail $")); pane.add(retailTF);

How to print reverse string in Java recursively?

Image
public class Reverse{ public static void main(String[]args){ doubleReverse("Java"); } public static void doubleReverse(String s){ int l = 0; if(null == s || 0 == (l = s.length())) return; char c = s.charAt(--l); System.out.printf("%c%c", c, c); s = s.substring(0,l); doubleReverse(s); } } Download

How to find unique words using python programming?

Image
words = list() print('Enter words:') while True: word = input().strip().lower() if 0 == len(word): break if word not in words: words.append(word) if words: print('Unique words given:') for word in words: print(word) else: print('No words given') Download