logo

Computer Science Quiz

Programming


Programming
C
C++

JAVA
Home
Programming
#include <stdio.h>
int main(){
   int m, n, c, d, first[10][10], second[10][10], sum[10][10];
    printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &m, &n);
   printf("Enter the elements of first matrix\n");
   for (c = 0; c < m; c++)
      for (d = 0; d < n; d++)
         scanf("%d", &first[c][d]);
    printf("Enter the elements of second matrix\n");
    for (c = 0; c < m; c++)
      for (d = 0 ; d < n; d++)
            scanf("%d", &second[c][d]);
    printf("Sum of matrices:-\n");
 
   for (c = 0; c < m; c++) {
      for (d = 0 ; d < n; d++) {
         sum[c][d] = first[c][d] + second[c][d];
         printf("%d\t", sum[c][d]);
      }
      printf("\n");
   }
 
   return 0;
}
#include <stdio.h>
int main(){
    int number, originalNumber, remainder, result = 0;
    printf("Enter a three digit integer: ");
    scanf("%d", &number);
    originalNumber = number;
    while (originalNumber != 0)    {
        remainder = originalNumber%10;
        result += remainder*remainder*remainder;
        originalNumber /= 10;
    }
    if(result == number)
        printf("%d is an Armstrong number.",number);
    else
        printf("%d is not an Armstrong number.",number);

    return 0;
}
#include <stdio.h>
int main(){
    double n1, n2, n3;
    printf("Enter three numbers: ");
    scanf("%lf %lf %lf", &n1, &n2, &n3);
    if( n1>=n2 && n1>=n3 )
        printf("%.2f is the largest number.", n1);
    if( n2>=n1 && n2>=n3 )
        printf("%.2f is the largest number.", n2);
    if( n3>=n1 && n3>=n2 )
        printf("%.2f is the largest number.", n3);
    return 0;
}

#include<stdio.h>
int main() {
 float celsius, fahrenheit;
 printf("\nEnter temp in Celsius : ");
 scanf("%f", &celsius);
 fahrenheit = (1.8 * celsius) + 32;
 printf("\nTemperature in Fahrenheit : %f ", fahrenheit);
 return (0);
}
#include <stdio.h>
#include <string.h>
int main(){
   char a[100], b[100];
   printf("Enter the first string\n");
   gets(a);
   printf("Enter the second string\n");
   gets(b);
   if (strcmp(a,b) == 0)
      printf("Entered strings are equal.\n");
   else
      printf("Entered strings are not equal.\n");
    return 0;
}
#include <stdio.h>
#include <math.h>
int main (){
  double param, result;
  param = 27.0;
  result = cbrt (param);
  printf ("cbrt (%f) = %f\n", param, result);
  return 0;
}
#include <stdio.h>
int main(){
    int n, i;
    unsigned long long factorial = 1;
    printf("Enter an integer: ");
    scanf("%d",&n);
    if (n < 0)
        printf("Error! Factorial of a negative number doesn't exist.");
   else{
        for(i=1; i<=n; ++i){
            factorial *= i;
        }
        printf("Factorial of %d = %llu", n, factorial);
    }

    return 0;
}
#include <stdio.h>
 int main(){
    float celsius, fahrenheit;
    printf("Enter temperature in Fahrenheit: ");
    scanf("%f", &fahrenheit);
    celsius = (fahrenheit - 32) * 5 / 9;
    printf("%.2f Fahrenheit = %.2f Celsius", fahrenheit, celsius);
    return 0;
}
#include<stdio.h>
 
int main(){
   int n, first = 0, second = 1, next, c;
   printf("Enter the number of terms\n");
   scanf("%d",&n);
   printf(" Fibonacci series are :",n);
   for ( c = 0 ; c < n ; c++ ){
      if ( c <= 1 )
         next = c;
      else {
         next = first + second;
         first = second;
         second = next;
      }
      printf("%d\n",next);
   }
 
   return 0;
}
#include <stdio.h>
int main()
{
    long long n;
    int count = 0;
    printf("Enter an integer: ");
    scanf("%lld", &n);
    while(n != 0){
        n /= 10;
        ++count;
    }
    printf("Number of digits: %d", count);
}
#include <stdio.h>
int main(){
    int a, b, c;
    printf("Enter a,b,c: ");
    scanf("%d %d %d", &a, &b, &c);
    if (a > b && a > c) {
        printf("a is Greater than b and c");
    }
    else if (b > a && b > c) {
        printf("b is Greater than a and c");
    }
    else if (c > a && c > b) {
        printf("c is Greater than a and b");
    }
    else {
        printf("all are equal or any two values are equal");
    } 
    return 0;
}
#include <stdio.h>
int main(){
    int n1, n2, i, gcd;
    printf("Enter two integers: ");
    scanf("%d %d", &n1, &n2);
    for(i=1; i <= n1 && i <= n2; ++i)    {
        if(n1%i==0 && n2%i==0)
            gcd = i;
    }
    printf("G.C.D of %d and %d is %d", n1, n2, gcd);
    return 0;
}
#include <stdio.h>
int main(){
    int n1, n2, minMultiple;
    printf("Enter two positive integers: ");
    scanf("%d %d", &n1, &n2);
    minMultiple = (n1>n2) ? n1 : n2;
    while(1)    {
        if( minMultiple%n1==0 && minMultiple%n2==0 )        {
            printf("The LCM of %d and %d is %d.", n1, n2,minMultiple);
            break;
        }
        ++minMultiple;
    }
    return 0;
}
#include <stdio.h>
int main() {
   int year;
   year = 2016;
   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;
}
#include <stdio.h>
#include <string.h>
int main(){
   char a[100];
   int length;
   printf("Enter a string \n");
   gets(a);
   length = strlen(a);
   printf("Length of string is = %d\n",length);
   return 0;
}
#include <stdio.h>
int main()
{
  int m, n, p, q, c, d, k, sum = 0;
  int first[10][10], second[10][10], multiply[10][10];
  printf("Enter the number of rows and columns of first matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix\n");
  for (c = 0; c < m; c++)
    for (d = 0; d < n; d++)
      scanf("%d", &first[c][d]);
  printf("Enter the number of rows and columns of second matrix\n");
  scanf("%d%d", &p, &q);
  if (n != p)
    printf("Error.\n");
  else
  {
    printf("Enter the elements of second matrix\n");
 
    for (c = 0; c < p; c++)
      for (d = 0; d < q; d++)
        scanf("%d", &second[c][d]);
 
    for (c = 0; c < m; c++) {
      for (d = 0; d < q; d++) {
        for (k = 0; k < p; k++) {
          sum = sum + first[c][k]*second[k][d];
        }
 
        multiply[c][d] = sum;
        sum = 0;
      }
    }
    printf("Product of entered matrices:-\n");
 
    for (c = 0; c < m; c++) {
      for (d = 0; d < q; d++)
        printf("%d\t", multiply[c][d]);
 
      printf("\n");
    }
  }
 
  return 0;
}
#include <stdio.h>
int main()
{
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number);
    if(number % 2 == 0)
        printf("%d is even.", number);
    else
        printf("%d is odd.", number);

    return 0;
}
#include <stdio.h>
int main(){
    int n, reversedInteger = 0, remainder, originalInteger;
    printf("Enter an integer: ");
    scanf("%d", &n);
    originalInteger = n;
    while( n!=0 ) {
        remainder = n%10;
        reversedInteger = reversedInteger*10 + remainder;
        n /= 10;
    }
	if (originalInteger == reversedInteger)
        printf("%d is a palindrome.", originalInteger);
    else
        printf("%d is not a palindrome.", originalInteger);
    
    return 0;
}
#include <stdio.h>
int main(){
    int i, j, rows;
    printf("Enter number of rows: ");
    scanf("%d",&rows);
    for(i=1; i<=rows; ++i){
       for(j=1; j<=i; ++j){
            printf("%d ",j);
        }
        printf("\n");
    }
    return 0;
}
#include <stdio.h>
int main(){
    int base, exponent;
    long long result = 1;
    printf("Enter a base number: ");
    scanf("%d", &base);
    printf("Enter an exponent: ");
    scanf("%d", &exponent);
    while (exponent != 0){
        result *= base;
        --exponent;
    }
    printf("Answer = %lld", result);
    return 0;
}
#include <stdio.h> 
    main() {
    
      int n, i, c = 0;
      printf("Enter any number n:");
      scanf("%d", &n);
      for (i = 1; i <= n; i++) {
          if (n % i == 0) {
             c++;
          }
      }
      if (c == 2) {
      	printf("n is a Prime number");
      }
      else {
     	 printf("n is not a Prime number");
      }
      return 0;    
    }  
#include<stdio.h>
int main(){
    int n, reverse = 0;    
    printf("Enter a number to reverse\n");    
    scanf("%d",&n);
    while (n != 0){
        reverse = reverse * 10;
        reverse = reverse + n%10;
        n = n/10;
    }
    printf("Reverse of entered number is = %d\n", reverse);    
    return 0;
}
#include<stdio.h>
#include<string.h>
int main() {
   char str[100], temp;
   int i, j = 0;
    printf("\nEnter the string :");
   gets(str);
   i = 0;
   j = strlen(str) - 1;
    while (i < j) {
      temp = str[i];
      str[i] = str[j];
      str[j] = temp;
      i++;
      j--;
   }
   printf("\nReverse string is :%s", str);
   return 0;
   }
#include<stdio.h>
int main() {
   int amount, rate, time, si;
   printf("\nEnter Principal Amount : ");
   scanf("%d", &amount);
   printf("\nEnter Rate of Interest : ");
   scanf("%d", &rate);
   printf("\nEnter Period of Time   : ");
   scanf("%d", &time);
   si = (amount * rate * time) / 100;
   printf("\nSimple Interest : %d", si);
   return(0);
}
#include <stdio.h>
void main(){
    int i, j, a, n, number[30];
    printf("Enter the value of N \n");
    scanf("%d", &n);
    printf("Enter the numbers \n");
    for (i = 0; i < n; ++i)
        scanf("%d", &number[i]);
    for (i = 0; i < n; ++i)    {
        for (j = i + 1; j < n; ++j) {
            if (number[i] > number[j]){
                a =  number[i];
                number[i] = number[j];
                number[j] = a;
            }
        }
    }
    printf("Ascending order are given below \n");
    for (i = 0; i < n; ++i)
        printf("%d\n", number[i]);
}
#include <stdio.h>
#include <math.h>
int main(){
    double num, root;
    printf("Enter any number to find square root: ");
    scanf("%lf", &num);
    root = sqrt(num);
     printf("Square root of %.2lf = %.2lf", num, root);
    return 0;
} 
#include <stdio.h>
int main(){
   int n, t, sum = 0, remainder;
   printf("Enter an integer\n");
   scanf("%d", &n);
   t = n;
   while (t != 0){
      remainder = t % 10;
      sum       = sum + remainder;
      t         = t / 10;
   }
   printf("Sum of digits of %d = %d\n", n, sum);
   return 0;
}
#include<stdio.h>
int main(){
  int Number, i, Sum = 0;
  printf("\nPlease Enter any Integer Value\n");
  scanf("%d", &Number);
  for(i = 1; i <= Number; i++){
     Sum = Sum + i;
  }
  printf("Sum of Natural Numbers = %d", Sum);
  return 0;
}
#include<stdio.h>
#include<conio.h>
void main(){
 int i,a[5],b[5],c[5];
 clrscr();
 printf("\nReading the first array\n");
 for (i=0;i<5;i++)
   {
   printf("Enter the  value");
   scanf("%d",&a[i]);
   }
 printf("\nReading the second array\n");
 for (i=0;i<5;i++)
   {
   printf("Enter the  value");
   scanf("%d",&b[i]);
   }
   printf("\nThe output of addition of two array is\n");
   for(i=0;i<5;i++)
     {
     c[i]=a[i]+b[i];
     printf("\nthe sum of %d & %d is %d",a[i],b[i],c[i]);
     }
     getch();
}
#include <stdio.h>
int main()
{
   int x, y, temp;
   printf("Enter the value of x and y\n");
   scanf("%d%d", &x, &y);
   printf("Before Swapping\nx = %d\ny = %d\n",x,y);
   temp = x;
   x    = y;
   y    = temp;
   printf("After Swapping\nx = %d\ny = %d\n",x,y);
   return 0;
}
#include<stdio.h>
#include<string.h>
int main(){
  char str[20];
  int i;
  printf("Enter any string->");
  scanf("%s",str);
  printf("The string is->%s",str);
  for(i=0;i<=strlen(str);i++){
      if(str[i]>=65&&str[i]<=90)
       str[i]=str[i]+32;
  }
  printf("\nThe string in lower case is->%s",str);
  return 0;
}
#include <iostream>
int main(){
    int r, c, a[100][100], b[100][100], sum[100][100], i, j;
    cout << "Enter number of rows : ";
    cin >> r;

    cout << "Enter number of columns: ";
    cin >> c;

    cout << endl << "Enter elements of first matrix: " << endl;
    for(i = 0; i < r; ++i)
       for(j = 0; j < c; ++j)       {
           cout << "Enter element a" << i + 1 << j + 1 << " : ";
           cin >> a[i][j];
       }
     cout << endl << "Enter elements of second matrix: " << endl;
    for(i = 0; i < r; ++i)
       for(j = 0; j < c; ++j)
       {
           cout << "Enter element b" << i + 1 << j + 1 << " : ";
           cin >> b[i][j];
       }
	for(i = 0; i < r; ++i)
        for(j = 0; j < c; ++j)
            sum[i][j] = a[i][j] + b[i][j];
     cout << endl << "Sum of two matrix is: " << endl;
    for(i = 0; i < r; ++i)
        for(j = 0; j < c; ++j){
            cout << sum[i][j] << "  ";
            if(j == c - 1)
                cout << endl;
        }

    return 0;
}
#include <iostream>
int main(){
  int origNum, num, rem, sum = 0;
  cout << "Enter a positive  integer: ";
  cin >> origNum;
  num = origNum;
  while(num != 0)  {
      digit = num % 10;
      sum += digit * digit * digit;
      num /= 10;
  }

  if(sum == origNum)
	cout << origNum << " is an Armstrong number.";
  else
    cout << origNum << " is not an Armstrong number.";
  return 0;
}
#include <iostream>
int main(){    
    float n1, n2, n3;
    cout << "Enter three numbers: ";
    cin >> n1 >> n2 >> n3;
    if(n1 >= n2 && n1 >= n3)    {
        cout << "Largest number: " << n1;
    }
    if(n2 >= n1 && n2 >= n3)    {
        cout << "Largest number: " << n2;
    }
    if(n3 >= n1 && n3 >= n2) {
        cout << "Largest number: " << n3;
    }
    return 0;
}
#include<iostream>
int main(){
    float fahrenheit, celsius;
    cout << "Enter the temperature in Celsius : ";
    cin >> celsius;
    fahrenheit = (celsius * 9.0) / 5.0 + 32;
    cout << "Temperature in Celsius    : " << celsius << endl;
    cout << "Temperature in Fahrenheit : " << fahrenheit << endl;
    return 0;
}
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main(){
	clrscr();
	char str1[100], str2[100];
	cout<<"Enter first string : ";
	gets(str1);
	cout<<"Enter second string : ";
	gets(str2);
	if(strcmp(str1, str2)==0)	{
		cout<<"Both the strings are equal";
	}
	else	{
		cout<<"Both the strings are not equal";
	}
	getch();
}
#include <stdio.h>
#include <math.h>
int main ()
{
  double param, result;
  param = 27.0;
  result = cbrt (param);
  printf ("cbrt (%f) = %f\n", param, result);
  return 0;
}
#include<iostream.h>
#include<conio.h>
void main(){
int i, no, fact=1;
clrscr();
cout<<"Enter the any no. : ";
cin>>no;
for(i=1;i<=no;i++){
fact=fact*i;
}
cout<<"Factorial: "<<fact;
getch();
}
#include<conio.h>
#include<iostream.h>
void main()
{
float f,c;
clrscr();
cout<<"Enter Fahrenheit degree to find temperature in celsius: ";
cin>>f;
c = (f-32)/1.8;
cout<<"\n\n\tCELSIUS DEGREE = "<<c;
getch();
}
#include<iostream.h>
#include<conio.h>
void main(){
int i,no, first=0, second=1, next;
clrscr();
first=0;
second=1;
cout<<"Enter nubmer of terms for Series: ";
cin>>no;
cout<<"Fibonacci series are: \n";
for(i=0; i<no; i++){
cout<<"\n"<<first;
next = first + second;
first = second;
second = next;
}
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int no,a=0;
clrscr();
cout<<"Enter any number : ";
cin>>no;
while(no>0)
{
no=no/10;
a++;
}
cout<<"\n no. of digits in given number is: "<<a;
getch();
}
#include <iostream>
int main(){    
    float n1, n2, n3;
    cout << "Enter three numbers: ";
    cin >> n1 >> n2 >> n3;
    if(n1 >= n2 && n1 >= n3)    {
        cout << "Largest number: " << n1;
    }
    if(n2 >= n1 && n2 >= n3) {
        cout << "Largest number: " << n2;
    }
    if(n3 >= n1 && n3 >= n2) {
        cout << "Largest number: " << n3;
    }
    return 0;
}
#include<iostream.h>
#include<conio.h>
void gcd(int,int);
void main(){
    int a,b;
    clrscr();
    cout<<"Enter two numbers: ";
    cin>>a>>b;
    gcd(a,b);
    getch();
  }
void gcd(int a,int b){
    int m,n;
    m=a;
    n=b;
    while(m!=n){
        if(m>n)
            m=m-n;
        else
            n=n-m;
    }
    cout<<"\nH.C.F of"<<a<<" & "<<b<<" is "<<m;
}
#include <iostream>
int main(){
    int n1, n2, max;
    cout << "Enter two numbers: ";
    cin >> n1 >> n2;
    max = (n1 > n2) ? n1 : n2;
    do{
        if (max % n1 == 0 && max % n2 == 0){
            cout << "LCM = " << max;
            break;
        }
        else
            ++max;
    } while (true);
    
    return 0;
}
#include<iostream.h>
 #include<conio.h>
 main()  {
       clrscr();
       int year;
       cout<<"\n Enter the year = ";
       cin>>year;
       if(year%4==0 || year%400==0 || year%100==0)
      cout<<"this is a leap year"<<endl;
       else
      cout<<"this is not a leap year"<<endl;
       getch();
   }
#include <iostream>
int main(){
    string str = "C++ Programming";
    cout << "String Length = " << str.size();
    return 0;
}
#include <iostream>
int main()
{
    int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
    cout << "Enter rows and columns for first: ";
    cin >> r1 >> c1;
    cout << "Enter rows and columns for second: ";
    cin >> r2 >> c2;
	while (c1!=r2)    {
        cout << "Error! column of first matrix not equal to row of second.";

        cout << "Enter rows and columns for first matrix: ";
        cin >> r1 >> c1;

        cout << "Enter rows and columns for second matrix: ";
        cin >> r2 >> c2;
    }

    cout << endl << "Enter elements of matrix 1:" << endl;
    for(i = 0; i < r1; ++i)
        for(j = 0; j < c1; ++j) {
            cout << "Enter element a" << i + 1 << j + 1 << " : ";
            cin >> a[i][j];
        }

    cout << endl << "Enter elements of matrix 2:" << endl;
    for(i = 0; i < r2; ++i)
        for(j = 0; j < c2; ++j)  {
            cout << "Enter element b" << i + 1 << j + 1 << " : ";
            cin >> b[i][j];
        }

	for(i = 0; i < r1; ++i)
        for(j = 0; j < c2; ++j)  {
            mult[i][j]=0;
        }

    for(i = 0; i < r1; ++i)
        for(j = 0; j < c2; ++j)
            for(k = 0; k < c1; ++k)  {
                mult[i][j] += a[i][k] * b[k][j];
            }

    cout << endl << "Output Matrix: " << endl;
    for(i = 0; i < r1; ++i)
    for(j = 0; j < c2; ++j) {
        cout << " " << mult[i][j];
        if(j == c2-1)
            cout << endl;
    }

    return 0;
}
#include <iostream>
int main(){
    int n;
    cout << "Enter an integer: ";
    cin >> n;
    if ( n % 2 == 0)
        cout << n << " is even.";
    else
        cout << n << " is odd.";

    return 0;
}
#include<iostream>
int main(){
    int palindrome, reverse=0;
    cout<<"Enter number:  ";
    cin>>palindrome;
    int num=0,key=palindrome;
for(int i=1;palindrome!=0;i++){
    num=palindrome%10;
    palindrome=palindrome/10;
    reverse=num+(reverse*10);
   }
   if(reverse==key){
   cout<<key<<" is a Palindrome Number";
   }
 else{
   cout<<key<<"is NOT a Palindrome Number";
   }
return 0;
}
#include<iostream>
int main(){
    int rows;
    cout << "Enter the number of rows : ";
    cin >> rows;
    cout << endl;
    for (int i = 0; i < rows; i++){
        int val = 1;
        for (int j = 1; j < (rows - i); j++) {
            cout << "   ";
        }
        for (int k = 0; k <= i; k++) {
            cout << "      " << val;
            val = val * (i - k) / (k + 1);
        }
        cout << endl << endl;
    }
    cout << endl;
    return 0;
}
#include <stdio.h>
#include <math.h> 
int main (){
  printf ( pow (8.0, 3.0) );
   return 0;
}
#include<iostream.h>
#include<conio.h>

void main()
{
  int i,no;
  clrscr();
  cout<<"Enter any num: ";
  cin>>no;
  if(no==1)  {
  cout<<"Smallest prime num is 2";
  }
  for(i=2;i<no;i++)  {
  if(no%i==0)  {
  cout<<"Not prime num";
  break;
  }
  }
 if(no==i) {
 cout<<"Prime num";
 }
  getch();
}
#include <iostream>
using namespace std;

int main(){
    int n, reversedNumber = 0, remainder;
    cout << "Enter an integer: ";
    cin >> n;
    while(n != 0)
    {
        remainder = n%10;
        reversedNumber = reversedNumber*10 + remainder;
        n /= 10;
    }
    cout << "Reversed Number = " << reversedNumber;
    return 0;
}
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(){
 char str[100],temp;
 int i,j=0;
 clrscr();
 cout<<"Enter any the string :";
 gets(str); 
 i=0;
 j=strlen(str)-1;
  while(i<j)   {
   temp=str[i];
   str[i]=str[j];
   str[j]=temp;
   i++;
   j--;
   }
 cout<<"Reverse string is: "<<str;
 getch();
}
#include<iostream>
int main(){
	int p,r,t,i;
	cout<<"Enter Principle : ";
	cin>>p;
	cout<<"Enter Rate : ";
	cin>>r;
	cout<<"Enter Time : ";
	cin>>t;
	i=(p*r*t)/100;
	cout<<"Simple interest is : "<<i;
	return 0;
}
#include<iostream.h>
#include<conio.h>
 void main() {
 int i,a[10],temp,j;
 clrscr();
 cout<<"Enter any 10 num in array: \n";
 for(i=0;i<=10;i++) {
 cin>>a[i];
 }
 cout<<"\nData before sorting: ";
 for(j=0;j<10;j++) {
 cout<<a[j];
 }
 for(i=0;i<=10;i++) {
 for(j=0;j<=10-i;j++) {
 if(a[j]>a[j+1]) {
 temp=a[j];
 a[j]=a[j+1];
 a[j+1]=temp;
 }
 }
 }
 cout<<"\nData after sorting: ";
 for(j=0;j<10;j++) {
 cout<<a[j];
 }
 getch();
 }
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main() { 
int num,ans; 
clrscr(); 
cout<<"Enter any Num: "; 
cin>>num; 
ans=pow(num,0.5);
cout<<"\n Squre of "<<num<<" is: "<<ans; 
getch(); 
} 
#include<iostream>
int main(){
    int val, num, sum = 0;
    cout << "Enter the number : ";
    cin >> val;
    num = val;
    while (num != 0)    {
        sum = sum + num % 10;
        num = num / 10;
    }
    cout << "Sum of the digits of "<< val << " is " << sum;
}
#include<iostream>
int main(){
    int i,n,sum=0;
    cout<<"How many numbers? ";
    cin>>n;
    for(i=1;i<=n;++i)    {
        sum+=i;
    }
    cout<<"Sum="<<sum;
    return 0;
}
#include<iostream>
void main(){
   int first[20], second[20], c, n;
   cout << "Enter the number of elements in the array ";
   cin >> n;
   cout << "Enter elements of first array " << endl;
   for ( c = 0 ; c < n ; c++ )
     cin >> first[c];
   cout << "Enter elements of second array " << endl;
   for ( c = 0 ; c < n ; c++ )
      cin >> second[c];
   cout << "Sum of elements of two arrays " << endl;
   for ( c = 0 ; c < n ; c++ )
     cout << first[c] + second[c] << endl;
   return 0;
}
#include <iostream>
int main()
{
    int a = 5, b = 10, temp;
    cout << "Before swapping." << endl;
    cout << "a = " << a << ", b = " << b << endl;
    temp = a;
    a = b;
    b = temp;
    cout << "\nAfter swapping." << endl;
    cout << "a = " << a << ", b = " << b << endl;
    return 0;
}
#include<iostream>
int main(){
	char uppercase;
	int offset = 'a' - 'A';
	char lowercase;
	cout<<"Enter an uppercase letter:  ";
	cin>>uppercase;
	if(uppercase>='A' && uppercase<='Z')
		lowercase = (char)(uppercase + offset);
	else
		lowercase= uppercase;
	cout<<"The lowercase letter is:  "<<lowercase << endl;
	return 0;
}
import java.util.Scanner;
class AddTwoMatrix{
   public static void main(String args[])   {
      int m, n, c, d;
      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of rows and columns");
      m = in.nextInt();
      n  = in.nextInt();
	  int first[][] = new int[m][n];
      int second[][] = new int[m][n];
      int sum[][] = new int[m][n];
      System.out.println("Enter the elements of first matrix");
 
      for (  c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            first[c][d] = in.nextInt();
 
      System.out.println("Enter the elements of second matrix");
 
      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            second[c][d] = in.nextInt();
 
      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
             sum[c][d] = first[c][d] + second[c][d];  
      System.out.println("Sum of entered matrices:-");
 
      for ( c = 0 ; c < m ; c++ )      {
         for ( d = 0 ; d < n ; d++ )
            System.out.print(sum[c][d]+"\t");
 
         System.out.println();
      }
   }
}
class Armstrong{  
  public static void main(String[] args)  {  
    int c=0,a,temp;  
    int n=153;
    temp=n;  
    while(n>0){  
    a=n%10;  
    n=n/10;  
    c=c+(a*a*a);  
    }  
    if(temp==c)  
    System.out.println("armstrong number");   
    else  
        System.out.println("Not armstrong number");   
   }  
} 
import java.util.Scanner;
class LargestThreeNumbers{
   public static void main(String args[])   {
      int x, y, z;
      System.out.println("Enter three integers ");
      Scanner in = new Scanner(System.in);
      x = in.nextInt();
      y = in.nextInt();
      z = in.nextInt();
      if ( x > y && x > z )
         System.out.println("First number is largest.");
      else if ( y > x && y > z )
         System.out.println("Second number is largest.");
      else if ( z > x && z > y )
         System.out.println("Third number is largest.");
      else   
         System.out.println("Entered numbers are not distinct.");
   }
}
import java.util.Scanner;
public class CelsiusToFahrenheit {
public static void main(String[] args) {
	System.out.println("Enter a temperature in Celsius: ");
	Scanner in = new Scanner(System.in);
	double Fahrenheit = 0;
	if(in.hasNextDouble()){
		Fahrenheit = (in.nextDouble() + 32)*9/5;
	}
	System.out.println("Temperature in Fahrenheit is: "+Fahrenheit);
}}
class Stringcomparison{  
 public static void main(String args[]){  
   String s1="Durai";  
   String s2="Durai";  
   System.out.println(s1.equals(s2));
 }  
}  
package com.tutorialspoint;
import java.lang.*;
public class MathDemo {
   public static void main(String[] args) {
      double x = 25;
      double y = 10;
		System.out.println("Math.cbrt(" + x + ")=" + Math.cbrt(x));
      System.out.println("Math.cbrt(" + y + ")=" + Math.cbrt(y));
     
   }
}
class Factorial{  
 public static void main(String args[]){  
  int i,fact=1;  
  int number=5;    
  for(i=1;i<=number;i++){    
      fact=fact*i;    
  }    
  System.out.println("Factorial of "+number+" is: "+fact);    
 }  
}  
import java.util.*;
class FahrenheitToCelsius {
  public static void main(String[] args) {
    float temperatue;
    Scanner in = new Scanner(System.in);      
    System.out.println("Enter temperatue in Fahrenheit");
    temperatue = in.nextInt();
    temperatue = ((temperatue - 32)*5)/9;
 
    System.out.println("Temperatue in Celsius = " + temperatue);
  }
}
class Fibonacci{  
public static void main(String args[]){    
 int n1=0,n2=1,n3,i,count=10;    
 System.out.print(n1+" "+n2);
 for(i=2;i
	
import java.util.Scanner;
class Numofdigit {
	public static void main(String[] args){
		int no,a=0;
		Scanner s=new Scanner(System.in);
		System.out.println("Enter any number : ");
		no = s.nextInt();

	if(no<0){
		no=no * -1;
	}
	else if(no==0){
		no=1;
	}
	while(no>0){
		no=no/10;
		a++;
	}
		System.out.println("Number of Digits in given number is: "+a);
	}
}
import java.util.Scanner;
class LargestOfThreeNumbers{
   public static void main(String args[])   {
      int x, y, z;
      System.out.println("Enter three integers ");
      Scanner in = new Scanner(System.in);
      x = in.nextInt();
      y = in.nextInt();
      z = in.nextInt();
      if ( x > y && x > z )
         System.out.println("First number is largest.");
      else if ( y > x && y > z )
         System.out.println("Second number is largest.");
      else if ( z > x && z > y )
         System.out.println("Third number is largest.");
      else   
         System.out.println("Entered numbers are not distinct.");
   }
}
import java.util.*;
class Gcd {
public static void main(String args[])throws Exception{
	Scanner sc = new Scanner(System.in);
	System.out.print("Enter the First no : ");
	int n1=sc.nextInt();
	System.out.print("Enter the Second no : ");
	int n2=sc.nextInt();
	int r;
	while(n2 != 0){
		r = n1 % n2;
		n1 = n2;
		n2 = r;
	}
	System.out.print("GCD = "+n1);
}
}
import java.util.Scanner;
public class JavaProgram{
    public static void main(String args[])    {
        int a, b, x, y, t, hcf, lcm;
        Scanner scan = new Scanner(System.in);
	    System.out.print("Enter Two Number : ");
        x = scan.nextInt();
        y = scan.nextInt();
	    a = x;
        b = y;
	    while(b != 0){
            t = b;
            b = a%b;
            a = t;
        }
		
        hcf = a;
        lcm = (x*y)/hcf;
	    System.out.print("HCF = " +hcf);
        System.out.print("\nLCM = " +lcm);
    }
}
public class LeapYear{
 public static void main(String[] args) {
  int year = 2004;
	if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
		System.out.println("Year " + year + " is a leap year");
	else
		System.out.println("Year " + year + " is not a leap year");
        }
}
import java.io.*;
public class Test {
   public static void main(String args[]) {
      String Str1 = new String("Welcome to this app");
      String Str2 = new String("Java" );

      System.out.print("String Length :" );
      System.out.println(Str1.length());

      System.out.print("String Length :" );
      System.out.println(Str2.length());
   }
}
import java.util.Scanner;
class MatrixMultiplication{
   public static void main(String args[])   {
      int m, n, p, q, sum = 0, c, d, k;
      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of rows and columns of first matrix");
      m = in.nextInt();
      n = in.nextInt();
      int first[][] = new int[m][n];
      System.out.println("Enter the elements of first matrix");
      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            first[c][d] = in.nextInt();
      System.out.println("Enter the number of rows and columns of second matrix");
      p = in.nextInt();
      q = in.nextInt();
 
      if ( n != p )
         System.out.println("Error");
      else  {
         int second[][] = new int[p][q];
         int multiply[][] = new int[m][q];
         System.out.println("Enter the elements of second matrix");
         for ( c = 0 ; c < p ; c++ )
            for ( d = 0 ; d < q ; d++ )
               second[c][d] = in.nextInt();
 
         for ( c = 0 ; c < m ; c++ ) {
            for ( d = 0 ; d < q ; d++ ) {   
               for ( k = 0 ; k < p ; k++ ){
                  sum = sum + first[c][k]*second[k][d];
               }
 
               multiply[c][d] = sum;
               sum = 0;
            }
         }
         System.out.println("Product of entered matrices:-");
 
         for ( c = 0 ; c < m ; c++ ){
            for ( d = 0 ; d < q ; d++ )
               System.out.print(multiply[c][d]+"\t");
 
            System.out.print("\n");
         }
      }
   }
}
mport java.util.Scanner;
class CheckEvenOdd{
  public static void main(String args[]) {
    int num;
    System.out.println("Enter an Integer number:");

    Scanner input = new Scanner(System.in);
    num = input.nextInt();
    
    if ( num % 2 == 0 )
        System.out.println("Entered number is even");
     else
        System.out.println("Entered number is odd");
  }
}
import java.util.Scanner;
class Palindrome {
public static void main(String[] args){
int a,no,b,temp=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter any num: ");
no=s.nextInt();
b=no;
while(no>0)
{
a=no%10;
no=no/10;
temp=temp*10+a;
}
if(temp==b)
{
System.out.println("Palindrome");
}
else
{
System.out.println("not Palindrome");
}
}
}
import java.util.Scanner;
public class JavaPascalTriangle{
    public static void main(String args[])	{
	    int r, i, k, number=1, j;
		Scanner scan = new Scanner(System.in);
		System.out.print("Enter Number of Rows : ");
		r = scan.nextInt();
		for(i=0;ii; k--)			{
				System.out.print(" ");
			}
            number = 1;
			for(j=0;j<=i;j++)			{
				 System.out.print(number+ " ");
                 number = number * (i - j) / (j + 1);
			}
			System.out.println();
		}
	}
}
import java.lang.*;
public class MathDemo {
   public static void main(String[] args) {
	double x = 2.0;
    double y = 5.4;
	System.out.println(Math.pow(x, y));
    System.out.println(Math.pow(y, x));
   }
}
class PrimeNumbers
{
   public static void main (String[] args){		
       int i =0;
       int num =0;
       String  primeNumbers = "";
       for (i = 1; i <= 100; i++){ 		  	  
          int counter=0; 	  
          for(num =i; num>=1; num--){
             if(i%num==0){
				counter = counter + 1;
			 }
		}
	  if (counter ==2)
	  {
	     primeNumbers = primeNumbers + i + " ";
	  }	
       }	
       System.out.println("Prime numbers from 1 to 100 are :");
       System.out.println(primeNumbers);
   }
}
class ReverseNumber{
   public static void main(String args[]){
      int n, reverse = 0;
 
      System.out.println("Enter the number to reverse");
      Scanner in = new Scanner(System.in);
      n = in.nextInt();
      while( n != 0 ){
          reverse = reverse * 10;
          reverse = reverse + n%10;
          n = n/10;
      }
 
      System.out.println("Reverse of entered number is "+reverse);
   }
}
class StringReverse{  
 public static void main(String args[]){
	String str = "Durai";
	char[] strArray = str.toCharArray();
	for (int i = strArray.length - 1; i >= 0; i--)
	{
		System.out.print(strArray[i]);  
	}
}
}
import java.util.Scanner;
public class SimpleInterest{
    public static void main(String args[])    {
        float p, r, t;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter the Principal : ");
        p = s.nextFloat();
        System.out.print("Enter the Rate of interest : ");
        r = s.nextFloat();
        System.out.print("Enter the Time period : ");
        t = s.nextFloat();
        float si;
        si = (r * t * p) / 100;
        System.out.print("The Simple Interest is : " + si);
    }
}
import java.util.Scanner;
public class Order {
    public static void main(String[] args)     {
        int n, temp;
        Scanner s = new Scanner(System.in);
        System.out.print("Enter no. of elements you want:");
        n = s.nextInt();
        int a[] = new int[n];
        System.out.println("Enter all the elements:");
        for (int i = 0; i < n; i++){
            a[i] = s.nextInt();
        }
        for (int i = 0; i < n; i++){
            for (int j = i + 1; j < n; j++) 
            {
                if (a[i] > a[j]) 
                {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
        System.out.print("Ascending Order:");
        for (int i = 0; i < n - 1; i++) 
        {
            System.out.print(a[i] + ",");
        }
        System.out.print(a[n - 1]);
    }
}
class FindSquareRoot {
	public static void main(String[] args) {
		double n = 20;
		double x = 1;
		for (int i = 0; i < n; i++) {
			x = 0.5 * (x + n / x);
		}
		System.out.println("Square root of " + n + " is " + x);
		}
	}
public class SumOfDigits{
public static void main(String args[]) {
	Scanner sc = new Scanner(System.in);
	int number = sc.nextInt();
	int sum = 0;
	int input = number;
 while (input != 0) {
	int lastdigit = input % 10;
	sum += lastdigit; input /= 10;
 }
 System.out.printf("Sum of digits of number %d is %d", number, sum);
 }
}
public class Natural{
public static void main(String args[]) {
	int x, i = 1 ;
	int sum = 0;
	System.out.println("Enter Number of items :");
	Scanner s = new Scanner(System.in);
	x = s.nextInt();
	while(i <= x) {
		sum = sum +i;
		i++;
	}
	System.out.println("Sum of "+x+" numbers is :"+sum);
} 
}
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
    int n = 11;
    int[] firstArray = {8, 9, 2,5,1,9,5,9,5,4,7};
    int[] secondArray = {8, 9, 2,5,1,9,5,9,5,4,7};
    List<Object> total = new ArrayList<>();
    for(int i = 0; i < n; i++){
        int sum = firstArray[i] + secondArray[i];
        total.add(sum);
    }
    Integer[] sumArray = total.toArray(new Integer[total.size()]);
    for(int i = 0; i < n; i++) {
        System.out.print(sumArray[i] + " ");
    }
}
}
public static void main(String a[]){
	int x = 10;
	int y = 20;
	System.out.println("Before swap:");
	System.out.println("x value: "+x);
	System.out.println("y value: "+y);
	x = x+y;
	y=x-y;
	x=x-y;
	System.out.println("After swap:");
	System.out.println("x value: "+x);
	System.out.println("y value: "+y);
 }
import java.io.*;
public class Test {
   public static void main(String args[]) {
      String Str = new String("WELCOME");
     
      System.out.println(Str.toLowerCase() );
   }
}

Most Viewed Computer Science Links