C programming solved programs/examples with solutions

161

Here we are sharing Types, Operators and Expressions related C programs | c programming examples | c programming questions | basic c programs

  • Write a program to convert a given number of days into months and days.

 #include<stdio.h>

int main()

{

  int days,months;

  printf(“Enter the number of days:”);

  scanf(“%d”,&days);

  months=(days/30);

  days=days-months*30; // days=days%30;

  printf(“The entered number of days are equal to %d months%d days”,months,days);

}

Output:

 [[email protected] ~]$ gcc dy_to_mn_conv.c

[[email protected] ~]$ ./a.out

Enter the number of days:45

The entered number of days are equal to 1 months15 days

  • Write a program to read length and width from the input and compute perimeter and  area of the rectangle.

#include<stdio.h>

int main()

{

  float length,width;

  float area,perimeter;

  printf(“Enter the length and width of a reactangle:”);

  scanf(“%f %f”,&length,&width);

  area=length*width;          //calculates area

  perimeter=2*(length+width);//calculates perimeter

  printf(“Area is %f and perimeter is%f”,area,perimeter);

}

Output:

 [[email protected] ~]$ gcc area_peri.c

[[email protected] ~]$ ./a.out

Enter the length and width of a reactangle:2.5 3

Area is 7.500000 and perimeter is11.000000[

  • Write a program to read the diameter of the circle and compute the perimeter and area of the circle.

#include<stdio.h>

int main()

{

  int diameter;

  float perimeter,area;

  printf(“Enter the diameter of the circle”);

  scanf(“%d”,&diameter);

  perimeter=diameter*3.14;        //calculates perimeter

  area=(3.14*diameter*diameter)/4;//calculates area

  printf(“Area of circle is %f and perimeter of circle is %f”,area,perimeter);

}

Output:

[[email protected] ~]$ gcc areaofcircle.c

[[email protected] ~]$ ./a.out

Enter the diameter of the circle26

Area of circle is 530.659973 and perimeter of circle is 81.639999

  • Write a program to read  a floating point number from the standard input and print right most digit of the integral part and left most digit of real part.

#include<stdio.h>

#define EXIT_SUCCESS 0

int main()

{

  float num,real_part;

  int int_part,r_num,i_num;

  printf(“Enter floating type number:”);

  scanf(“%f”,&num);

  int_part=num;

  real_part=num-int_part;

  printf(“integral part is %d\n real part is %f”,int_part,real_part);

  i_num=int_part%10;//gives the leftmostdigit of realpart

  real_part=real_part*10;//gives the rightmost digit of intger part

  r_num=(int)real_part;

  printf(“\n leftmost digit of realpart is %d\n right most digit of integral part is %d”,r_num,i_num);

  return EXIT_SUCCESS;

}

Output:

[[email protected] ~]$ gcc floatir.c

[[email protected] ~]$ ./a.out

Enter floating type number:1234.56

integral part is 1234

 real part is 0.560059

 leftmost digit of realpart is 5

 right most digit of integral part is 4

  • Write a program to read values of “x” and “y” from the input and evaluate the following expression and print the result

Expr:  7×5 + 3×3 + 12 x2 + 5x + 10

#include<stdio.h>

#include<math.h>

#define exit 0

int main()

{

  int x,res;

  printf(“Enter the values of X:”);

  scanf(“%d”,&x);

  res=(7*pow(x,5))+(3*pow(x,3))+(12*pow(x,2))+(5*x)+10;

  printf(“\nResult of the expresison is%d”,res);

  return exit;

}

Output:

[[email protected] ~]$ gcc -lm  eval_expr.c -o eval

[[email protected] ~]$ ./eval

Enter the values of X:12

Result of the expresison is1748806

  • Write a program to determine the ranges of char, short, int, float and long variables both signed and unsigned.

Output:-  Range of signed char  is -128 to 127.

                Range of unsigned char is from 0 to 255.

#include<stdio.h>

#include<limits.h>//contains the functions to check the range of char,so on

#define exit 0

int main()

{

  printf(“Max value of type char is %d\n”,CHAR_MAX);

  printf(“Min value of type char is %d\n”,CHAR_MIN);

  printf(“Max value of type SIGNED char is %d\n”,SCHAR_MAX);

  printf(“Min value of type SIGNED char is %d\n”,SCHAR_MIN);

  printf(“Max value of type UNSIGNED char is %u\n”,UCHAR_MAX);

  printf(“Max value of short is %d\n”,SHRT_MAX);

  printf(“Min value of short is %d\n”,SHRT_MIN);

  printf(“Max value of UNSIGNED short is%u\n”,USHRT_MAX);

  printf(“Max&MIN value of type int is%d%d\n”,INT_MAX,INT_MIN);

  printf(“Max value of type UNSIGNED int is %d\n”,UINT_MAX);

  printf(“Max value of type long is %ld\n”,LONG_MAX);

  printf(“Min value of type long is %ld\n”,LONG_MIN);

  printf(“Max value of UNSIGNED long is%ld\n”,ULONG_MAX);

}

Output:

[[email protected] ~]$ gcc range.c -o range

[[email protected] ~]$ ./range

Max value of type char is 127

Min value of type char is -128

Max value of type SIGNED char is 127

Min value of type SIGNED char is -128

Max value of type UNSIGNED char is 255

Max value of short is 32767

Min value of short is -32768

Max value of UNSIGNED short is65535

Max&MIN value of type int is2147483647-2147483648

Max value of type UNSIGNED int is -1

Max value of type long is 2147483647

Min value of type long is -2147483648

Max value of UNSIGNED long is-1


  • Write a loop equivalent to below for loop without using  ’&&’  or  ‘||’

#include<stdio.h>

#define exit 0

int main()

{

/*  int i; //given loop in the assignment

  char c;

  char s[100];

  int limit=100;

  for(i=0;i<limit-1&&(c=getchar())!=’\n’ && c!=EOF;++i)

  s[i]=c;

  printf(“%s”,s);*/

  int i=0,limit=100;

  char c,s[100];

  while(i<limit-1)//untill this condition fails

  {

    c=getchar(); //read into c

    if(c==EOF)  // && c!=EOF–>if c=end of file stop rading char and come out

       break;

    else if (c==’\n’)//&&(c=getchar())!=’\n’–>equal to newline comeout

           break;

    s[i++]=c; //if both the above conditions are satisfied,copy it into s

  }

s[i]=’\0′; //terminate the string

  printf(“%s”,s);

  return exit;

}

Output:

[[email protected] ~]$ gcc equivfor.c -o for

[[email protected] ~]$ ./for

this is a program to write the loop without using && or ||

this is a program to write the loop without using && or ||


  • Write a program to give the count of No of 1s in binary format of a number given.

Eg:  count = NoOf1sCount(155) = 5 (as Binary form  of 155 is 10011011)

#include<stdio.h>

#define exit 0

int main()

{

  long base=1,number,snum,count=0,rem,bin=0;

  printf(“Enter an decimal number”);

  scanf(“%d”,&number);

//—-converting into binary—-//

  snum=number;

  while(number>0)

  {

    rem=number%2;

    if(rem==1)//if it finds a 1 then increment the count

    {

      count++;

    }

    bin=bin+rem*base;

    number=number/2;

    base=base*10;

  }

  printf(“Input number is:%d \n”,snum);

  printf(“Binary equivalent is: %d\n”,bin);

  printf(“NO.of 1’s are: %d \n”,count);

return exit;

}

Output:

[[email protected] ~]$ gcc count_binary.c -o count

[[email protected] ~]$ ./count

Enter an decimal number155

Input number is:155

Binary equivalent is: 10011011

NO.of 1’s are: 5


  • Write a program to get product  of  2 pow n and a given number without using “*’ operation

Eg:  res  = myProduct(32, 2) = 32 * 4 = 128

                  myProduct(25, 4) = 25 * 16 = 400

#include<stdio.h>

#define exit 0

int main()

{

  int x,y,prod;

  printf(“Enter a number and the value of n in 2 pow n:”);

  scanf(“%d%d”,&x,&y);

  prod=x<<y;

  printf(“Product is %d\n”,prod);

  return exit;

}

Output:

[[email protected] ~]$ gcc myProduct.c -o product

[[email protected] ~]$ ./product

Enter a number and the value of n in 2 pow n:32

2

Product is 128


  • Write a program to get 1’s compliment of a given number without using “~” operator.

Eg: res= compliment1s(170) = 85

#include<string.h>

#define EXIT 0

int main()

{

   int num,i=0,j,s=0,k=0,l;

   char  bin[50];

    printf(“Enter an integer:”);

    scanf(“%d”,&num);

 //converting an integer to binary

    while(num>0)

    {

     bin[i]=(‘1’-1)+(num%2);

       i++;

       num=num/2;

    }

    printf(“Equivalent binary is  :”);

    for(j=i-1;j>=0;j–)

    printf(“%c”,bin[j]);

 //fliiping 1’s and 0’s to get 1s complement

  for(j=i;j>=0;j–)

  {

if(j==i)

   {

    if(bin[j]==’0′)

       bin[j]=’1′;

    else if(bin[j]==’1′)

       bin[j]=’0′;

   }

  }

  printf(“\n 1’s Complement is %s”,bin);

//converting again into integer

   l=strlen(bin);

   l–;

   for(i=l;bin[i]>=0;i–)

   {

    if(bin[i]==’1′)

    {

    s=pow(2,k)+s;

     k++;

    }

   else

     k++;

  }

 printf(“\nEquivalent integer is %d”,s);

}

 Output:

[[email protected] ~]$ gcc -lm  withoutild.c -o wtild

[[email protected] ~]$ ./wtild

Enter an integer:170

Equivalent binary is  :10101010

 1’s Complement is 01010101

Equivalent integer is 85


  • Write a program to get hexadecimal representation of given number using bit wise operations.

#include <stdio.h>

void hexconv(int a);

main()

{

 int a;

 printf(“Enter a no. in decimal system:-  “);

 scanf(“%d”,&a);

 hexconv(a);

}

 void hexconv(int a)//converts an integer into hex

 {

  int b,c=0,hex[5],i=0;

  b=a;

  while (b>15)

  {

   hex[i]=b%16;

   b=b>>4;

   i++;

   c++;

}

  hex[i]=b;

  printf(“Its hexadecimal equivalent is  “);

  for (i=c;i>=0;–i)

  {

   if (hex[i]==10)

        printf(“A”);

   else if (hex[i]==11)

        printf(“B”);

   else if (hex[i]==12)

        printf(“C”);

   else if (hex[i]==13)

        printf(“D”);

   else if (hex[i]==14)

        printf(“E”);

   else if (hex[i]==15)

        printf(“F”);

   else

        printf(“%d”,hex[i]);

  }

  return;

 }

Output:

[[email protected] ~]$ vi Hex_conv.c

[[email protected] ~]$ gcc Hex_conv.c -o hex

[[email protected] ~]$ ./hex

Enter a no. in decimal system:-  123 Its hexadecimal equivalent is  7B

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.