Practical Assignment 04
Practical Assignment 04
SET A
1. WAP to add first 10 numbers.
#include<stdio.h>
void main()
{
//Declaring Variable
int i=1, sum = 0;
//Calculating Sum
while(i<=10)
{
sum = sum + i;
i++;
}
printf("\n Sum of first 10 Natural Numbers is : %d", sum);
}
Output🡪
Sum of first 10 Natural Numbers is : 55
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", reverse);
return 0;
}
Output🡪
Enter an integer: 4532
Reversed number = 2354
*
**
***
****
*****
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output🡪
Enter the number of rows: 5
*
**
***
****
*****
2. a) WAP to print the square of number(s) repeatedly till 1 is entered by user.
Using do-while loop.
while(count <= N)
{
sum = sum + (count * count);
count++;
}
printf("Sum of squares of numbers from 1 to %d is %d.\n", N, sum);
return 0;
}
Output🡪
Enter the limit
5
Sum of squares of numbers from 1 to 5 is 55.
b) WAP to print following series
a) Fibonacci series of n numbers
#include <stdio.h>
int main() {
int i, n;
#include<stdio.h>
int main()
{
int n,sum=0,i=1;
printf("Enter the range of number:");
scanf("%d",&n);
while(i<=n)
{
sum+=i;
i+=2;
}
printf("The sum of the series = %d",sum);
}
Output🡪
Enter the range of number:9
The sum of the series = 25
SET C
1. WAP to print following pattern
a) for example if n=5 then output should be
*
* *
* * *
* * * *
* * * * *
#include <stdio.h>
int main()
{
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0) {
for (space = 1; space <= rows - i; ++space)
{
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("\n");
}
return 0;
}
Output🡪
Enter the number of rows: 5
*
***
*****
*******
*********