C Programming Assignment 2...
C Programming Assignment 2...
C Programming Assignment 2...
WORKSHEET-2
#include<stdio.h>
int main()
{
printf("List of first 10 natural numbers : \n");
for(int i=1; i<=10; i++)
{
printf("%d\n", i);
}
return 0;
Q2. Write a program in c to display the first 10 natural numbers in reverse order.
#include<stdio.h>
int main()
{
for(int i=10; i>0; i--)
{
printf("%d\n", i);
}
return 0;
Q3. Write a program in C to display the cube of the number up to given an integer.
Test Data:
Input number of terms: 5 Expected
Output :
Number is : 1 and cube of the 1 is :1
Number is : 2 and cube of the 2 is :8
#include<stdio.h>
#include<math.h>
1
int main()
{
int num;
printf("Enter a number\n");
scanf("%d",&num);
printf("Number is : %d and cube of the %d is :%d\n”, num, num,(int)pow(num,
3)); return 0;
#include<stdio.h>
int main()
{
int num;
printf(" Input the number (Table to be calculated):");
scanf("%d", &num);
printf("%d\n", num);
for(int i=1;i<=10;i++)
{
printf("%d * %d = %d\n", i , num, i*num);
}
return 0;
#include<stdio.h>
int main()
{
int num;
printf("Input number of terms\n");
2
scanf("%d", &num);
int i=1;
int sum=0;
while(i<num)
{
printf("%d\t", i);
sum+=i;
i+=2;
}
printf("\n The Sum of odd Natural Number up to %d terms: %d", num, sum);
return 0;
#include<stdio.h>
int main()
{
int num, rev_num=0;
int checker;
int rev;
printf("Enter a number\n");
scanf("%d", &num);
check=num;
while(num!=0)
{
rev = num%10;
rev_num = rev_num*10+rev;
num=num/10;
}
printf("%d\n", rev_num);
if(rev_num == check)
{
printf("The number is Palindrome\n");
}
else
{
printf("The number is not Palindrome\n");
}
return 0;
}
3
Q7. Write a program in C to display series in 5 6 8 11 15 21..... n terms.
Test Data:
Input the number of terms: 5
Expected Output :5 6 8 11 15
#include<stdio.h>
int main()
{
int n;
int y;
printf("Enter the number of terms\n");
scanf("%d",&n);
y=n;
for(int i=0; i<n;i++)
{ y+
=i;
printf("%d\t", y);
}
return 0;
Q8. Write a c program to find the perfect numbers within a given number of ranges. [Hint a
positive integer that is equal to the sum of its proper divisors. Example 6 is a perfect number
as
the divisor of 6 are 1, 2, and 3, their sum is 6]
Test Data:
Input the starting range or number : 1 Input
the ending range of number : 50 Expected
Output :
The Perfect numbers within the given range : 6 28
#include<stdio.h>
int main()
{
int min , max;
int checker=0;
printf("Enter minimum and maximum value in range\n");
scanf("%d %d", &min, &max);
for(int I=min; I<max; i++)
{
for(int j=min; j<max; j++)
{
4
if(i !=j && i % j == 0)
{
checker += j;
}
}
if(checker == i)
{
printf("%d is a perfect number\n", i);
}
checker=0;
}
Q9. Write a program in C to display the pattern like right angle triangle using an asterisk.
*
**
***
****
#include<stdio.h>
int main()
{
int n=4;
for(int i=0; i<n; i++)
{
for(int j=0; j<=i; j++)
{
printf("*");
}
printf("\n");
}
Q10. Write a program in C to display the pattern like right angle triangle using an asterisk.
The pattern like:
*
**
***
****
*****
****
***
**
*
5
#include<stdio.h>
#include<math.h>
int main()
{
int n=9;
for(int i=0 ;i<n/2; i++)
{
for(int j=0; j<=i; j++)
{
printf("*");
}
printf("\n");
printf("\n");
}
for(int i=0; i<n/2+1 ;i++)
{
for(int j=i; j<n/2+1 ;j++)
{
printf("*");
}
printf("\n");
printf("\n");
}
return 0;
}
Q11. Write a program in C to make such a pattern like right angle triangle with a number
which will repeat a number in a row.
The pattern like:
1
22
333
4444
#include<stdio.h>
int main()
{
for(int i=1;i<5;i++)
{
for(int j=1;j<=i; j++)
{
printf("%d ",i);
}
printf("\n");
6
}
return 0;
Q12. Write a program in C to make such a pattern like right angle triangle with number
increased by 1. The pattern like:
1
23
456
7 8 9 10
#include<stdio.h>
int main()
{
int n=1;
for(int i=1; i<5; i++)
{
for(int j=1; j<=i; j++)
{
printf("%d ",n);
n++;
}
printf("\n");
}
return 0;
Q 13. Write a program in C to find the sum of the series 1 - 11 + 111 + .. n terms
Test Data : Input the number
of terms : 5 Expected
Output :
1 - 11 + 111 - 1111 + 11111
The Sum is : 10101
#include<stdio.h>
int main()
{
int n;
printf("Enter the number of terms\n");
7
scanf("%d", &n);
int count = 0;
int num = 1;
count++;
{
printf(" - ");
}
else if (i < n - 1)
{
printf(" + ");
}
} printf("\
n");
return 0;
}