Explain public static void main (String args[])

Featured


The public keyword is an access specifier, which allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared.In this case, main( ) must be declared as public, since it must be called by code outside of its class when the program is started. The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java interpreter before any objects are made. The keyword void simply tells the compiler that main( ) does not return a value. As you will see, methods may also return values.

Continue reading

10945 – Mother bear



#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

int main()
{
 char s[100000],a[100000],b[100000];
 int len,i,j;

 while(cin.getline(s,100000))
  {
   if (strcmp(s,"DONE")==0)
        break;
   len=strlen(s);
   int k=0;
   for(i=len-1;i>=0;i--)
    {
      if (s[i]>='A' && s[i]<='Z')
         a[k++]=tolower(s[i]);
      else if (s[i]>='a' && s[i]<='z')
         a[k++]=s[i];
 }
 a[k]=0;
 int z=0;
 for(i=strlen(a)-1;i>=0;i--)
  {
   b[z]=a[i];
   z++;
  }
 b[z]=0;
 if (strcmp(a,b)==0)
   cout<<"You won't be eaten!"<<endl;
 else
   cout<<"Uh oh.."<<endl;
 }
 return 0;
}

11854 – Egypt


#include <iostream>
#include <stdio.h>
using namespace std;

int main () {
  int a,b,c;

  while (scanf("%d%d%d",&a,&b,&c)==3) {

    if(a==0 && b==0 && c==0)
       break;

    if(a<=0 || b<=0 ||c<=0)
         cout << "wrong\n";
    else if (a*a==b*b+c*c)
         cout << "right\n";
    else if (b*b==a*a+c*c)
         cout << "right\n";
    else if (c*c==a*a+b*b)
         cout << "right\n";
    else
         cout << "wrong\n";
  }
}

11777 – Automate the Grades


#include <iostream>
#include <algorithm>
using namespace std;

int main () {

 int T, k=1, sum, sum1, Term1, Term2,
     Final, Attendance, Class_Test[3];

 cin >> T;

 while (T--) {
   cin >> Term1 >> Term2 >> Final >> Attendance;
   sum = Term1+Term2+Final+Attendance;

   for (int z=0;z<3;z++) {
    cin >> Class_Test [z];
   }

   sort (Class_Test,Class_Test+3);

   int Class = (Class_Test[1]+Class_Test[2])/2;

   sum1 = sum+Class;

   if(sum1>=90 )
      cout<<"Case "<<k<<": "<<"A"<<endl;
   else if(sum1>=80 && sum1<90 )
      cout<<"Case "<<k<<": "<<"B"<<endl;
   else if(sum1>=70 && sum1<80)
      cout<<"Case "<<k<<": "<<"C"<<endl;
   else if(sum1>=60 && sum1<70 )
      cout<<"Case "<<k<<": "<<"D"<<endl;
   else if(sum1<60)
      cout<<"Case "<<k<<": "<<"F"<<endl;

   k++;
  }
}

Write a Program to check given year is leap year or not


#include<stdio.h>
int main()
{
 /* declaring variables */
 int year;

 /* prompting user */
 printf("Enter any year: ");
 scanf("%d",&year);

 /* calculating leap year */
 if(((year%4==0)&&(year%100!=0))||(year%400==0))
        printf("%d is a leap year",year);
 else
        printf("%d is not a leap year",year);

 return 0;
}

Output:

Enter any year: 2010

2010 is not a leap year

Write a Program to check given number is palindrome number or not


#include <stdio.h>

int main()
{
 /* declaring variables */
 int number,palindrome, temp,sum=0;

 /* prompting user */
 printf("Enter a number to cheek palindrome: ");
 scanf("%d",&number);

 palindrome = number;

 /* reversing a number */
 while(number != '\0')
 {
  temp=number%10;
  sum=sum*10 + temp;
  number=number/10;
 }

 /* cheek palindrome number */
 if(palindrome == sum)
        printf("%d is a palindrome number",palindrome);
 else
        printf("%d is not a palindrome number",palindrome);

 return 0;
}

Output:

Enter a number to cheek palindrome: 131

131 is a palindrome number

Write a Program to display Fibonacci series using recursion


#include <stdio.h>

/* function declaration */
int fibo (int number);

/* function definition */
int fibo (int number)
{
 if (number==0)
 return 0;
 else if (number==1)
 return 1;
 else
 return (fibo(number-1) + fibo(number-2));
}

/* main function */
int main()
{
 /* declaring variables */
 int number,i=-1;

 /* prompting user */
 printf("Enter an integer to find fibonacci series: ");
 scanf("%d",&number);

 printf("Fibonacci series: ");

 /* calling a function for finding fibonacci series */
 while (++i < number)
   {
     printf ("%d\t",fibo(i));
   }

 return 0;
}

Output:

Enter an integer to find fibonacci series: 5

Fibonacci series: 0       1       1       2       3

Write a Program to find factorial of a number using recursion


#include <stdio.h>

/* function declaration */
int fact (int number);

/* function definition */
int fact (int number)
{
 if (number==1)
 return 1;
 else
 return (number*fact(number-1));
}

/* main function */
int main()
{
 /* declaring variables */
 int number;

 /* prompting user */
 printf("Enter an integer to find factorial: ");
 scanf("%d",&number);

 /* calling a function for finding factorial number */
 printf ("%d! = %d",number,fact(number));

 return 0;
}

Output:

Enter an integer to find factorial: 5

5! = 120