logo

Computer Science Quiz

Programming in C


Programming in C
Elements
Attributes

Html5

API
Graphics

Programming in C
#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;
}

Most Viewed Computer Science Links