[go: up one dir, main page]

0% found this document useful (0 votes)
44 views25 pages

Lab5 2023103012

The document contains 19 C program code snippets that perform various tasks like accepting user input, counting positive/negative/zero integers, calculating powers and factorials, checking if a number is prime, Armstrong, or a power of 2, printing patterns, multiplication tables, and Pascal's triangle. The code snippets demonstrate basic programming concepts like loops, functions, conditionals, and taking user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views25 pages

Lab5 2023103012

The document contains 19 C program code snippets that perform various tasks like accepting user input, counting positive/negative/zero integers, calculating powers and factorials, checking if a number is prime, Armstrong, or a power of 2, printing patterns, multiplication tables, and Pascal's triangle. The code snippets demonstrate basic programming concepts like loops, functions, conditionals, and taking user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

1.

//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;

printf("Enter number: ");

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;}

printf("Enter number: ");

scanf(" %d",&n);

printf("No. of positives: %d\nNo.of negatives: %d\nNo. of zeroes : %d",pos,neg,zero);

return 0;

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

#include <stdio.h>

#include<math.h>

int main()

int x,n,ans;

printf("Enter the number:");

scanf("%d",&x);

printf("Enter the power: ");

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;

printf("Enter a number : ");

scanf("%d",&n);

while(n>0)

sum+=n%10;

n/=10;

printf("the digit sum is : %d",sum);

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)

{printf("%d is an armstrong number",n1);}

else

{printf("%d is not an armstrong number",n1);}

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;

printf("Enter number of digits to add:");

scanf("%d",&n);

while(n>0)

{printf("Enter number : ");

scanf("%d",&n1);

sum+=n1;

n--;}

printf("The sum is : %d",sum);

return 0;

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

#include<stdio.h>

int main()

int num,f=1;

printf("Enter number : ");

scanf("%d",&num);

while(num>0)

{f*=num;

num--;}

printf("The factorial is : %d",f);

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;

printf("Enter start of range: ");

scanf("%d",&num1);

printf("Enter end of range: ");

scanf(" %d",&num2);

i=num1;

j=num1;

printf("Even numbers in range : ");

while(i<=num2)

{if (i%2==0)

{printf("%d,",i);

esum+=i;}

i+=1;}

printf("\nOdd numbers in range : ");

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;

printf("Enter integer : ");

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;

printf("Enter number : ");

scanf(" %d",&n);

printf("The prime factors are : ");

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;

printf("Enter number : ");

scanf(" %d",&n);

while(n>=2)

{i=n%2;

if(i==1)

{printf("not pow of 2");

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;

printf("Enter first number : ");

scanf(" %d",&n1);

printf("Enter second number : ");

scanf(" %d",&n2);

while(i<=n1 && i<=n2)

{if (n1%i==0 && n2%i==0)

{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()

int n1, n2, i, j = 2;

printf("Enter first number: ");

scanf("%d", &n1);

printf("Enter second number: ");

scanf("%d", &n2);

i = n1;

while (i <= n2)

{j=2;

while (j <= i)

{if (j == i)

{printf("%d,", i);

break;}
else if (j<i && i%j==0)

{break;}

else if (j<i && i%j!=0)

{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;

printf("Enter base: ");

scanf("%d", &x);

printf("Enter power: ");

scanf("%d", &n);

while(i<=n && j<=n)

{sum=sum+(pow(x,i))/f;

j++;

f=f*j;

i++;
}

printf("\n%f",sum);

return 0;

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


#include <stdio.h>
#include <math.h>
int main()
{
int x,n,i=1,j=1,f=1,fact=1;
printf("Enter base: ");
scanf("%d", &x);
printf("Enter number of terms: ");
scanf("%d", &n);
float sum=0;
for(i=1,j=1;j<=n;i+=2,j++)
{while(f<=i)
{fact=fact*f;
f++;}
if(j%2==0)
{sum=sum-pow(x,i)/fact;}
else if(j%2!=0)
{sum=sum+pow(x,i)/fact;}
f=1;
fact=1;}
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;
}

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


#include <stdio.h>
int main() {
int n, coef;
printf("Enter the number of rows for Pascal's Triangle: ");
scanf("%d", &n);
for (int line = 1; line <= n; line++)
{coef = 1;
for (int space = 1; space <= n - line; space++)
printf(" ");
for (int i = 1; i <= line; i++)
{printf("%4d", coef);
coef = coef * (line - i) / i;
}
printf("\n");}
return 0;
}

You might also like