C Assignment Solve 2
C Assignment Solve 2
C Assignment Solve 2
Lab Solution
Assignment – 2
Created By
Rupali Banerjee
And
Partha Sarathi Chatterjee
1. Write a program to print your name 5 times on the screen.
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for(i=1;i<=5;i++)
{
printf("\n My name is Tapan ");
}
return 0;
Output:
2. Write a program to print first 10 natural numbers
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
printf("\nThe first 10 natural numbers are....\n\n");
for(i=1;i<=10;i++)
{
printf("%d ",i);
}
return 0;
}
Output:
3. Write a program to print first 10 natural numbers in reverse order.
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
printf("\nThe first 10 natural numbers in reverse order are....\n\n");
for(i=10;i>=1;i--)
{
printf("%d ",i);
}
return 0;
}
Output:
4. Write a program to print first N natural numbers. (N is given by user)
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i;
printf("Enter a number: ");
scanf("%d",&n);
i=1;
do
{
printf("%d ",i);
i++;
} while(i<=n);
return 0;
}
Output:
5. Write a program to print first N natural numbers in reverse order. (N is
given by user)
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i;
printf("Enter a number: ");
scanf("%d",&n);
i=n;
do
{
printf("%d ",i);
i--;
} while(i>=1);
return 0;
}
Output:
6. Write a program to print first 10 even natural numbers.
#include<stdio.h>
#include<conio.h>
int main()
{
int n=1,count=1;
while(count<=10)
{
if(n%2==0)
{
printf("%d ",n);
count=count+1;
}
n=n+1;
}
return 0;
}
Output:
7. Write a program to print first 10 odd natural numbers.
#include<stdio.h>
#include<conio.h>
int main()
{
int n=1,count=1;
while(count<=10)
{
if(n%2!=0)
{
printf("%d ",n);
count=count+1;
}
n=n+1;
}
return 0;
}
Output:
8. Write a program to print first N even natural numbers.
#include<stdio.h>
#include<conio.h>
int main()
{
int n=1,i=1,count;
printf("\n Enter the value of N ");
scanf("%d",&count);
while(i<=count)
{
if(n%2==0)
{
printf("%d ",n);
i=i+1;
}
n=n+1;
}
return 0;
}
Output:
9. Write a program to print first N odd natural numbers.
#include<stdio.h>
#include<conio.h>
int main()
{
int n=1,i=1,count;
printf("\n Enter the value of N");
scanf("%d",&count);
while(i<=count)
{
if(n%2!=0)
{
printf("%d ",n);
i=i+1;
}
n=n+1;
}
return 0;
}
Output:
10. Write a program to print first N even natural numbers in reverse order
#include<stdio.h>
#include<conio.h>
int main()
{ int N;
printf("Enter a natural number");
scanf("%d",&N);
while(N)
{
printf("%d\t",2*N);
N--;
}
getche();
return 0;
}
Output:
11. Write a program to print first N odd natural numbers in reverse order
#include<stdio.h>
#include<conio.h>
int main()
{
int N;
printf("\n Enter a natural number ");
scanf("%d",&N);
while(N)
{
printf("%d ",2*N-1);
N--;
}
return 0;
}
Output:
12. Write a program to print table of user’s choice
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n;
printf("\n Enter your choice for the table :");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
printf("\n %d X %d = %d \n",n,i,n*i);
}
return 0;
}
Output:
13. Write a program to calculate sum of first N natural numbers
#include<stdio.h>
#include<conio.h>
int main()
{
int i,sum=0;
for(i=1;i<=10;i++) //here N is 10
{
sum=sum+i;
}
printf("\n The result is %d ",sum);
return 0;
}
Output:
14. Write a program to calculate product of first N natural numbers
#include<stdio.h>
#include<conio.h>
int main()
{
int i,product=1;
for(i=1;i<=10;i++) //here N is 10
{
product=product*i;
}
printf("\n The result is %d ",product);
return 0;
}
Output:
15. Write a program to calculate factorial of a number
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,fact=1;
printf("\n Enter a number :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("\n The factorial of %d is %d ",n,fact);
return 0;
}
Output:
16. Write a program to find the factor of a number
#include <stdio.h>
int main()
{
int number, i;
return 0;
}
Output:
17. Write a program to check weather a number is perfect or not.
/* Perfect number is a number which is equal to sum of its divisor. For eg,
divisors of 6 are 1,2 and 3. The sum of these divisors is 6. So 6 is called as perfect
number.*/
#include <stdio.h>
int main()
{
int number, rem, sum = 0, i;
printf("Enter a Number\n");
scanf("%d", &number);
for (i = 1; i <= (number - 1); i++)
{
rem = number % i;
if (rem == 0)
{
sum = sum + i;
}
}
if (sum == number)
printf("Entered Number is perfect number");
else
printf("Entered Number is not a perfect number");
return 0;
}
Output:
18. Write a program to calculate sum of first N even natural numbers
#include<stdio.h>
#include<conio.h>
int main()
{
int n=1,i=1,count,sum=0;
printf("\n Enter a natural number ");
scanf("%d",&count);
while(i<=count)
{
if(n%2==0)
{
printf("%d ",n);
i=i+1;
sum=sum+n;
}
n=n+1;
}
printf("\n The sum of even numbers is %d",sum);
return 0;
}
Output:
19. Write a program to calculate sum of first N odd natural numbers
#include<stdio.h>
#include<conio.h>
int main()
{
int n=1,i=1,count,sum=0;
printf("\n Enter a natural number ");
scanf("%d",&count);
while(i<=count)
{
if(n%2!=0)
{
printf("%d ",n);
i=i+1;
sum=sum+n;
}
n=n+1;
}
printf("\n The sum of odd numbers is %d",sum);
return 0;
}
Output:
20. Write a program to calculate x power y.(when user input x is 2 and y is
3 then the result will be 8)
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y,pr,i=1,count,sum=0;
printf("\n Enter base : ");
scanf("%d",&x);
printf("\n Enter power : ");
scanf("%d",&y);
pr*x;
while(i<=y-1)
{
pr=pr*x;
i=i+1;
}
printf("\n The result of base %d and power %d is %d",x,y,pr);
return 0;
}
Output:
21. Write a program to count digits in a given number.
#include <stdio.h>
int main()
{
int Number, Reminder, Count=0;
while(Number > 0)
{
Number = Number / 10;
Count = Count + 1;
}
Output:
22. Write a program to calculate sum of the digits of a given number
include <stdio.h>
void main()
{
long num, temp, digit, sum = 0;
Output:
23. Write a program to reverse a number
#include <stdio.h>
int main()
{
int n, reversedNumber = 0, remainder;
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
return 0;
}
Output:
24. Write a program to print all Armstrong numbers under 1000
#include <stdio.h>
main()
{
int number, temp, digit1, digit2, digit3;
Output:
25. Write a program to calculate LCM of two numbers
#include <stdio.h>
int main()
{
int n1, n2, minMultiple;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
// Always true
while(1)
{
if( minMultiple%n1==0 && minMultiple%n2==0 )
{
printf("The LCM of %d and %d is %d.", n1, n2,minMultiple);
break;
}
++minMultiple;
}
return 0;
}
Output:
26. Write a program to calculate HCF of two numbers.
#include <stdio.h>
int main()
{
int i, num1, num2, min, hcf=1;
Output:
27. Write a program to check whether a given number is prime or not
#include <stdio.h>
main() {
int n, i, c = 0;
printf("Enter any number n:");
scanf("%d", &n);
if (c == 2) {
printf("%d is a Prime number",n);
}
else {
printf("%d is not a Prime number",n);
}
return 0;
}
Output:
28. Write a program to print all prime numbers between two given
numbers
#include <stdio.h>
int main()
{
int low, high, i, flag;
printf("Enter two numbers(intervals): ");
scanf("%d %d", &low, &high);
if (flag == 0)
printf("%d ", low);
++low;
}
return 0;
}
Output:
29. Write a program to print all prime factors of a given number. [For
example prime factors of 36 are 2,2,3,3]
# include <stdio.h>
# include <math.h>
int main()
{
int i,n = 36;
while (n%2 == 0)
{
printf("%d ", 2);
n = n/2;
}
for (i = 3; i <= sqrt(n); i = i+2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
printf("%d ", i);
n = n/i;
}
}
if (n > 2)
printf ("%d ", n);
return 0;
}
Output:
30. Write a program to print first N terms of Fibonacci series
#include <stdio.h>
int main()
{
int a, b, c, i, terms;
return 0;
}
Output:
31. Series Problems Program in C 1+(1+2)+(1+2+3)+…+(1+2+3+…+N)
#include<stdio.h>
int main()
{
int N,i,n,s,sum=0;
printf("\n Enter the nth term ");
scanf("%d",&N);
for(n=1;n<=N;n++)
{
s=0;
for(i=1;i<=n;i++)
s=s+i;
sum=sum+s;
}
printf("%d",sum);
return 0;
}
Output:
32. Write a program to find the sum of series:
1+1/2+1/3+………..+1/n
#include<stdio.h>
int main()
{
int N,i;
float n, sum=0;
printf("\n Enter the nth term ");
scanf("%d",&N);
for(n=1;n<=N;n++)
{
sum=sum+(1/n);
}
printf("sum of the series is %f",sum);
return 0;
}
Output:
33. Series Problems Print first N Prime numbers
#include<stdio.h>
int main()
{
int N,i,x=2;
printf("%d\t",x);
N--;
}
x++;
}
Output:
34.Print like this pattern
1
23
456
7 8 9 10
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k=1;
for(i=1;i<=4;i++)
{
for(j=1;j<=5;j++)
{
if(j<=i)
{
printf("%d",k);
k++;
}
else
printf(" ");
}
printf("\n");
}
}
Output:
35.Print like this pattern:
0
12
345
6789
01234
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k=0;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if(j<=i)
{
printf("%d",k);
if(k<9)
k++;
else
k=0;
}
else
printf(" ");
}
printf("\n");
}
}
Output:
36.Print Star pattern *
* *
* * *
* * * *
* * * * *
#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if(j<=i)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
Output:
37.Print Star pattern *
* *
* * *
* * * *
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k;
for(i=1;i<=4;i++)
{
for(j=1;j<=7;j++)
{
if(((j>=5-i&& j<=3+i) && k))
{
printf("*");
k=0;
}
else
{
printf(" ");
k=1;
}
}
printf("\n");
}
}
Output:
38. Print pattern * * * * *
* * * *
* * *
* *
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if(j>=i)
{ printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
Output:
39.Print pattern * * * * *
* * * *
* * *
* *
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if(j<=6-i)
{ printf("*");
}
else
{ printf(" "); }
}
printf("\n");
}
}
Output:
40.Print pattern
* * *
* * * * *
* * * * * * *
#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=9;j++)
{
if(j>=6-i&& j<=4+i)
{ printf("*"); }
else
{printf(" ");}
}
printf("\n");
}
}
Output:
41. Write a menu driven program which has following options
e) Exit
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
printf("\n1. Addition");
printf("\n2. Subtraction");
printf("\n3. Multiplication");
printf("\n4. Division");
printf("\n5. Exit");
scanf("%d",&ch);
switch(ch)
result=a+b;
scanf("%d%d",&a,&b);
result=a-b;
scanf("%d%d",&a,&b);
result=a*b;
scanf("%d%d",&a,&b);
result=a/b;
case 5:exit(0);
default:printf("Invalid Entry"); }
}
Output:
42. Write a menu driven program which has following options
a) Factorial of a number
b) Prime or not
c) Odd or even
d) Exit.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
printf("\n4. Exit");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n Enter a number :");
scanf("%d",&n);
for(i=1;i<=n;i++)
fact=fact*i;
scanf("%d", &n);
if (n % i == 0)
c++;
if (c == 2) {
else {
}
break; //it is used to move control out of switch body
scanf("%d",&a);
if(a%2==0)
else
case 4:exit(0);
Output: