Lab5 3623
Lab5 3623
Lab5 3623
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()
scanf("%d", &n);
while(n!=-1)
if(n > 0)
positive++;
negative++;
else
zero++;
scanf("%d", &n);
CODE- #include<stdio.h>
int main()
scanf("%d", &x);
scanf("%d", &n);
while(n>0)
{
product= (product*x);
n--;
return 0;
CODE-
#include<stdio.h>
int main()
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;
CODE-
#include<stdio.h>
#include<math.h>
int main()
int num,arm=0,num2,rem,num3;
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)
else if(arm!=num3)
CODE- #include<stdio.h>
int main()
scanf("%ld", &n);
while(n>0)
{ d=(n%10);
rev=((rev*10)+d);
n=(n/10);
return 0;
CODE- #include<stdio.h>
int main()
int n, sum=0;
scanf("%d", &n);
while(n>0)
{ sum+=n;
scanf("%d", &n);
return 0;
}
CODE-
#include<stdio.h>
int main()
int n, product=1;
scanf("%d", &n);
while(n>0)
{ product=(product*n);
n--;
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");
for(int i=a;i<=b;i++)
if(i%2==0)
{ es+=i;
printf("%d ",i);
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 );
return 0;
CODE-
#include<stdio.h>
int main ()
while(d!=0)
r=d%2;
d/=2;
bin+=(r*i);
i*=10;
}
printf("Binary no : %ld \n",bin);
return 0;
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;
}
CODE-
#include<stdio.h>
int main()
int n;
scanf("%d", &n);
while(n>1)
if(n%2==0)
{ n/=2; }
else
goto end;
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;
scanf("%d",&num1);
scanf("%d",&num2);
for(int i=2;i<=num1,i<=num2;i++)
gcd=i;
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;
scanf("%d",&lower);
scanf("%d",&upper);
for(int i=lower;i<=upper;i++)
if(prime(i))
printf("%d \t",i);
}
}
CODE-
#include<stdio.h>
#include<math.h>
int main()
int num,range;
scanf("%d",&num);
scanf("%d",&range);
float series=1;
int fact=1;
for(int i=1;i<=range-1;i++)
fact*=i;
series+=(pow(num,i))/fact;
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;
int fact;
float s1=x;
for(int i=5;i<=n;i+=4)
fact=factorial(i);
s1+=pow(x,i)/fact;
return s1;
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;
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("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);
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++;
}
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:
CODE-
#include<stdio.h>
int factorial(int x)
int fact;
if(x==0)
return 1;
else
return x*factorial(x-1);
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;
scanf("%d",&row);
pascal(row);