Lab5 3623

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Assignment – 4 – Lab 5

1. Write a C program that accepts input from the keyboard and counts the positive, negative
integers and zero until * is entered.

CODE - #include<stdio.h>

int main()

int n, positive = 0, negative = 0, zero = 0;

printf("Enter numbers and give -1 to stop\n");

scanf("%d", &n);

while(n!=-1)

if(n > 0)

positive++;

else if(n < -1)

negative++;

else

zero++;

scanf("%d", &n);

printf("\nPositive Numbers: %d\n", positive);

printf("Negative Numbers: %d\n", negative);

printf("Number of zero: %d\n", zero);


return 0;

2. Write a C program to calculate and print xn.

CODE- #include<stdio.h>

int main()

int n,x, product=1;

printf("Enter value for x \n");

scanf("%d", &x);

printf("Enter value for n \n");

scanf("%d", &n);

while(n>0)
{

product= (product*x);

n--;

printf("\n The value of x^n is : %d \n",product);

return 0;

3. Write a C program to print the sum of the digits of a number.

CODE-

#include<stdio.h>

int main()

long int d,n,sum=0;

printf("Enter a number : \n");

scanf("%ld", &n);

while(n>0)

{ d=(n%10);

sum+=d;

n=(n/10);

}
printf("\n The the sum of digits of the number entered is : %ld \n",sum);

return 0;

4. Write a C program to check whether a number is Armstrong or not.

CODE-

#include<stdio.h>

#include<math.h>

int main()

int num,arm=0,num2,rem,num3;

printf("Enter a Number :");

scanf("%d",&num);

int j=0;

num2=num;

num3=num;

while(num>0)

num/=10;

j++;

for(int i=0;i<j;i++)

rem=num2%10;

arm+=pow(rem,j);
num2/=10;

if(arm==num3)

printf("It is an armstrong Number");

else if(arm!=num3)

printf("It is not an armstrong number");

5. Write a C program to print the reverse of a number.

CODE- #include<stdio.h>

int main()

long int d,n,rev=0;

printf("Enter a number : \n");

scanf("%ld", &n);

while(n>0)

{ d=(n%10);

rev=((rev*10)+d);
n=(n/10);

printf("\n The reverse of the number is %ld \n",rev);

return 0;

6. Write a C program to print the sum of ‘n’ numbers.

CODE- #include<stdio.h>

int main()

int n, sum=0;

printf("Enter numbers and give 0 to stop\n");

scanf("%d", &n);

while(n>0)

{ sum+=n;

scanf("%d", &n);

printf("\n Sum of n numbers is %d\n",sum );

return 0;
}

7. Write a C program to print the factorial of a number.

CODE-

#include<stdio.h>

int main()

int n, product=1;

printf("Enter the number to find it's factorial :\n");

scanf("%d", &n);

while(n>0)

{ product=(product*n);

n--;

printf("\n Factorial of the Number entered is %d\n",product );

return 0;

}
8. Write a C program to print all the even and odd numbers of a certain range as indicated by the
user and find their respective sum.

CODE-

#include<stdio.h>

int main()

int a,b,os=0,es=0;

printf("Enter a range\n");

scanf("%d %d", &a,&b);

printf("The Even numbers entered are :");

for(int i=a;i<=b;i++)

if(i%2==0)

{ es+=i;

printf("%d ",i);

printf("\n The Odd numbers entered are :");

for(int i=a;i<=b;i++)

if(i%2!=0)

{ os+=i;

printf("%d ",i);

}
printf("\n Sum of Even numbers is %d\n",es );

printf("\n Sum of Odd numbers is %d\n",os);

return 0;

9. Write a C program to print the binary equivalent of an integer.

CODE-

#include<stdio.h>

int main ()

long int d, bin=0, i=1,r;

printf("Enter a decimal number: ");

scanf("%ld ", &d);

while(d!=0)

r=d%2;

d/=2;

bin+=(r*i);

i*=10;

}
printf("Binary no : %ld \n",bin);

return 0;

10. Write a C program to print the prime factors of a number.

CODE-

#include <stdio.h>

int main()

int n,i=1,c;

printf("Number : ");

scanf("%d",&n);

while(i<=n)

if(n%i==0)

c=0;

for(int j=1;j<=i;j++)

if(i%j==0)

c++;

if(c==2)

printf("%d ",i);

i++;

return 0;
}

11. Write a C program to check whether a number is a power of 2 or not.

CODE-

#include<stdio.h>

int main()

int n;

printf("Enter the number :\n");

scanf("%d", &n);

while(n>1)

if(n%2==0)

{ n/=2; }

else

{ printf("\n The Number is not a power of 2");

goto end;

printf("The number is a power of 2");

end:{printf(" ");}

return 0;

}
12. Write a C program to print the GCD of two numbers.

CODE -

#include<stdio.h>

int main()

int num1,num2,gcd;

printf("Enter two numbers to find GCD :");

scanf("%d",&num1);

scanf("%d",&num2);

for(int i=2;i<=num1,i<=num2;i++)

if(num1%i==0 && num2%i==0)

gcd=i;

printf("The Gcd of %d and %d is %d",num1,num2,gcd);

13. Write a C program to print the prime numbers in a given range.

CODE-
#include<stdio.h>

int prime(int i)

while(i<=1)

return 0;

for(int j=2;j*j<=i;j++)

while(i%j==0)

return 0;

return 1;

int main()

int lower,upper;

printf("Enter the range(lower and upper limit) :");

scanf("%d",&lower);

scanf("%d",&upper);

for(int i=lower;i<=upper;i++)

if(prime(i))

printf("%d \t",i);

}
}

14. Write a C program to print the sum of the series: 1 + x + x2 /2! + x3 / 3! +…

CODE-

#include<stdio.h>

#include<math.h>

int main()

int num,range;

printf("Enter a Number to evaluate the series :");

scanf("%d",&num);

printf("Enter the range :");

scanf("%d",&range);

float series=1;

int fact=1;

for(int i=1;i<=range-1;i++)

fact*=i;

series+=(pow(num,i))/fact;

printf("The value of the series is %f",series);

15. Write a C program to print the sum of the series: x - x3/ 3! + x5 / 5! - ….

CODE -
#include<stdio.h>

#include<math.h>

int factorial(int f)

int fact=1;

for(int i=2;i<=f;i++)

fact*=i;

return fact;

float series1(int x,int n)

int fact;

float s1=x;

for(int i=5;i<=n;i+=4)

fact=factorial(i);

s1+=pow(x,i)/fact;

return s1;

float series2(int x,int n)

int fact;

float s2=0;

for(int i=3;i<=n;i+=4)

{
fact=factorial(i);

s2-=pow(x,i)/fact;

return s2;

int main()

int x,n;

printf("enter the number and range to find the sum of the series :");

scanf("%d",&x);

scanf("%d",&n);

float s1,s2;

int n1=n+(n-1);

s1=series1(x,n1);

s2=series2(x,n1);

float series;

series=s1+s2;

printf("the sum of the series is : %f",series);

16. Write a C program to generate calendar of a month, given the start day of the week and the
number of days in that month.

CODE-

#include<stdio.h>

int main()

{
int d,max,month;

printf("Enter the Start day of the calendar : \n");

printf("1.Sunday \n");

printf("2.Monday \n");

printf("3.Tuesday \n");

printf("4.Wednesday \n");

printf("5.Thursday \n");

printf("6.Friday \n");

printf("7.Saturday \n");

scanf("%d",&d);

printf("Enter the maximum no. of days in the month :");

scanf("%d",&max);

printf("\t\t\t\t\t\tCALENDAR\t\t\t\t\t\t\n");

printf("|\tS\t||\tM\t||\tT\t||\tW\t||\tT\t||\tF\t||\tS\t|\n");

for(int i=1;i<d;i++)

printf("|\t \t|");

for(int i=1;i<=max;i++)

if(d%7==1)

printf("\n");

printf("|\t%d\t|",i);

else

printf("|\t%d\t|",i);

d++;
}

17. Write a C program to print the following patterns:

CODE-

#include<stdio.h>

int main()

int i,j;

for(i=1;i<=5;i++)

for(j=1;j<=i;j++)

printf("*\t");

printf("\n");

}
}

#include<stdio.h>

int main()

int i,j;

for(i=1;i<=5;i++)

for(j=1;j<=i;j++)

printf("%d\t",j);

printf("\n");

#include<stdio.h>

int main()

int i,j;

for(i=1;i<=5;i++)

for(j=1;j<=i;j++)

printf("%d\t",i);

printf("\n");

}
18. Write a C program to print the multiplication table up to ‘n’ numbers.

CODE-

#include<stdio.h>

int main()

int n1,n2,multi;

printf("Enter the number you want the multiplication table for :");

scanf("%d",&n1);

scanf("%d",&n2);

for(int i=1;i<=n1;i++)

multi=n2*i;

printf("%d x %d = %d \n",i,n2,multi);

}
}

Spot Question:

19. Write a C program to print Pascal’s triangle up to a given number of rows.

CODE-

#include<stdio.h>

int factorial(int x)

int fact;

if(x==0)

return 1;

else

return x*factorial(x-1);

void pascal(int row)


{

int n;

n=row;

for(int i=0;i<n;i++)

for(int j=1;j<=n-i;j++)

printf(" ");

for(int j=0;j<=i;j++)

int coef=factorial(i)/(factorial(j)*factorial(i-j));

printf("%3d",coef);

printf("\n");

int main()

int row;

printf("Enter the number of Rows :");

scanf("%d",&row);

pascal(row);

You might also like