Lab5 2023103012
Lab5 2023103012
Lab5 2023103012
//Write a C program that accepts input from the keyboard and counts the
positive, negative integers and zero until -1 is entered.
#include<stdio.h>
int main()
signed int n;
int pos=0,neg=0,zero=0;
scanf(" %d",&n);
while (n!=-1)
{ if (n>0)
{pos+=1;}
else if (n<-1)
{neg+=1;}
else if (n==0)
{zero+=1;}
else
{printf("error.try again.");
break;}
scanf(" %d",&n);
return 0;
}
2. // Write a C program to calculate and print xn.
#include <stdio.h>
#include<math.h>
int main()
int x,n,ans;
scanf("%d",&x);
scanf(" %d",&n);
while(n>2)
x*=x;
n--;
ans=x;
printf("%d",ans);
return 0;
}
3. // Write a C program to print the sum of the digits of a number.
#include<stdio.h>
int main()
int n,sum;
scanf("%d",&n);
while(n>0)
sum+=n%10;
n/=10;
return 0;
}
4. // WWrite a C program to check whether a number is Armstrong or not
#include<stdio.h>
#include<math.h>
int main()
int n,num,n1,sum,dig,i,j;
printf("Enter number:");
scanf("%d",&n);
num=n;
n1=n;
while(n!=0)
{n/=10;
dig+=1;}
i=dig;
while(num>0)
{j=num%10;
sum=sum+pow(j,dig);
num/=10;
if (sum==n1)
else
return 0;
}
5. //Write a C program to print the reverse of a number
#include<stdio.h>
int main()
int n,dig;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{dig=n%10;
n/=10;
printf("%d",dig);}
return 0;
}
6. //Write a C program to print the sum of ‘n’ numbers
#include<stdio.h>
int main()
int n,n1,sum;
scanf("%d",&n);
while(n>0)
scanf("%d",&n1);
sum+=n1;
n--;}
return 0;
}
7. //Write a C program to print the factorial of a number.
#include<stdio.h>
int main()
int num,f=1;
scanf("%d",&num);
while(num>0)
{f*=num;
num--;}
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.
#include<stdio.h>
int main()
int num1,num2,i,j,esum=0,osum=0;
scanf("%d",&num1);
scanf(" %d",&num2);
i=num1;
j=num1;
while(i<=num2)
{if (i%2==0)
{printf("%d,",i);
esum+=i;}
i+=1;}
while(j<=num2)
{if (j%2!=0)
{printf("%d,",j);
osum+=j;}
j+=1;}
printf("\nThe sum of even numbers : %d\nThe sum of odd numbers : %d",esum,osum);
return 0;
}
9. //Write a C program to print the binary equivalent of an integer
#include<stdio.h>
int main()
int n,bin=0,i=1,r,t=1;
scanf("%d",&n);
while(n>0)
{r=n%2;
n=n/2;
bin=bin+(t*r);
t*=10;
printf("%d",bin);
return 0;
}
10. // Write a C program to print the prime factors of a number.
#include <stdio.h>
int main()
int n,i=3;
scanf(" %d",&n);
while(n%2==0)
{printf("2,");
n/=2;}
while(i<=n)
{while (n%i==0)
{printf("%d,",i);
n/=i;}
i+=2;
return 0;
}
11. //Write a C program to check whether a number is a power of 2 or not.
#include <stdio.h>
int main()
int n,i;
scanf(" %d",&n);
while(n>=2)
{i=n%2;
if(i==1)
break;}
n/=2;
if(n==2)
{printf("pow of 2");}
return 0;
}
12. //Write a C program to print the GCD of two numbers.
#include <stdio.h>
int main()
int n1,n2,gcd,i=1;
scanf(" %d",&n1);
scanf(" %d",&n2);
{gcd=i;}
i+=1;
printf("%d",gcd);
return 0;
}
13. //Write a C program to print the prime numbers in a given range
#include <stdio.h>
int main()
scanf("%d", &n1);
scanf("%d", &n2);
i = n1;
{j=2;
while (j <= i)
{if (j == i)
{printf("%d,", i);
break;}
else if (j<i && i%j==0)
{break;}
{j++;
continue;}
i++;
return 0;
14. //Write a C program to print the sum of the series: 1 + x + x^2 /2! + x^3 / 3! +…
#include <stdio.h>
#include <math.h>
int main()
int x,n,i=1,j=1,f=1;
float sum=1;
scanf("%d", &x);
scanf("%d", &n);
{sum=sum+(pow(x,i))/f;
j++;
f=f*j;
i++;
}
printf("\n%f",sum);
return 0;
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.
#include <stdio.h>
int main()
{
int startday,totday,i=1,j=1,n,m=1;
char month[20];
char line[60]="---------------------------";
printf("Enter month name :");
scanf("%s",&month);
printf("Enter starting day (1/2/3/4/5/6/7): ");
scanf("%d", &startday);
printf("Enter total no. of days (28-31): ");
scanf(" %d", &totday);
printf("%s",line);
printf("\n %s\n%s\n",month,line);
printf("S\tM\tT\tW\tT\tF\tS\n");
for(i=1;i<startday;i++)
{printf(" \t");}
while(j<=totday)
{
if (startday%7==1)
{printf("\n");}
printf("%d\t",j);
j++;
startday++;
}
return 0;
}
17.
A) //. Write a C program to print the following patterns:
#include <stdio.h>
int main()
{
int n,i=1,j=1;
printf("Enter no. of rows :");
scanf("%d",&n);
for (i=1;i<=n;i++)
{printf("\n");
for(j=1;j<=i;j++)
{printf("*\t");}
}
return 0;
}
B) //. Write a C program to print the following patterns:
#include <stdio.h>
int main()
{
int n,i=1,j=1;
printf("Enter no. of rows :");
scanf("%d",&n);
for (i=1;i<=n;i++)
{printf("\n");
for(j=1;j<=i;j++)
{printf("%d\t",j);}
}
return 0;
}
C) //. Write a C program to print the following patterns:
#include <stdio.h>
int main()
{
int n,i=1,j=1,m=1;
printf("Enter no. of rows :");
scanf("%d",&n);
for (i=1;i<=n;i++)
{printf("\n");
for(j=1;j<=i;j++)
{printf("%d\t",m);
}
m++;
}
return 0;
}
18. //. Write a C program to print the multiplication table up to ‘n’ numbers.
#include <stdio.h>
int main()
{
int n,m,i,j;
printf("Enter no. : ");
scanf("%d",&n);
printf("Enter the table : ");
scanf("%d",&m);
i=1;
while(i<=m)
{j=n*i;
printf("\n%d * %d = %d",n,i,j);
i++;}
return 0;
}