0% found this document useful (0 votes)
12 views

C Assignment

Uploaded by

swet9736
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

C Assignment

Uploaded by

swet9736
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

C Assignment

61. Write a C program to convert the given binary number into its
equivalent decimal.

INPUT:
#include <stdio.h>

int binaryToDecimal(int binaryNumber) {


int decimalNumber = 0, base = 1, remainder;

while (binaryNumber > 0) {


remainder = binaryNumber % 10;
decimalNumber += remainder * base;
base *= 2;
binaryNumber /= 10;
}

return decimalNumber;
}

int main() {
int binaryNumber;

printf("Enter a binary number: ");


scanf("%d", &binaryNumber);

int decimalNumber = binaryToDecimal(binaryNumber);

printf("Decimal equivalent: %d\n", decimalNumber);

return 0;
}
OUTPUT:
45. Write a program to compute the surface area and volume of
a cube.

INPUT:
#include<stdio.h>
main(){
float a;
printf("Enter the side of the cube:");
scanf("%f",&a);
float surfacearea = 6*a*a;
printf("The surfacearea of cube is: %f\n",surfacearea);
float volume = a*a*a;
printf("The volume of cube is: %f ",volume);
}
OUTPUT:

47. Write a program to find whether a given year is a leap year or


not.

INPUT:

#include<stdio.h>
main(){
int x;
printf("Enter year\n");
scanf("%d",&x);
if(
( (x%4==0)&&(x%100!=0))||(x%400==0)
){
printf("%d is an leap year",x);
}
else{
printf("%d is not an leap year",x); /
}
}
OUTPUT
40. Write the program to generate Fibonacci sequences.

INPUT:
#include<stdio.h>
main(){
int fib1=0,fib2=1,fib3,n,count=0;
printf("Enter the value of n \n");
scanf("%d",&n);
printf("First %d fibonacci numbers are \n",n);
printf("%d\n",fib1);
printf("%d \n",fib2);
count=2;
while(count<n){
fib3 = fib1+fib2;
count++;
printf("%d \n", fib3);
fib1=fib2;
fib2=fib3;
}
}
OUTPUT:

49. Write a program to convert a given number of days to a measure


of time given in years, weeks and days. For example, 375 days is
equal to 1 year 1 week and 3 days (ignore leap year).

INPUT:
#include<stdio.h>
main(){
int x; //Assuming x = number of days
printf("enter the number of days");
scanf("%d",&x);
int year = x / 365;
int week = ( x % 365)/7;
int days = ( x% 365)%7;
printf("year =%d \n",year);
printf("week = %d\n",week);
printf("days=%d \n",days);
}
OUTPUT:

42. Write a program to accept an integer and reverse it.

INPUT:
#include<stdio.h>
main()

{
int x,remainder,reverse=0;
printf("enter any integer\n");
scanf("%d",&x);
while(x!=0){
remainder=x%10;
reverse=( reverse*10 )+ remainder;
x = x/10;
}
printf("Reverse number= %d\n",reverse);
}
OUTPUT:

39. Write a program to accept N integer numbers and store them


in an array AR. The odd elements in the AR are copied into OAR
and other elements are copied into EAR. Display the contents of
OAR and EAR.

INPUT:
#include<stdio.h>
main()
{
int AR[50],OAR[50],EAR[50];
int i,j=0,k=0,n;
printf("enter the number of elements:\n");
scanf("%d",&n);
printf("enter the elements of the array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&AR[i]);

}
for(i=0;i<n;i++)
{
if(AR[i]%2==0)
{
EAR[j]=AR[i];
j++;
}
else{
OAR[k]=AR[i];
k++;
}
}

printf("the elements of EAR(Even elements in an array):\n");


for(i=0;i<j;i++)
{
printf("%d\n",EAR[i]);
}
printf("the elements of OAR(Odd elements in an array):\n");
for(i=0;i<k;i++)
{
printf("%d\n",OAR[i]);
}
}
OUTPUT:

62) Write a c program to multiply a given number by 4 using bitwise


operators .

INPUT:
#include<stdio.h>
main()
{
int number;
int operater;
printf("enter an integer");
scanf("%d",&number);
operater=number;
number=number<<2;
printf("%d*4=%d\n",operater,number);

}
OUTPUT:

46. Write a program to accept a decimal number and convert it to


binary and count the number of 1’s in the binary number.
INPUT:
#include <stdio.h>
main()
{
int num, decimal_num, remainder, base = 1, binary = 0, number_of_1s =
printf("Enter a decimal integer \n");
scanf("%d", &num);
decimal_num = num;
while (num > 0)
{
remainder = num % 2;
// To count no.of 1s
if (remainder == 1)
{
number_of_1s++;
}
binary = binary + remainder * base;
num = num / 2;
base = base * 10;
}
printf("Input number is = %d\n", decimal_num);
printf("Its binary equivalent is = %d\n", binary);
printf("No.of 1's in the binary number is = %d\n", number_of_1s);
}
OUTPUT:

48. Write a program to swap the contents of two numbers using


bitwise XOR operation. Don’t use either the temporary variable
arithmetic operations.

INPUT:
#include<stdio.h>
main()
{
int a,b;
printf("enter two integers\n");
scanf("%d%d",&a,&b);
printf("\n before swapping a=%d and b=%d",a,b);
a=a^b;
b=a^b;
a=a^b;
printf("\n after swapping a=%ld and b=%ld",a,b);
}
OUTPUT:

You might also like