Friday, November 28, 2014

C using simple pro



C Program to Calculate the Sum of Odd & Even Numbers
#include
#include 
void main()
{
    int i, num, odd_sum = 0, even_sum = 0;
    clrscr();
    printf("Enter the value of num\n");
    scanf("%d", &num);
    for (i = 1; i <= num; i++)
    {
        if (i % 2 == 0)
            even_sum = even_sum + i;
        else
            odd_sum = odd_sum + i;
    }
    printf("Sum of all odd numbers  = %d\n", odd_sum);
    printf("Sum of all even numbers = %d\n", even_sum);
    getch();
}

Output

Enter the value of num
10
Sum of all odd numbers = 25
Sum of all even numbers = 30

C Program to Reverse a Number & Check if it is a Palindrome

#include 
#include 
void main()
{
    int num, temp, remainder, reverse = 0;
    clrscr();
    printf("Enter an integer \n");
    scanf("%d", &num);
    temp = num;
    while (num > 0)
    {
        remainder = num % 10;
        reverse = reverse * 10 + remainder;
        num /= 10;
    }
    printf("Given number is = %d\n", temp);
    printf("Its reverse is  = %d\n", reverse);
    if (temp == reverse)
        printf("Number is a palindrome \n");
    else
        printf("Number is not a palindrome \n");
    getch();
}
 
Output
 
Enter an integer
6789
Given number is = 6789
Its reverse is  = 9876
Number is not a palindrome

C Program to Find the Number of Integers Divisible by 5

#include 
#include
void main()
{
    int i, num1, num2, count = 0, sum = 0;
    clrscr();
    printf("Enter the value of num1 and num2 \n");
    scanf("%d %d", &num1, &num2);
    printf("Integers divisible by 5 are \n");
    for (i = num1; i < num2; i++)
    {
        if (i % 5 == 0)
        {
            printf("%3d,", i);
            count++;
            sum = sum + i;
        }
    }
    printf("\n Number of integers divisible by 5 between %d and %d =
 %d\n", num1, num2, count);
    printf("Sum of all integers that are divisible by 5 = %d\n", sum);
   getch();
}
 
Output

Enter the value of num1 and num2
12 17
Integers divisible by 5 are
 15,
Number of integers divisible by 5 between 12 and 17 = 1
Sum of all integers that are divisible by 5 = 15

C Program to Read Two Integers M and N & Swap their Values

#include 
#include
void swap(float *ptr1, float  *ptr2);
void main()
{
float m, n;
clrscr();
printf("Enter the values of M and N \n");
scanf("%f %f", &m, &n);
printf("Before Swapping:M = %5.2ftN = %5.2f\n", m, n);
swap(&m, &n);
printf("After Swapping:M  = %5.2ftN = %5.2f\n", m, n);
}
void swap(float *ptr1, float *ptr2)
{
float temp;
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}

Output

Enter the values of M and N
2 3
Before Swapping:M =  2.00    N =  3.00
After Swapping:M  =  3.00    N =  2.00

No comments:

Post a Comment