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

Lab Assignment 5

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

Lab Assignment 5

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

Name - Tanmay Shrivastava

Course-BTech(CSE)
Roll No.-24155589

Day 7(Lab Assignments)


Question1)Print the sum of digits of any number.
#include <stdio.h>
int main()
{
Input:
int a,i,A,n,s,j,k,l;
Enter the number=234
n=0;
s=0;
printf("Enter the number:");
scanf("%d",&a);
for(A=a;A>0;n++) OUTPUT:
{ The sum is 9
A=A/10;
}
while(a>0)
{
j=a%10;
a=a/10;
s=s+j;
}
printf("The sum is %d",s);
return 0;
}
Question2)Find the factorial of a number.

#include <stdio.h> INPUT:


int main() Enter the number=5
{
int a,i,s=1;
printf("Enter the number=");
scanf("%d",&a);
for(i=1;i<=a;i++) OUTPUT:
{ The factorial of 5 is 120
s=s*i;
}
printf("The factorial of %d is %d",a,s);
return 0;
}
Question3)Convert a binary number to a decimal number.
#include <stdio.h>
int main()
{
int a,A,i,j,r,d,D; INPUT:
printf("Enter a binary number:");
scanf("%d",&a); Enter the binary
A=a; number=101010
j=1;
D=0;
for(i=0;A>0;j=j*2)
{
r=A%10; OUTPUT:
d=r*j; The binary number=101010
D=D+d;
A=A/10;
The decimal number=42
}
printf("The binary number=%d\n",a);
printf("The decimal number=%d",D);
return 0;
}
Question4)Check whether a number is a palindrome number.
#include <stdio.h>
int main() INPUT:
{
int a,A,s,r; Enter the number=232
printf("Enter the number=");
scanf("%d",&a);
A=a;
r=0;
while(A>0)
{
s=A%10;
r=r*10+s;
A=A/10;
}
if(r==a) OUTPUT:
{
printf("It is a palindrome number"); It is a palindrome number
}
else
{
printf("Not a palindrome number");
}
return 0;
}
Question5)Check whether a 3 digit number is an Armstrong number.
#include <stdio.h>
int main()
{
int a,i,A,c,s;
s=0;
printf("Enter the three digit number="); INPUT:
scanf("%d",&a); Enter the three digit number=153
A=a;
for(i=0;i<=3;i++)
{
c=A%10;
c=c*c*c;
s=s+c;
A=A/10;
} OUTPUT:
if(a==s)
{ It is an armstrong number
printf("It is an armstrong number");
}
else
{
printf("Not a armstrong number");
}
return 0;
}
Question 6)Count the digits of a number using do...while loop

#include <stdio.h>
int main()
{ INPUT:
int a,i; Enter a number=634
i=0;
printf("Enter a number=");
scanf("%d",&a);
do
{ OUTPUT:
i=i+1; The number of digits
a=a/10; are 3
}while(a>0);
printf("The number of digits are %d",i);
return 0;
}
Question 7)Find the sum of numbers entered using do...while loop

#include <stdio.h>
int main()
{
Input:
int a,b,s=0;
printf("Enter the number="); Enter the number=234
scanf("%d",&a);
do
{
b=a%10;
s=s+b; Output:
a=a/10; The sum of digits is 9
} while (a>0);
printf("The sum of digits is
%d",s);
return 0;
}
Question 8)Multiply two positive numbers without using * operator

#include <stdio.h> Input:


int main() Enter the 1st number:2
{ Enter the 2nd number:3
int a,b,s=0,i=1;
printf("Enter the 1st number:");
scanf("%d",&a);
printf("Enter the 2nd number:");
scanf("%d",&b);
do
{ Output:
s=s+a; The multiplication of 2 and 3 is 6
i=i+1;
} while (i<=b);
printf("The multiplication of %d and %d is
%d",a,b,s);
return 0;
}
Question 9)Find the sum of this series upto n terms 1+2+4+7+11+16+….

#include <stdio.h>
int main()
{ Input:
int a,i,s=1,r=0; Enter the number of terms needed:7
printf("Enter the number of terms
needed:");
scanf("%d",&a);
for(i=1;i<=a;i++)
{ Output:
r=r+s; The sum of series till 7 is 63
s=s+i;
}
printf("The sum of series till %d is
%d",a,r);
return 0;
}
Question 10)Generate Fibonacci series 1,1,2,3,5,8,13,34,55,89

#include <stdio.h>
int main()
{ Input:
long a,b,c; Enter the number of terms required:7
int i,j;
printf("Enter the number of terms required:");
scanf("%d",&j);
a=0;
b=1;
printf("%d\t",b);
for(i=0;i<=j;i++)
{ Output:
c=a+b; 1 1 2 3 5 8 13
printf("%d\t",c);
a=b;
b=c;
}
return 0;
}

You might also like