Monday, 24 May 2021

Write a program in C to find the length of a string without using library function.

 Write a program in C to find the length of a string without using library function.









#include 
#include 
int main()
{
    char str[10];
    int length;

    printf("\nEnter the String : ");
    scanf("%s", str);

    length = 0;

    while (str[length] != '\0')
        length++;

    printf("\nLength of the String is : %d", length);
    return (0);
}


Output:



Share:

Write a program in C to swap two numbers using function

 Write a program in C to swap two numbers using function






#include 
#include 

void swap(int x, int y)
{
    x = x + y;
    y = x - y;
    x = x - y;
    printf("\nAfter Swapping: x = %d, y = %d", x, y);
}
int main()

{
    int x, y;
    printf("Enter Value of x : ");
    scanf("%d", &x);
    printf("\nEnter Value of y : ");
    scanf("%d", &y);
    swap(x, y);

    return 0;
}

Output:



Share:

Write a C program that read 5 numbers and sum of all odd values between them.

 Write a C program that read 5 numbers and sum of all odd values between them.





#include 
#include 
int main()
{
    int a, b, c, d, e;
    int sum = 0;
    printf("Enter 1st number :");
    scanf("%d", &a);
    printf("Enter 2nd number :");
    scanf("%d", &b);
    printf("Enter 3rd number :");
    scanf("%d", &c);
    printf("Enter 4th number :");
    scanf("%d", &d);
    printf("Enter 5th number :");
    scanf("%d", &e);
    if (a % 2 == 1)
        sum = sum + a;
    if (b % 2 == 1)
        sum = sum + b;
    if (c % 2 == 1)
        sum = sum + c;
    if (d % 2 == 1)
        sum = sum + d;
    if (e % 2 == 1)
        sum = sum + e;
    printf("Sum of the odd number is = %d", sum);
}

Output:






Share:

Write a C program to find the sum of first 10 natural numbers

 Write a C program to find the sum of first 10 natural numbers





#include 
#include 
void main()
{
    int j, r, sum = 0;
    printf("Enter the range :");
    scanf("%d", &r);
    printf("The first %d natural number sum is :\n", r);

    for (j = 1; j <= r; j++)
    {
        sum = sum + j;
        printf("%d", j);
        while (j < r)
        {
            printf("+");
            break;
        }
    }
    printf(" = %d\n", sum);
}


Output:






Share:

Write a C program to find whether a given year is a leap year or not

 Write a C program to find whether a given year is a leap year or not




#include 
#include 

int main()
{
    int year, a;
    printf("Enter a year to chack its leap year or not :");
    scanf("%d", &year);
    a = year % 4;
    if (a == 0)
    {
        printf("%d is leap year", year);
    }
    else
    {
        printf("%d is not leap year", year);
    }

    return 0;
}

Output:





Share: