sum of digits in a number
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num,rem=0,sum=0;
printf("enter number : ");
scanf("%d",&num);
while(num!=0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
}
printf("sum of integer is : %d",sum);
printf("\n");
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num=0;
printf("enter the number :");
scanf("%d",&num);
num=num%2;
if(num==0)
{
printf("number is even ");
}
else
{
printf("number is odd");
}
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int total_days=0,b1=0,year=0,week=0,day=0;
printf("enter days : ");
scanf("%d",&total_days);
year=total_days/365;
b1=total_days-(year*365);
week=b1/7;
day=b1-(week*7);
printf("years=%d,week=%d,days=%d \n",year,week,day);
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num,rem=0,dec=0,base=1;
printf("enter binary number : ");
scanf("%d",&num);
while(num>0)
{
rem=num%10;
dec=dec+(rem*base);
base=base*2;
num=num/10;
}
printf("decimal number is : %d ",dec);
}
decimal to binary
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num,rem=0,bin=0,base=1,dec;
printf("enter decimal number : ");
scanf("%d",&dec);
while(dec!=0)
{
rem=dec%2;
bin=bin+(rem*base);
base=base*10;
dec=dec/2;
}
printf("corresponding binary= %d\n",bin);
}
#include<stdio.h>
int main()
{
int n,result,count=0;
printf("enter the number : ");
scanf("%d",&n);
while (n)
{
n &= (n-1) ;
count++;
}
printf("number of 1's in the number = %d\n",count);
}
Description
9 = 1001 so there is total 2 number of 1s in 9
position of 1s
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num=0,check=0,i=0;
printf("enter the number=");
scanf("%d",&num);
printf("1's in\n");
for( i=0;i<9;i++)
{
if(num&(0x80>>i))
{
printf("position %d\n",i+1);
}
}
}
Description
Here we enter the number 9 = 00001001 ( 8 bit binary equivalent of 9 ) . So here in this example from LSB to MSB 1's in the position 5 and 8.
Pointers
sum of array
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num[11],*ptr,sum=0,i;
printf("enter 10 elements : \n");
for(i=0;i<10;i++)
{
printf("element %d=",(i+1));
scanf("%d",&num[i]); // exit from scanf() only when press enter key
}
ptr=#
for(i=0;i<10;i++)
{
sum=sum+*ptr;
ptr++;
}
printf("sum of array elements = %d\n",sum);
}
swapping
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1,num2;
printf("enter first number:");
scanf("%d",&num1);
printf("enter second number:");
scanf("%d",&num2);
swap(&num1,&num2);
printf("\nswapped result is\n");
printf("first number=%d\n",num1);
printf("second number=%d\n",num2);
}
int swap(int *ptr1,int *ptr2)
{
int reg;
reg=*ptr1;
*ptr1=*ptr2;
*ptr2=reg;
}
#include<stdio.h>
int main()
{
int num,rem,rev=0;
printf("enter the number : ");
scanf("%d",&num);
while(num>0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
printf("The reverse of number = %d\n",rev);
}
length of a string
#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[20],length=0;
printf("enter the string=");
gets(str);
length=string_length(&str);
printf("length of string=%d",length);
length=0;
}
string_length(char *ptr)
{
int num=0;
while(*ptr!='\0')
{
num++;
ptr++;
}
return num;
}