Programming Codes for Braching statement In C - Programming Languages

Latest

Wednesday 1 February 2017

Programming Codes for Braching statement In C

                               Simple codes 1 


 Content


1. check if given integer is odd or even
2. check if given integer is positive or negative
3. find the biggest of 3 numbers
4. calculate the sum of odd & even number
5. reverse a number &check if it is a palindrome or not
6. read two integers M and N &swap their value
7.reverse a given number
8. convert a decimal number binary &count the number of 1s
9. find if a given year is a leap year
10.swap the Contents of two Numbers using Bitwise XOR operation




1. check if the given is odd or even

#include<stdio.h>
#include<conio.h>
void main()
{
int number;
clrscr();
printf("please enter a no.\n");
scanf("%d",&number);
if(number%2==0)
{
printf("this no. is even/n");
}
else
{
printf("this no. is odd\n");
}
getch();
}


2. check if given integer is positive or negative

#include<stdio.h>
#include<conio.h>
void main()
{
sign int number;
clrscr();
printf("enter no\n");
scanf("%d",&number);
if(number>=0)
{
printf("no is positive\n");
}
else
{
printf("no is negative\n");
}
getch();
}



3. find the biggest of 3 numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int no1,no2,no3;
clrscr();
printf("enter value of no1,no2,no3\n");
scanf("%d %d %d,&no1,&no2,&no3);
if(no1>no2)
{
if(no1>no3)
{
printf("first no is greatest in three\n");
}
else
{
printf("second no is grestest in three\n");
}
}
else if(no2>no3)
{printf(no2 is greatest in three\n");
}
else
{
printf("no3 is the greatest in three\n");
}
getch();
}


4. calculate the sum of odd & even number

#include<stdio.h>
#include<conio.h>
void main()
{
int num=100,i,sum1=0,sum2=0;
clrscr();
for(i=0,i<100;i++)
{
if(i%2==0)
{
sum1=sum1+i;
}
else
{
sum2=sum2+i;
}
}
printf("sum of odd no is %d\n",sum2);
printf("sum of even no. is %d\n",sum1);
getch();
}


5. reverse a number &check if it is a palindrome or not
  

#include<stdio.h>
#include<conio.h>
void main()
{
int num,temp,remainder,reverse=0;
clrscr()
{
printf("please enter no. to check\n");
scanf("%d",&num);
temp=num;
while(num>0)
{
remainder=num%10;
reverse=reverse*10+remainder;
num/10;
}
if(reverse==temp)
{
printf("no is palandrom\n");
}
else
{
printf("no is not paladrome\n");
}
getch();
}


6. read two integers M and N &swap their value

#include<stdio.h>
#include<conio.h>
void main()
{
int n,m,swap=0;
clrscr();
printf("fill value of n and m\n");
scanf("%d %d",&n,&m);
swap=n;
n=m;
m=swap;
printf("value of n is :%d and m is:%d\n",n,m);
getch();
}


7.reverse a given number

#include<stdio.h>
#include<conio.h>
void main()
{
int num,temp,remainder,reverse=0;
clrscr()
{
printf("please enter no. to check\n");
scanf("%d",&num);
temp=num;
while(num>0)
{
remainder=num%10;
reverse=reverse*10+remainder;
num/10;
}
printf("given number is :%d and acutall number is :%d\n",temp,reverse);
getch();
}


8. convert a decimal number binary &count the number of 1s

#include<conio.h>
#include<stdio.h>
void main()
{
long num,decimal_num,remainder,base=1,binary=0,no_of_1s=0;
clrscr();
printf("enter a decimal integer\n");
scanf("%ld",&num);
decimal_num=num;
while(num>0)
{
remainder=num%2;
if(remainder==1)
{
no_of_1s++;
}
binary=binary+remainder+base;
num=num/2;
base=base*10;
}
printf("input number is =%ld\n",decimal_num);
printf("its binary equivalent is=%ld",binary);
printf("no of 1s is the binary no. is =%ld",no_of_1s);
getch();
}


9. find if a given year is a leap year


#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("enter year\n");
scanf("%d",&year);
if(year%400==0||year%4==0)
{
printf("%d is leap year\n",year);
}
else
{
printf("%d is not a leap year\n",year);
}
getch();
}


10.swap the Contents of two Numbers using Bitwise XOR operation


#include<conio.h>
#include<stdio.h>
void main()
{
long i,k;
clrscr();
printf("enter two integer\n");
scanf("%ld %ld",&i,&k);
printf("before swapping i=%ld and k=%ld",i,k);
i=i^k;
k=i^k;
i=i^k;
printf("after swapping i is:%ld & k is:%ld\n",i,k);
getch();
}


 

No comments:

Post a Comment