Key Features

Experienced Teachers
Smart Classes
Regular Test
Study Material

Well qualified teachers with industrial experience.

To visualise or understand topics very easly.

We are organizing online and offline regular test.

We are providing prescribed study material.

Showing posts with label C Programming By Suraj Singh Rajput. Show all posts
Showing posts with label C Programming By Suraj Singh Rajput. Show all posts

Friday, October 30, 2020

Write a C Program to Count Number of Digits in an Integer.

/*

Write a C Program to Count Number of Digits in an Integer.
*/

#include <stdio.h>
int main()
{
            long long num;
             int count = 0;
            /* Input number from user */
            printf("Enter any number: ");
            scanf("%lld", &num);
            /* Run loop till num is greater than 0 */
            while(num != 0)
            {
                         /* Increment digit count */
                        count++;
                        /* Remove last digit of 'num' */
                         num /= 10;
            }
            printf("Total digits: %d", count);
            return 0;
}

OUTPUT :
Enter any number: 45698
Total digits: 5

Thank you very much for reading carefully, if you have any other questions, you can share it with us through comments, if this information was important to you, please let us know through comments.

Please do comment and share.
Thank You.

Write a C Program to find the factorial of a number.

Question :  Write a C Program to find the factorial of a number.
/*
Write a C Program to find the factorial of a number.
*/

#include <stdio.h>
int main()
{
            int n, i;
            unsigned long long fact = 1;
            printf("Enter an integer: ");
            scanf("%d", &n);
            // shows error if the user enters a negative integer
            if (n < 0)
            {
                        printf("Error! Factorial of a negative number doesn't exist.");
            }
            else
{
                        for (i = 1; i <= n; ++i)
{
                         fact *= i;
                        }
             printf("Factorial of %d = %llu", n, fact);
            }
    return 0;
}

OUTPUT :
Enter an integer: 10
Factorial of 10 = 3628800

Thank you very much for reading carefully, if you have any other questions, you can share it with us through comments, if this information was important to you, please let us know through comments.

Please do comment and share.
Thank You.

Question : Write a C Program to check whether a number is positive or negative.

Question :  Write a C Program to check whether a number is positive or negative.

/*
Write a C Program to check whether a number is positive or negative.
*/

#include <stdio.h>
int main()
{
            double num;
            printf("Enter a number: ");
            scanf("%lf", &num);
            if (num <= 0.0)
{
                        if (num == 0.0)
                        {
                                    printf("You entered 0.");
                        }
else
{
printf("You entered a negative number.");
}
            }
else
{
            printf("You entered a positive number.");
            }
    return 0;
}

OUTPUT :
Enter a number: 12.3
You entered a positive number.

Thank you very much for reading carefully, if you have any other questions, you can share it with us through comments, if this information was important to you, please let us know through comments.

Please do comment and share.
Thank You.

Question : Write a C Program to check whether a year is leap year or not.


 Question :  Write a C Program to check whether a year is leap year or not.

/*
Write a C Program to check whether a year is leap year or not.
*/

#include <stdio.h>
int main()
{
            int year;
            printf("Enter a year: ");
            scanf("%d", &year);
            if (year % 4 == 0)
{
                        if (year % 100 == 0)
{
                                    // the year is a leap year if it is divisible by 400.
                                                if (year % 400 == 0)
                                    {
                                     printf("%d is a leap year.", year);
                                    }
                                     else
{          
                                    printf("%d is not a leap year.", year);
                                    }
                         }
else
{
                                    printf("%d is a leap year.", year);
}
            }
else
{
            printf("%d is not a leap year.", year);
            }
    return 0;
}

OUTPUT :
Enter a year: 1900
1900 is not a leap year.

Thank you very much for reading carefully, if you have any other questions, you can share it with us through comments, if this information was important to you, please let us know through comments.

Please do comment and share.
Thank You.

Question : Write a C Program to find minimum element in array.

Question :  Write a C Program to find minimum element in array.

/*
Write a C Program to find minimum element in array.
*/

#include <stdio.h>
int main()
{
            int array[100], minimum, size, c, location = 1;
            printf("Enter number of elements in array\n");
            scanf("%d", &size);
            printf("Enter %d integers\n", size);
for (c = 0; c < size; c++)
            scanf("%d", &array[c]);
            minimum = array[0];
            for (c = 1; c < size; c++)
                        {
                                    if (array[c] < minimum)
                                                {
                                                 minimum = array[c];
                                                location = c+1;
                                                }
                         }
  
printf("Minimum element is present at location %d and its value is %d.\n", location, minimum);
    return 0;
}
OUTPUT :
Enter number of elements in array
5
Enter 5 integers
2
5
4
1
6
Minimum element is present at location 4 and its value is 1.

Thank you very much for reading carefully, if you have any other questions, you can share it with us through comments, if this information was important to you, please let us know through comments.

Please do comment and share.
Thank You.

Question : Write a C Program to find ASCII value of a character entered by user.

Question :  Write a C Program to find ASCII value of a character entered by user.


#include <stdio.h>
int main()
{ 
            char c;
            printf("Enter a character: ");
            scanf("%c", &c); 
            // %d displays the integer value of a character
            // %c displays the actual character
            printf("ASCII value of %c = %d", c, c);
            return 0;
}

OUTPUT :
Enter a character: G
ASCII value of G = 71

Thank you very much for reading carefully, if you have any other questions, you can share it with us through comments, if this information was important to you, please let us know through comments.

Please do comment and share.
Thank You.

Question : Write a C Program for Fibonacci series.

Question :   Write a C Program for Fibonacci series.


#include <stdio.h>
int main()   
{   
int n1=0,n2=1,n3,i,number;   
            printf("Enter the number of elements:");   
scanf("%d",&number);   
            printf("\n%d %d",n1,n2);//printing 0 and 1   
            for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed    
            {   
                        n3=n1+n2;   
                        printf(" %d",n3);   
                         n1=n2;   
                         n2=n3;   
 } 
  return 0; 
 }
  
OUTPUT :
Enter the number of elements:15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

Thank you very much for reading carefully, if you have any other questions, you can share it with us through comments, if this information was important to you, please let us know through comments.

Please do comment and share.
Thank You.