Pratik Ghelani: C Programming

Pratik Ghelani

Breaking

Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

Friday, March 29, 2019

4:32 AM

Write a C program to calculate factorial of a number using recursion.

Write a C program to calculate factorial of a number using recursion.

Program:-

#include <stdio.h>
mul(int n);
int main()
{
    int n;
    printf("Enter a positive integer: ");
    scanf("%d", &n);
    printf("Factorial of %d = %ld", n, mul(n));
    return 0;
}
mul(int n)
{
    if (n >= 1)
        return n*mul(n-1);
    else
        return 1;

}


Output:-


4:25 AM

Write a C program Using user-defined function for addition of N numbers.


Write a C program Using user-defined function for addition of N numbers.
Program:-
#include <stdio.h>
int addNumbers(int n);
int main()
{
    int num;
    printf("Enter a positive integer: ");
    scanf("%d", &num);
    printf("Sum = %d",add(num));
    return 0;
}
int add(int n)
{
    if(n != 0)
        return n + add(n-1);
    else
        return n;
}

Output:-


4:11 AM

Write a C Program to perform addition of two matrices.

Write a C Program to perform addition of two matrices.

Program:-

#include <stdio.h>
void 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 entered 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");
   }

}


Output:-


4:06 AM

Write a C Program to read array of integers and print its sum and average.

Write a C Program to read array of integers and print its sum and average.

Program:-
#include <stdio.h>
void main()
{
    int Arr[100], n, i, sum = 0;

    printf("Enter the number of elements : ");
    scanf("%d", &n);

    for (i = 0; i < n; i++)
    {
        printf("Enter element %d : ", i + 1);
        scanf("%d", &Arr[i]);
        sum += Arr[i];
    }
    printf("\nThe sum of the array is : %d", sum);
    printf("\nThe average of the array is : %0.2f", (float)sum / n);
}

Output:-




Friday, February 1, 2019

6:22 AM

Write a C Program: to print Fibonacci series 0, 1, 1, 2, 3, 5 ...N using do while loop.

Program:-

#include<stdio.h>
void main()
{
    int n,a,b=-1,c=1;

     
    printf("  Enter The Number Of Terms:");
    scanf("%d",&n);
    printf("  The Fibonacci Series is:");
    do
    {
        a=b+c;
        b=c;
        c=a;
        printf("  \n %d",a);
        n--;
    }while(n>0);
}

Output:-





6:11 AM

Write a C Program To find whether the given number is prime or not.

Program:-

#include <stdio.h> 
void main() 
{ 
    int n, i, a = 1; 
    printf("Enter a number: \n"); 
    scanf("%d", &n); 
    for (i = 2; i <= n / 2; i++) { 
        if (n % i == 0) { 
            a = 0; 
            break; 
        } 
    } 
    if (a == 1) { 
        printf("%d is a prime number", n); 
    } 
    else { 
        printf("%d is not a prime number", n); 
    } 

}


Output:-





6:04 AM

Write a C Program To determine whether an entered character is vowel or not.

Program:-

#include <stdio.h>
void main()
{
    char a;
    int isLowercaseVowel, isUppercaseVowel;
    printf("Enter an alphabet: ");
    scanf("%c",&a);
if(a == 'a'|| a == 'e' || a == 'i' || a == 'o' || a == 'u' || a == 'A' || a == 'E' || a == 'I' || a == 'O' || a == 'U' )
{
   printf(" vowel.",&a); 
}
 else
 {
        printf("not vowel",&a); 
 }

}

Output:-




Friday, January 11, 2019

4:34 AM

Write a C Program find the largest number out of three without using nested if.

Program:-

#include <stdio.h>
void main()
{
    double a,b,c;

    printf("Enter three different numbers: ");
    scanf("%lf %lf %lf", &a,&b,&c);

    if( a>=b && a>=c )
        printf("%.2f is the largest number.", a);

    if( b>=a && b>=c )
        printf("%.2f is the largest number.", b);

    if( c>=a && c>=b )
        printf("%.2f is the largest number.", c);
  

}

Output:-




4:29 AM

Write a C Program check whether given number is positive, negative or zero.

Program:-

#include <stdio.h>
void main()
{
    double a;

    printf("Enter a number: ");
    scanf("%lf", &a);
    if (a < 0.0)
        printf("You entered number is negative number.");
    else if (a > 0.0)
        printf("You entered number is  positive number.");
    else
        printf("You entered number is Zero number.");


}

Output:-






Thursday, January 10, 2019

3:30 AM

Write a C Program to read two numbers from key board and perform addition, subtraction, division and multiplication using assignment operators.

Program:-

#include <stdio.h>

void main()
{
   int a, b, add, sub, mul;
   float divi;

   printf("Enter two integers\n");
   scanf("%d%d", &a, &b);

   add = a + b;
   sub = a - b;
   mul = a * b;
   divi = a / (float)b;   

   printf("Addition = %d\n", add);
   printf("subtraction = %d\n", sub);
   printf("Multiplication = %d\n", mul);
   printf("Division = %.2f\n", divi);


}


Output:-