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.
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"; } }
12372 – Packing for Holiday
#include <iostream> using namespace std; int main () { int T,W,H,L,i=0; cin >> T; while (T--){ cin >> W >> H >> L; if (W<=20 && H<=20 && L<=20) cout << "Case " << ++i << ": " <<"good" <<endl; else cout << "Case " << ++i << ": " << "bad"<< endl; } }
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++; } }
11332 – Summing Digits
#include <iostream> #include <cstdio> using namespace std; int main() { int n,sum; while (scanf("%d",&n)==1 && n!=0) { if (n<10) cout << n << endl; if (n>9) { while (n>9) { sum=0; while (n>0 ) { sum=sum+n%10; n=n/10; } n=sum; } cout << sum << endl; } } return 0; }
10346 – Peter’s Smokes
#include <iostream> #include <cstdio> using namespace std; int main (){ int n,k; while (scanf ("%d %d",&n,&k)==2 && k>1) { int smoke=n; while (n>=k) { smoke += n/k; n = n/k + n%k; } cout << smoke << endl; } }
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
11150 – Cola
#include <iostream> using namespace std; int main () { int n; while(cin >> n) { n += n/2; cout << n << endl; } }
11727 – Cost Cutting
#include <iostream> using namespace std; int main () { int T,a,b,c,i; cin >> T; for (i=1;i<=T;i++) { cin >> a >> b >> c; if (a>b && a<c || a<b && a>c) cout << "Case " << i << ": " << a <<endl; else if (b>a && b<c || b<a && b>c) cout << "Case " << i << ": " << b << endl; else cout << "Case " << i << ": " << c << endl; } }
Write a program to print alphabets
class Alphabets { public static void main(String args[]) { char ch; for( ch = 'a' ; ch <= 'z' ; ch++ ) System.out.print(ch + " "); } }
Command line arguments
class Arguments { public static void main(String[] args) { for (String t: args) { System.out.println(t); } } }
Hello World in Java
class HelloWorld { public static void main (String args []) { System.out.println ("Hello World"); } }
Output:
Hello World
How is printf statement interpreted?
#include <stdio.h> int main () { printf ( 5 + "Good Morning "); }
Explanation:
There are a lot of things happening here. As others have said, printf()
doesn’t ‘know’ anything about the expression 5+"Good Morning"
. The value of that expression is determined by the C language.
Why goto statements are not available in Java?
Goto statements lead to confusion for a programmer. Especially in a large program, if several Goto statements are used, the programmer would be perplexed while understanding the flow from where to where the control is jumping.
Write a program to print “Hello World” without using main function
#include <stdio.h> #define hello main int hello () { printf ("Hello World"); }
What is string args in java main?
In the main method, the String array args refers to the arguments that the program may require before starting. In many cases you may want the program to take some values as input for processing. this string array is for that purpose. It is needed to configure the application to run a particular way or provide it with some piece of information it needs.
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