Tuesday, November 25, 2014

C using binary 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

C Program to convert the given Binary Number into Decimal

#include 
        #include
void main()
{
int  num, binary_val, decimal_val = 0, base = 1, rem; 
clrscr();
printf("Enter a binary number(1s and 0s) \n");
scanf("%d", &num); 
binary_val = num;
while (num > 0)
{
rem = num % 10;
decimal_val = decimal_val + rem * base;
num = num / 10 ;
base = base * 2;
}
printf("The Binary number is = %d \n", binary_val);
printf("Its decimal equivalent is = %d \n", decimal_val);
getch();
}

Output
 
Enter a binary number(1s and 0s)
10101001
The Binary number is = 10101001
Its decimal equivalent is = 169S

C Program to Reverse a Given Number

#include 
    #include
void main()
{
long  num, reverse = 0, temp, remainder;
clrscr();
printf("Enter the number\n");
scanf("%ld", &num);
temp = num;
while (num > 0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Given number = %ld\n", temp);
printf("Its reverse is = %ld\n", reverse);
getch();
}

Output
 
Enter the number
567865
Given number   = 567865
Its reverse is = 568765

C Program to illustrate how User Authentication is done

#include 
#include
void main()
{
   char password[10], username[10], ch;
   int i;
   clrscr();
   printf("Enter User name: ");
   gets(username);
   printf("Enter the password < any 8 characters>: ");
   for (i = 0; i < 8; i++)
   {
            ch = getchar();
            password[i] = ch;
            ch = '*' ;
            printf("%c", ch);
   }
        password[i] = '\0';
   printf("\n Your password is :");
   for (i = 0; i < 8; i++)
   {
            printf("%c", password[i]);
   }
}
Output
 
Enter User name: rajaraman
Enter the password <any 8 characters>: shashi12
********
Your password is :shashi12

No comments:

Post a Comment