C
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;
printf("Enter a positive integer: ");
scanf("%d",&number);
printf("Factors of %d are: ", number);
for(i=1; i <= number; ++i)
{
if (number%i == 0)
{
printf("%d ",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;
printf("\n Please Enter any number\n");
scanf("%d", &Number);
while(Number > 0)
{
Number = Number / 10;
Count = Count + 1;
}
printf("\n Number of Digits in a Given Number = %d", Count);
return 0;
}
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;
printf("Enter the number \n");
scanf("%ld", &num);
temp = num;
while (num > 0)
{
digit = num % 10;
sum = sum + digit;
num /= 10;
}
printf("Given number = %ld\n", temp);
printf("Sum of the digits %ld = %ld\n", temp, sum);
}
Output:
23. Write a program to reverse a number
#include <stdio.h>
int main()
{
int n, reversedNumber = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
printf("Reversed Number = %d", reversedNumber);
return 0;
}
Output:
24. Write a program to print all Armstrong numbers under 1000
#include <stdio.h>
main()
{
int number, temp, digit1, digit2, digit3;
printf("Print all Armstrong numbers between 1 and 1000:\n");
number = 001;
while (number <= 900)
{
digit1 = number - ((number / 10) * 10);
digit2 = (number / 10) - ((number / 100) * 10);
digit3 = (number / 100) - ((number / 1000) * 10);
temp = (digit1 * digit1 * digit1) + (digit2 * digit2 * digit2) + (digit3 * digit3 *
digit3);
if (temp == number)
{
printf("\n Armstrong no is:%d", temp);
}
number++;
}
}
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);
// maximum number between n1 and n2 is stored in minMultiple
minMultiple = (n1>n2) ? 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;
/* Input two numbers from user */
printf("Enter any two numbers to find HCF: ");
scanf("%d%d", &num1, &num2);
/* Find minimum between two numbers */
min = (num1<num2) ? num1 : num2;
for(i=1; i<=min; i++)
{
/* If i is factor of both number */
if(num1%i==0 && num2%i==0)
{
hcf = i;
}
}
printf("HCF of %d and %d = %d\n", num1, num2, hcf);
return 0;
}
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);
/*logic*/ for (i = 1; i <= n; i++) {
if (n % i == 0) {
c++;
}
}
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);
printf("Prime numbers between %d and %d are: ", low, high);
while (low < high)
{
flag = 0;
for(i = 2; i <= low/2; ++i)
{
if(low % i == 0)
{
flag = 1;
break;
}
}
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;
/* Input number from user */
printf("Enter number of terms: ");
scanf("%d", &terms);
/* Fibonacci magic initialization */
a = 0;
b = 1;
c = 0;
printf("Fibonacci terms: \n");
/* Iterate through n terms */
for(i=1; i<=terms; i++)
{
printf("%d, ", c);
a = b; // Copy n-1 to n-2
b = c; // Copy current to n-1
c = a + b; // New term
}
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("\n Enter a number ");
scanf("%d",&N);
while(N)
{
for(i=2;i<x;i++)
if(x%i==0)
break;
if(i==x)
{
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
a) Add two numbers
b) Subtract two numbers
c) Multiply two numbers
d) Divide two numbers
e) Exit
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{int a, b, result, ch;
while(1) // condition is always true, thus an infinite loop
printf("\n1. Addition");
printf("\n2. Subtraction");
printf("\n3. Multiplication");
printf("\n4. Division");
printf("\n5. Exit");
printf("\n\nEnter your choice");
scanf("%d",&ch);
switch(ch)
case 1:printf("Enter two numbers");
scanf("%d%d",&a,&b);
result=a+b;
printf("Sum is %d", result);
break; //it is used to move control out of switch body
case 2:printf("Enter two numbers");
scanf("%d%d",&a,&b);
result=a-b;
printf("Difference is %d", result);
break; //it is used to move control out of switch body
case 3:printf("Enter two numbers");
scanf("%d%d",&a,&b);
result=a*b;
printf("Product is %d", result);
break; //it is used to move control out of switch body
case 4:printf("Enter two numbers");
scanf("%d%d",&a,&b);
result=a/b;
printf("Quotient is %d", result);
break; //it is used to move control out of switch body
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()
{int ch, i,n,fact=1, c = 0,a;
while(1) // condition is always true, thus an infinite loop
printf("\n1. Factorial of a number");
printf("\n2. Prime or not");
printf("\n3. Odd or even ");
printf("\n4. Exit");
printf("\n\nEnter your choice");
scanf("%d",&ch);
switch(ch)
{
case 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);
break; //it is used to move control out of switch body
case 2:printf("Enter any number n:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
if (n % i == 0)
c++;
if (c == 2) {
printf("%d is a Prime number",n);
else {
printf("%d is not a Prime number",n);
}
break; //it is used to move control out of switch body
case 3:printf("\n Enter a number :");
scanf("%d",&a);
if(a%2==0)
printf("\nthe number %d is even ",a);
else
printf("\n The number %d is odd ",a);
break; //it is used to move control out of switch body
case 4:exit(0);
default:printf("Invalid Entry") }}}
Output: