Tuesday, 16 September 2014

Write a C program "if...else" statement

 Write a C program to check whether a number entered by user is even or odd

#include <stdio.h>
int main(){
      int num;
      printf("Enter a number you want to check.\n");
      scanf("%d",&num);
      if((num%2)==0)          //checking whether remainder is 0 or not.
           printf("%d is even.",num);
      else
           printf("%d is odd.",num);
      return 0;
}




Write a C program " if statement"

Write a C program to print the number entered by user only if the number entered is negative.


#include <stdio.h>
      int main(){
      int num;
      printf("Enter a number to check.\n");
      scanf("%d",&num);
      if(num<0) {      /* checking whether number is less than 0 or not. */ 
            printf("Number = %d\n",num); 
      }  
/*If test condition is true, statement above will be executed, otherwise it will not be executed */
      printf("The if statement in C programming is easy.");
return 0;
}




Monday, 15 September 2014

Write a program Java for loop example to print stars in console

Java for loop example to print stars in console

class Stars {
  public static void main(String[] args) {
    int row, numberOfStars;

    for (row = 1; row <= 10; row++) {
      for(numberOfStars = 1; numberOfStars <= row; numberOfStars++) {
        System.out.print("*");
      }
      System.out.println(); // Go to next line
    }
  }
}

O/P:

Java for loop example program output

Write a program Java for loop

Java for Loop

class ForLoop {
  public static void main(String[] args) {
    int c;

    for (c = 1; c <= 10; c++) {
      System.out.println(c);
    }
  }
}



O/P:

Java for loop example program output

Write a Java hello world program

Hello world program

class HelloWorld
{
   public static void main(String args[])
   {
      System.out.println("Hello World");
   }
}


O/P:


Java hello world code output

Write a C program for print integer

C programming code

#include <stdio.h>

int main()
{
  int a;

  printf("Enter an integer\n");
  scanf("%d", &a);

  printf("Integer that you have entered is %d\n", a);

  return 0;
}


O/P:

input number

Write a C program to add n numbers.

 Add n numbers.

#include <stdio.h>

int main()
{
   int n, sum = 0, c, value;

   printf("Enter the number of integers you want to add\n");
   scanf("%d", &n);

   printf("Enter %d integers\n",n);

   for (c = 1; c <= n; c++)
   {
      scanf("%d",&value);
      sum = sum + value;
   }

   printf("Sum of entered integers = %d\n",sum);

   return 0;
}



O/P:

Add n numbers c program