Enhance your programming skills from Scratch

shalini_goel14
@shalini-goel14-ASmC2J β€’ Oct 26, 2024

Hi All CEans ,

So you guys don't get enough time to try your hands on programming? No tensions anymore, you will be given a chance here to do simple programming and in return get a surprise every month if your code will be liked.

Rules:
1. No sms language will be allowed
2. You can write your program in any language but expect answers of questions asked here in Java only.
3. Your program should be well compiled and executed.
4. Your program should be well-documented.
5. Your program should be enclosed within CODE tags.
6. Output should also be shown along with your program.
7. Don't make the thread dead.

You will get a surprise after every month.

In the starting programs will given here will be of Level1. Gradually its Level will be increased depending on the response of all CEans here.

Example
Sample Program: Write a Program that prints "Hello World" in console.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package myjava;

/**
 *
 * @author shalinig
 */
public class HelloWorldProgram {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("Hello World ");
    }

}

Ouput:

Hello World

Hope you all will enjoy here. πŸ˜€

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 14, 2009

    So strating this thread from a very simple program.πŸ˜€

    Program # 1 : Write a simple program that accepts two numbers from user and print them on console.

    All the best to all.

  • Kaustubh Katdare

    @thebigk Feb 14, 2009

    Sorry to interrupt: Is C/C++/PERL/PYTHON allowed? I can do the Program #1 πŸ˜‰

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 15, 2009

    The_Big_KSorry to interrupt: Is C/C++/PERL/PYTHON allowed? I can do the Program #1 πŸ˜‰

    @Big_K According to rule no 2, yes allowed. It will be really appreciating if you can write Program # 1 in all 4 languages mentioned by you but yes in 4 different posts.
    PS: You need to go for Java class. These languages are too outdated.πŸ˜‰


    All CEans,
    If you know more than 1 language Please do write the same program in all of those languages. It will be really very helpful to other languages programmers here and will be appreciated lot. Just remember the rules mentioned in my first post. πŸ˜€

  • raj87verma88

    @raj87verma88-ZpL2Wn Feb 15, 2009

    I am not a master of computer languages but I know C and assembly. That is, if Shalini counts them in the category of languages πŸ˜›

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 15, 2009

    raj87verma88I am not a master of computer languages but I know C and assembly. That is, if Shalini counts them in the category of languages πŸ˜›

    Yes raj, every language is allowed here if it is possible for you to make that program in your known language.

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 15, 2009

    silverscorpionOutdated? no way., every language has it's own pros and cons.
    I cant accept C as outdated compared to java, because, I don't know Java yet, but I know C quite well..😁😁

    Don't mind Scorpion if you know C well then why are you taking so much time to write a simple program?Tell?

    PS: Don't mind CEans who all know C. I have a question for you. Where were you all when I started following thread? Tell all C users (Big_K, raj, Scorpion and many such...) here hmm?

    #-Link-Snipped-#

  • silverscorpion

    @silverscorpion-iJKtdQ Feb 15, 2009

    shalini_goel14Program # 1 : Write a simple program that accepts two numbers from user and print them on console.

    Well, here goes my answer in C.

    #include<stdio.h>
    main()
    {
    int a,b;                                                         /*Declare variables*/
    char c;                
    printf("Enter the first number\n");        
    scanf("%d",&a);                                             /*input the first number*/
    printf("Enter the second number\n");
    scanf("%d",&b);                                             /*input second number*/
    printf("Do you want to print the numbers?(y/n)");/*asks whether to print or not*/
    scanf("%c",&c);
    if(c=='y' || c=='Y')
    printf("%d\n%d", a,b);                                    /*prints the numbers*/
    }


    And the ouptut is:

    Enter the first number
    5
    Enter the second number
    3
    Do you want to print the numbers?(y/n)
    y

    5
    3

  • raj87verma88

    @raj87verma88-ZpL2Wn Feb 15, 2009

    Because I have already mentioned that I am not a master of computer languages. Just crammed enough C to clear the exams. After the 3rd semester, we have no application of C in the mechanical course.

  • rohit330

    @rohit330-DvfKyG Feb 17, 2009

    //Program to accept two numbers and print the same 
    
    #include<iostream.h>
    #include<conio.h>
    void main()
    {
    clrscr(); // clear screen
    int a,b;  // declaration
    cout<<"ENTER TWO NUMBERS: "; 
    cin>>a>>b;
    cout<<"\n"; // new line
    cout<<a<<b;
    getch();
    }
    
    [U]output[/U]
    
    ENTER TWO NUMBERS: 3 2
    
    3 2
    
  • rohit330

    @rohit330-DvfKyG Feb 17, 2009

    //Program to accept two numbers and print the same
    
    #include<stdio.h>
    #include<cono.h>
    void main()
    {
    int a,b;  // declaration
    clrscr(); //clear screen
    printf("ENTER TWO NUMBERS: ");
    scanf("%d%d",&a,&b);
    printf("\n%d %d",a,b);
    getch();
    }
    
    
    [U]output[/U]
    
    ENTER TWO NUMBERS: 3 2
    
    3 2
    
  • Raviteja.g

    @ravitejag-02nJVr Feb 17, 2009

    import java.io.*;
    class first
    {
    public static void main(String args[ ])
    {
    InputStreamReader isr=new Inputstreamreader(System.in);
    BufferedReader br=new BufferedReader(isr);
    System.out.println("enter two numbers");
    int a=Integer.parseint(br.readLine( ));
    int b=Integer.parseint(br.readLine( ));
    System.out.println("if u want to print the numbers? (y/n)");
    String option=br.readLine( );
    if(option==y)
    System.out.println("the numbers are     "+a+"   "+b); 
    }
    }
    
    
    /*output is
    enter two numbers
    2
    3
    if u want to print the numbers? (y/n)
    y
    the numbers are     2   3
    */
    
    
  • komputergeek

    @komputergeek-Yf5hJ4 Feb 17, 2009

    code in python :

    print "Enter two numbers :"
    a=input()
    b=input()
    print "You entered '" , a , "' and '" , b ,"'"

    output :

    Enter two numbers :
    1
    2
    You entered ' 1 ' and ' 2 '
  • Ashraf HZ

    @Ash Feb 17, 2009

    Marvel the beauty of Python.

  • safwan

    @safwan-NH7W5Y Feb 17, 2009

    shalini_goel14Hi All CEans ,

    So you guys don't get enough time to try your hands on programming? No tensions anymore, you will be given a chance here to do simple programming and in return get a surprise every month if your code will be liked.

    Rules:
    1. No sms language will be allowed
    2. You can write your program in any language but expect answers of questions asked here in Java only.
    3. Your program should be well compiled and executed.
    4. Your program should be well-documented.
    5. Your program should be enclosed within CODE tags.
    6. Output should also be shown along with your program.
    7. Don't make the thread dead.

    You will get a surprise after every month.

    In the starting programs will given here will be of Level1. Gradually its Level will be increased depending on the response of all CEans here.

    Example
    Program #1: Write a Program that prints "Hello World" in console.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package myjava;
     
    /**
     *
     * @author shalinig
     */
    public class HelloWorldProgram {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            System.out.println("Hello World ");
        }
     
    }
    
    Ouput:
    Hope you all will enjoy here. πŸ˜€

    your first program to print message "Hello World"
    its here in Visual Basic6.0

    Private Sub Form_Load()
    MsgBox ("hello World")
    End Sub

    and out put will be :

    in a form
    "hello World"

  • Joe1717

    @joe1717-OzHtWn Feb 18, 2009

    great idea!!

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 18, 2009

    Well done Scorpion, Rohit,Raviteja,Komputegeek, safwan. I am proud of you guys. 😁. Keep it up. Please if you know any other languages do write your program in that also.


    Joe1717great idea!!

    Thanks for good words Joe but where are your programs? πŸ˜‰

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 19, 2009

    @safwan : Write the code for following also πŸ˜€
    Program # 1 : Write a simple program that accepts two numbers from user and print them on console.

    Here goes the second program:

    Program # 2 Write a simple program(in any computer language) that accepts a number from the user and prints "This is an even number" if the number is even and "This is not an even number" if the number is not even.

    All the best to all. Please do mention your name closed in comments at the top of your program.See example shown in first post of this thread. πŸ˜€

  • komputergeek

    @komputergeek-Yf5hJ4 Feb 19, 2009

    """
    Program : Write a simple program(in any computer language) that accepts a 
    number from the user and prints "This is an even number" if the number is even 
    and "This is not an even number" if the number is not even.
    Language : Python
    Author : komputergeek
    """
    print "Enter number :"
    num=input()
    if num % 2 == 0 :
        print "Number is even"
    else:
        print "Number is odd"
    
    """
    Output 1:
    Enter number :
    43
    Number is odd
    
    
    Output 2:
    Enter number :
    46
    Number is even
    
    """
    
  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 19, 2009

    Hi All CEans,

    Imp Notice: Please don't forget to mention the language you are using for writing your program along with it.Thanks

    @Komputergeek You are doing good job here man. I liked your regularity here. πŸ˜€

  • komputergeek

    @komputergeek-Yf5hJ4 Feb 19, 2009

    Thanx shalini :smile:

    /*
    Program : Write a simple program(in any computer language) that accepts a 
    number from the user and prints "This is an even number" if the number is even 
    and "This is not an even number" if the number is not even.
    Language : Java
    Author : komputergeek
    */
    
    import java.util.Scanner;
    
    public class EvenNumber
    {    
        public static void main(String args[])
        {
            int num;               //declaration
            Scanner sc=new Scanner(System.in); //create object of scanner class to read input from user
            System.out.print("Enter number : ");
            num=sc.nextInt();               //read int
            if((num % 2)==0)
                System.out.println("Number is even");
            else
                System.out.println("Number is odd");
        }
    }
    
    /*
    Output 1 :
    
    Enter number : 54
    Number is even
    
    
    Output 2 :
    
    Enter number : 67
    Number is odd
    */
    
    
  • safwan

    @safwan-NH7W5Y Feb 19, 2009

    hi all
    @ shallini your given second program is here

    To check wherther given no. is even or not

    Private Sub cmdcheck_Click()
    Dim intnum1 As Integer
    'declaration of variable
    If intnum1 Mod 2 = 0 Then
    Print "Given Number is even"
    Else
    Print "given number is Odd"
    End If
    'checking the variables

    End Sub

    out put is :

    2

    when user will press check button it will print "Given Number is even"
    if user enters 3 then it will print on form "given number is Odd"


    i have used visual basic 6.0
    and i know only two language
    SQL and Basic

  • rohit330

    @rohit330-DvfKyG Feb 19, 2009

    /*Program to check whether the given number is even or not (C++)*/
    
    #include<iostream.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int n;
    cout<<"Enter the number:";
    cin>>n;
    if(n%2==0)
    cout<<"This is an even number";
    else
    cout<<"This is not an even number";
    getch();
    }
    
    [U]OUTPUT[/U]
    
    Enter the number:2
    This is an even number
    
    Enter the number:3
    This is not an even number
    
    
  • rohit330

    @rohit330-DvfKyG Feb 19, 2009

    /*Program to check whether the given number is even or not (C)*/
    
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int n;
    clrscr();
    printf("Enter the number: ");
    scanf("%d",&n);
    if(n%2==0)
    printf("This is an even number");
    else
    printf("This is not an even number");
    getch();
    }
    
    
    [U]OUTPUT[/U]
    
    Enter the number:2
    This is an even number
    
    Enter the number:3
    This is not an even number
    
  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 22, 2009

    Program # 3 Write a program that check whether the given number is prime number or not. If the number is not a number, it should print desired output. Like if I input a charcater, it should print appropriate message at Console.Overall Your program should work for each and every kind of input appropriately.

    Note: Please keep on writing here codes for other Program # 1 and Program # 2 if you know more languages. πŸ˜€

    PS: At the end of month all the programs by an individual will be considered, not any single one.OK

  • komputergeek

    @komputergeek-Yf5hJ4 Feb 22, 2009

    """
    Program : Write a program that check whether the given number is prime number or not. If the number is
     not a number, it should print desired output. Like if I input a charcater, it should print appropriate message 
    at Console.Overall Your program should work for each and every kind of input appropriately.
    Language : Python
    Author : komputergeek
    """
    print "Enter number :"
    num=input()    #Take input from user 
    flag=True
    if num < 0 :
        num = num * -1 ;    # If number is negative,make it possitive so that 
                    #program will produce correct output for -ve numbers
    for i in range(2,num):
        if (num % i == 0):        #Check whether number is divisible by any other number
            flag=False        #if yes make flag false
            break
    if num < 2 :
        flag=False
    if flag == True :
        print "Number is prime"
    else :
        print "Number is not prime"
    """
    Output 1:
    Enter number :
    68
    Number is not prime
    
    
    Output 2:
    Enter number :
    67
    Number is prime
    
    """
  • komputergeek

    @komputergeek-Yf5hJ4 Feb 22, 2009

    /*
    Program : Write a program that check whether the given number is prime number or
    not. If the number is not a number, it should print desired output. Like if I input a
    charcater, it should print appropriate message at Console.Overall Your program should
    work for each and every kind of input appropriately.
    Language : Java
    Author : komputergeek
    */
    
    import java.util.Scanner;
    
    public class PrimeNumber
    {    
        public static void main(String args[])
        {
            int num;               //declaration
            boolean flag=true;
            Scanner sc=new Scanner(System.in); //create object of scanner class to read
                                                   //input from user
            System.out.print("Enter number : ");
            num=sc.nextInt();               //read int
            if (num<0)
                num = num * -1;        //If number is negative,make it possitive so that 
                        //program will produce correct output for -ve numbers
            for(int i=2;i<num;i++)
            {    
                    if((num % i) == 0)    //Check whether number is divisible by any other number
                    {    
                       flag = false;        //if yes make flag false
                       break;    
                    }
            }
            if (num < 2)
                flag=false;
            if (flag == true)
                System.out.println("Number is prime");
            else    
                System.out.println("Number is not prime");
        }
    }
    
    /*
    Output 1 :
    
    Enter number : 39
    Number is not prime
    
    
    Output 2 :
    
    Enter number : 37
    Number is prime
    
    */
  • silverscorpion

    @silverscorpion-iJKtdQ Feb 22, 2009

    This is not the full program right? What if the input is an alphabet?
    And what if negative numbers are given? Negative prime numbers are still prime, isn't it?
    I'll give my program soon..

  • komputergeek

    @komputergeek-Yf5hJ4 Feb 22, 2009

    What should be the output for alphabets?

  • komputergeek

    @komputergeek-Yf5hJ4 Feb 22, 2009

    I have modified both the programs.Now both are working for negative numbers.

  • vaibhav agrawal

    @vaibhav-agrawal-Kp9syZ Feb 23, 2009

    shalini_goel14So strating this thread from a very simple program.πŸ˜€

    Program # 1 : Write a simple program that accepts two numbers from user and print them on console.

    All the best to all.

    i am not very good in language but i am trying to give your question's answer.
    wiil it be ok? if i write in c language............

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a,b;
    printf("enter any two no.\n");
    scanf("%d%d",&a,&b);
    printf("your entered no are\na=%d and b=%d",a,b);
    getch();
    }

    output:
    enter any two no.
    5 6
    your entered no. are a=5 and b=6

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 23, 2009

    Well done vaibhav πŸ˜€
    1. Please use CODE tags symbol(#) given above to enclose your program in CODE block.
    2. Don't forget to mention the author's name in your program.
    3. Other programs (I mean Program # 2 and Program # 3 ) are also waiting for your code.
    4. If you know any other language. Please find out some time to write your program using them also.

    😁

    Thanks

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 26, 2009

    [Reminder]
    Guys, you all have 2 more days to submit your code here for Program #1, Program # 2 and Program #3 and then finally a surprise for someone(one who has written best code here till 28th Feb) 😁.

    So then what are you all waiting for? Go ahead and get a surprise. 😁

  • CEMember

    @cemember-PlqlWH Feb 26, 2009

    Hi Shalini,

    Following is my program in Java πŸ˜€

    Program # 1 : Write a simple program that accepts two numbers from user and print them on console.

    /*
     * Program1Class.java
     *
     * Created on February 27, 2009, 11:01 AM
     */
    package myjava;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    /**
     *
     * @author cemember
     */
    public class Program1Class {
        
       public static void main(String[] args){
           
          Integer num1, num2=null;
          BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
           try {
                System.out.println("Enter number 1:");
                num1=Integer.parseInt(br.readLine());
                System.out.println("Enter number 2:");
                num2=Integer.parseInt(br.readLine());
                
                System.out.println("Number1= "+num1+" and Number2= "+num2);
            } catch (IOException ex) {
                System.out.println("An IO Exception has occured");  
            }
       }
        
    }
    
    

    Output is:

    Enter number 1:
    32
    Enter number 2:
    51
    Number1= 32 and Number2= 51

    Please check if any corrections is required πŸ˜€

  • CEMember

    @cemember-PlqlWH Feb 26, 2009

    Shalini, here is my Program # 2 in Java only

    Program # 2 Write a simple program(in any computer language) that accepts a number from the user and prints "This is an even number" if the number is even and "This is not an even number" if the number is not even.

    /*
     * Program2Class.java
     *
     * Created on February 27, 2009, 11:20 AM
     */
    
    package myjava;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    /**
     *
     * @author cemember
     */
    public class Program2Class {
        
        public static void main(String arg[]){
            
            String enteredNum=null;
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter any value ");
            try {
                enteredNum= br.readLine();
                Integer enteredNumInInt=Integer.parseInt(enteredNum);
                if(Math.abs(enteredNumInInt)%2==0){
                    System.out.println("This is an even number");
                }
                else{
                      System.out.println("This is not an even number");
                }
            } catch (IOException ex) {
                System.out.println("An IO Exception has occured");          
            }catch (NumberFormatException ex) {
                System.out.println("This is an invalid value");
               
            }
            
        }
        
    }
    

    Ouput:
    Case 1: When entered number is an even number

    Enter any value
    0
    This is an even number

    Case 2: When entered number is not an even number

    Enter any value
    73
    This is not an even number

    Case 3: When entered value is not a number

    Enter any value
    a
    This is an invalid value

    Please check if any corrections are required. πŸ˜€

  • CEMember

    @cemember-PlqlWH Feb 26, 2009

    Shalini, here is my Program # 3 in Java only.

    Program # 3 Write a program that check whether the given number is prime number or not. If the number is not a number, it should print desired output. Like if I input a charcater, it should print appropriate message at Console.Overall Your program should work for each and every kind of input appropriately.

    /*
     * Program3Class.java
     *
     * Created on February 27, 2009, 11:33 AM
     *
     */
    package myjava;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    /**
     *
     * @author cemember
     */
    public class Program3Class {
        
        public static void main(String arg[]){
            
            String enteredNum=null;
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Enter any value ");
            try {
                enteredNum= br.readLine();
                Integer enteredNumInInt=Integer.parseInt(enteredNum);
                Integer numberToCheck =Math.abs(enteredNumInInt);
                
                /* static boolean expression evaluated outside 'for' loop */
                boolean staticexpr=false;
                /* dynamic boolean expression evaluated inside of 'for' loop */
                boolean dynamicexpr=true;
                
                /* Checks if the number is 1 */
                if(numberToCheck==1){
                    System.out.println("1 is neither a composite number nor a prime number");
                } else if(numberToCheck > 1){
                    
                    
                    /* checks if number is divisble by itself and 1 */
                    staticexpr=(numberToCheck%numberToCheck==0)&&(numberToCheck%1==0);
                    
                   /* loop that checks whether the number is
                    * divisible by all the numbers less than it
                    * excluding itself and 1
                    */                
                    for(int i=(numberToCheck-1);(i!=0 && i!=1 && i<numberToCheck); i--){
                        
                        if(numberToCheck%i==0){
                            dynamicexpr=false;
                      //if numberToCheck is divisible by any number less than numberToCheck
                            break;
                        }
                        
                    }
                    if(staticexpr&&dynamicexpr){
                        System.out.println("This is a prime number");
                    } else{
                        System.out.println("This is not a prime number");
                    }
                } else{
                    System.out.println("0 is not a prime number");
                }
            } catch (IOException ex) {
                System.out.println("An IO Exception has occured");
            }catch (NumberFormatException ex) {
                System.out.println("This is an invalid value");
            }
            
        }
        
    }
    

    Output:

    Case 1: When entered value is not a number

    Enter any value
    a
    This is an invalid value

    Case 2: When the number is 0

    Enter any value
    0
    0 is not a prime number

    Case 3: When the number is 1

    Enter any value
    1
    1 is neither a composite number nor a prime number

    Case 4: When the number is a prime number

    Enter any value
    17
    This is a prime number

    Case 5: When the number is not a prime number

    Enter any value
    57
    This is not a prime number

    Please check if it fails for any type of input or need any corrections.πŸ˜€

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 26, 2009

    Hey Good job CEMember ! 😁 Keep it up

    CEMember if you know any other languages, do write your programs in them also.

    All CEans, Where are you? Write only 3 simple programs. 2 days left. Come on get a surprise. 😁

    [ PS: All the programs will be checked whether they are running or not.]

  • Mayur Pathak

    @mayur-ywQKfu Feb 26, 2009

    This is a good initiative, although may be I'm commenting too late.

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 26, 2009

    mayurpathakThis is a good initiative, although may be I'm commenting too late.

    Thank you very much Sir 😁

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 28, 2009

    Notice for all CEans,

    Guys few more hours left for you and someone tomorrow will get a surprise for the programs written. This surprise is for month of Feb only. Coming months will have more new programs and more surprises. 😁

    So what are you all waiting here, Please write program # 1, Program #2, Program #3 and get a surprise. 😁 So easy to get right? πŸ˜‰

  • silverscorpion

    @silverscorpion-iJKtdQ Feb 28, 2009

    All rightie.. Here are my programs for all the three questions, all three in C++.

    Program # 1 : Write a simple program that accepts two numbers from user and print them on console.

    #include<iostream.h>
    void main()
    {
    int a,b;                            [COLOR=Red]// Declare two integers[/COLOR]
    cin >> a >> b;                     [COLOR=Red]// Get input from user[/COLOR]
    cout << a << endl << b;            [COLOR=Red]// print them[/COLOR]
    }
    [B]OUTPUT:[/B] 
    5 3
    5
    3



    Program # 2 Write a simple program(in any computer language) that accepts a number from the user and prints "This is an even number" if the number is even and "This is not an even number" if the number is not even.

    #include<iostream.h>
    void main()
    {
    int a;
    cin >> a;                                           [COLOR=Red]//Get two integers from user[/COLOR]
    if(a%2==0)                                         [COLOR=Red]//Check if it's divisible by two[/COLOR]
    cout << "This is an even number";                 [COLOR=Red]// Print if it's even or odd number[/COLOR]
    else
    cout << "This is not an even number;
    }
    OUTPUT:
    5
    This is an not an even number
    10
    This is an even number

    Program # 3 Write a program that check whether the given number is prime number or not. If the number is not a number, it should print desired output. Like if I input a charcater, it should print appropriate message at Console.Overall Your program should work for each and every kind of input appropriately.

    #include<iostream.h>
    void main()
    {
    int a,flag=0;
    cin >> a;                                              [COLOR=Red]//get the number[/COLOR]
    if(cin.fail())                                         [COLOR=Red]//check if the input is a number[/COLOR]
    {
    cout << "The input is not a number";                  [COLOR=Red]// print an appropriate message[/COLOR]
    exit(0);                                                   [COLOR=Red]//exit!![/COLOR]
    }
    if(a<0) a= -1*a;
    if(a==0)
    {
    cout << "The number is not a prime number";       [COLOR=Red]//if the number is 0 or 1, print corresponding msg[/COLOR]
    exit(0);
    }
    if(a==1)
    cout << "The number is neither prime, nor composite";
    else
    {
    for (i=2; i<=a/2; i++)
    {
    if(a%i==0)
    {                                                [COLOR=Red]//Checking if the number is prime(in the for loop)[/COLOR]
    flag=1;
    cout << "The number is not a prime number";
    exit(0)
    }
    }
    }
    if(flag==0)
    cout << "The number is a prime number";               [COLOR=Red]//and the job is done!! Voila![/COLOR]
    }
    OUTPUT
    a
    The input is not a number
    23
    The number is a prime number
    25
    The number is not a prime number

    Are these correct?? 😁😁

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 28, 2009

    Good job Scorpion. Keep it up .😁

    I am totally confused to select one person over here for giving the surprise πŸ˜”.

    I am impressed with you all 😁. I wish I can give the surprise to all. πŸ˜‰

  • Yamini L

    @yamini-uMEVDQ Feb 28, 2009

    Now i will right all these 3 programs in C....

    Program # 1 : Write a simple program that accepts two numbers from user and print them on console.



    #include<stdio.h>
    void main()
    {
    int a;
    printf("Enter two numbers\n ");//get the number from user
    scanf("%d%d\n",&a,&b);
    printf("The numbers are %d %d",a,b);//display it 
    }
    


    Output

    Enter two numbers
    5 6
    The numbers are 5 6

    Program # 2 Write a simple program(in any computer language) that accepts a number from the user and prints "This is an even number" if the number is even and "This is not an even number" if the number is not even.

    #include<stdio.h>
    void main()
    {
    int num;
    printf("Enter the number\n");//get the number
    scanf("%d\n",&num);
    if(num%2==0) //check whether its even or odd
    {
    printf("This is an even number ");
    }
    else
    {
    printf("This is not an even number");
    }
    }
     
    

    output

    Enter the number
    10
    This is an even number

    Enter the number
    5
    This is not an even number


    Program # 3 Write a program that check whether the given number is prime number or not. If the number is not a number, it should print desired output. Like if I input a charcater, it should print appropriate message at Console.Overall Your program should work for each and every kind of input appropriately.

    #include<stdio.h>
        #include<math.h>
      void  main()
        {
          int i,n,rem,sq;
            printf("enter the number ");
              scanf("%d",&n);
                sq=sqrt(n);
                  for(i=2;i<=sq;i++)
                    {
                         rem=n%i;
                              if(rem==0)
                          {
           printf("given number is not prime");
                  exit(0);
                       }
                         }
                           printf("given number is prime");
                           }
     
    

    output


    enter the number 5
    given number is prime

    enter the number 8
    given number is not prime
  • safwan

    @safwan-NH7W5Y Feb 28, 2009

    hay wait you are getting so fast i am posting my last programm within an hour .

  • zia.sepsis

    @ziasepsis-0NGPzn Feb 28, 2009

    Am too late to enter into this competition πŸ˜”

  • safwan

    @safwan-NH7W5Y Feb 28, 2009

    sorry unable to find how to check wheather user has enterd number or charecter .
    programe no. #3 so if any one knows in vb than share it.

  • shalini_goel14

    @shalini-goel14-ASmC2J Feb 28, 2009

    Firstly I am really impressed with you all devoting your efforts and time for this thread. 😁 Thanks to everyone from my side.😁

    It was really very difficult for me to decide a single person for the "surprise" but no tensions, those who could not get there is a reason behind it. If you want to know the problem in your program , Feel free to ask here or by PM ok.

    Well here goes the summary of who did what in this thread:

    Program # 1
    Scorpion -C,C++
    Rohit- C++
    Raviteja- Java
    komputergeek -Pyhon
    safwan -vb(but not Program #1 rather example program)
    vaibhav agarwal -C
    CEMember -Java
    miniy -C

    Program # 2
    komputergeek -python, java
    safwan -vb,
    Rohit - C++, C
    CEMember -Java
    Scorpion -C++
    miniy -C

    Program # 3
    komputergeek -python
    CEMember -Java
    SCcrpion -C++
    miniy -C

    I appreciated programs of everyone here. Being of not CS branch or being only a 12th standard student or being not so much regular member here, you all showed very good efforts. Hats off to you all but still I could not resist my self to give the surprise to "CEMember" who showed great neatness in his programs and showed outputs for wide no of cases.


    @CEMember Check your Inbox for the surprise 😁

    @Scorpion I liked your Program # 3. A "Dairy Milk" from my side for you. You can send me the bill. πŸ˜‰

    @komputergeek I liked your diversity shown towards programs writing in Java as well as Python. A "Dairy Milk" from my side for you also. You can also send me the bill. πŸ˜‰

    PS: Competition is not yet over. Every month I will give you chance and more the complication involved in the program, more better will be the surprise.

    Guys Look at him CEMember, he took the surprise. Common beat him next time in writing programs. Write much better than him and get the surprise next time. πŸ˜›

    PS I: Do let me know if anyone has any issues with my decision. πŸ˜€
    PS II: No Congratulations or Thanks or other similar posts in this thread. Use either visitor's message or PM facility to do so πŸ˜€

  • shalini_goel14

    @shalini-goel14-ASmC2J Mar 1, 2009

    ok Guys, here goew next program starting the month of march.

    Program # 4 Write a program that prints the reverse of the entered string or number in console. If entered value consists of single character, it shoudl display same value in output.

    e.g If I enter "Shalini" it prints "inilahS" and if I enter "123" it prints "321" and if I enter "a" or "1" int prints same "a" or "1" in output.

    All the best for writing best program. πŸ˜€

  • silverscorpion

    @silverscorpion-iJKtdQ Mar 1, 2009

    Does the case matter? ie, if you give "Shalini" as inout, should it print "inilahS" or is "inilahs" fine?
    And is spaces included in the input??

  • shalini_goel14

    @shalini-goel14-ASmC2J Mar 1, 2009

    silverscorpionDoes the case matter? ie, if you give "Shalini" as inout, should it print "inilahS" or is "inilahs" fine?
    And is spaces included in the input??

    Very good question-case matters and space is also allowed. πŸ˜€

  • Yamini L

    @yamini-uMEVDQ Mar 2, 2009

    Program # 4 Write a program that prints the reverse of the entered string or number in console. If entered value consists of single character, it shoudl display same value in output.

    Here is my code for the fourth program..
    PROGRAMMING LANGUAGE USED:C++

    Here,i have displayed a menu..User can either select string or number from the options displayed and can get its reverse printed on the screen....

    #include<iostream.h>
    #include<string.h>
    #include<conio.h>
    void reverse();//function declaration for finding reverse of a number 
    void revstring();//function declaration for finding reverse of string
    void main()
    {
    int n;
    char ch;
    clrscr();
    label:cout<<"\n\n\a\a********************MENU********************\a\a\n\n";        //display menu for the user to select one
    cout<<"1.String\n";
    cout<<"2.Number\n";
    cout<<"\nSelect any one option to print the reverse\n";
    cin>>n;
    switch(n)
    {
    case 1:
    revstring();          //String Reverse
    goto label;
     
    case 2:
    reverse();        //Number Reverese
    goto label;
     
    default:
    cout<<"\nEnter a valid option\n";                                  //default case if invalid option is selected
    cout<<"\nProgram Terminated\n";
    getch();
    }
    }
     
    void reverse()
    {
    long num,s=0,r;
    cout<<"\nEnter the number\n";                                    //get the number from user
    cin>>num;
    while(num>0)
    {
    r=num%10;
    s=(s*10)+ r;                                                               //find the reverse of number
    num=num/10;
    }
    cout<<"The reverse of the number is"<<" "<<s<<endl;     //print the reverse
    }
     
    void revstring()
    {
    int l=0,i,j;
    char c,str[120],temp[120];
    cout<<"enter the string to reverse:";                   //get the string from user
    cin.clear();                                                       //clear the error bits for the cin input stream
    cin.sync();                                                       //synchronize the input buffer, discarding any leftover characters in the buffer 
    cin.getline(str,120);
    while(str[l])                                                   //find length of string
    {l++;
    }
    cout<<"\nlength of the string is"<<l<<endl;        //Display string length
    j=l;
    for (i=0;j>=0;i++,j--)
    {
    temp[i]=str[j];                                            //reverse string(using another variable)
    }
    cout<<"Reversed string is"<<" ";                  //display reversed string
    for (i=0;i<=l;i++)
    {
    cout<<temp[i];
    }
    cout<<endl<<endl;
    }
    

    OUTPUT

    ********************MENU********************
    1.String
    2.Number
    Select any one option to print the reverse //select string
    1
    enter the string to reverse:a // character input
    length of the string is1
    Reversed string is a

    ********************MENU********************
    1.String
    2.Number
    Select any one option to print the reverse //select string
    1
    enter the string to reverse:YamIni //input with upper and lower cases
    length of the string is6
    Reversed string is inImaY

    ********************MENU********************
    1.String
    2.Number
    Select any one option to print the reverse //select string
    1
    enter the string to reverse:YamInI LakshmIPATHy //input with space
    length of the string is19
    Reversed string is yHTAPImhskaL InImaY

    ********************MENU********************
    1.String
    2.Number
    Select any one option to print the reverse //select string
    1
    enter the string to reverse:miniy is a CRAZY Computer Science ENGINEER
    length of the string is42
    Reversed string is REENIGNE ecneicS retupmoC YZARC a si yinim

    ********************MENU********************
    1.String
    2.Number
    Select any one option to print the reverse //select string
    1
    enter the string to reverse:Crazyengineers provide a common platform for enginee
    rs all around the world..Its not about Social Networking..
    length of the string is110
    Reversed string is ..gnikrowteN laicoS tuoba ton stI..dlrow eht dnuora lla sree
    nigne rof mroftalp nommoc a edivorp sreenigneyzarC

    ********************MENU********************
    1.String
    2.Number
    Select any one option to print the reverse //select number
    2

    Enter the number //zero as input
    0
    The reverse of the number is 0

    ********************MENU********************
    1.String
    2.Number
    Select any one option to print the reverse //single digit number input
    2
    Enter the number
    7
    The reverse of the number is 7

    ********************MENU********************
    1.String
    2.Number
    Select any one option to print the reverse //select number
    2
    Enter the number
    23
    The reverse of the number is 32

    ********************MENU********************
    1.String
    2.Number
    Select any one option to print the reverse //select number
    2
    Enter the number
    345
    The reverse of the number is 543

    ********************MENU********************
    1.String
    2.Number
    Select any one option to print the reverse //select number
    2
    Enter the number
    5698
    The reverse of the number is 8965

    ********************MENU********************
    1.String
    2.Number
    Select any one option to print the reverse //select number
    2
    Enter the number
    45097865 //multidigit number
    The reverse of the number is 56879054

    ********************MENU********************
    1.String
    2.Number
    Select any one option to print the reverse //select number
    2
    Enter the number
    45690
    The reverse of the number is 9654

    ********************MENU********************
    1.String
    2.Number
    Select any one option to print the reverse //invaild option
    3

    Enter a valid option //alert user
    Program Terminated

    ********************MENU********************
    1.String
    2.Number
    Select any one option to print the reverse //invalid option
    An
    Enter a valid option //alert user
    Program Terminated