You are on page 1of 23

Assignment 6:

Looping
Statements

NAME : JEET ARIWALA


ROLL NO. : A-69
1. Print the output for the following series
a) 1 + 4 – 9 + 16 – 25 + 36 …. + n—2

SOURCE CODE:
#include<stdio.h>
#include<math.h>
int main()
{
int s1=1,i=2,n,p;
printf("Please enter n:");
scanf("%d",&n);
while (i<=n)
{
p=pow(-1,i);
s1 = s1 + p*(i*i);
i++;
}
printf("sum of series is: %d \n",s1);
return 0;
}

OUTPUT:-
b) 1^2 + 2^2 + 3^2 + 4^2 + ......... +n^2

SOURCE CODE:

#include <stdio.h>
int main() {
int n,i=1,s=0;
printf("Please enter n:");
scanf("%d",&n);
while (i<=n) {
s=s+(i*i);
i++;
}
printf("sum:%d \n",s);
return 0;
}

OUTPUT:-
c) x – x 3 /3! + x5 /5! – x 7 /7! + x9 /9!...

SOURCE CODE:
#include <stdio.h>
#include <math.h>
int main() {
int n,s=0,f,i=1,d=1,x; // f is -1^x multiple , p is power , d is
denominator
printf("please enter n:");
scanf("%d",&n);
printf("please enter x:");
scanf("%d",&x);
while (i<=n) {
if(i%4==1)
f=1;
else if (i%4==3)
f=-1;
else
f=0;
d=d*i;
s=s+pow(x,i)*f*(1/d);
i++;
}
printf("answer:%d \n",s);
return 0;
}

OUTPUT:-

d) Given Number is Armstrong number or Not. (153 = 13 + 33 + 53)

SOURCE CODE:
#include <stdio.h>
int main() {
int n,s=0,d,b; // n is input number,s is sum,d is digit,b is to save
value of n
printf("Please enter value of n:");
scanf("%d",&n);
b=n;
while (n>0) {
d=n%10;
s=s+d*d*d;
n=n/10;
}
if(s==b){
printf("It is armstrong number.\n");
}else {
printf("not an armstrong number.\n");
}
return 0;
}

OUTPUT:-
e) Given Number is Strong number or Not. (145 = 1! + 4! + 5!)

SOURCE CODE:
#include <stdio.h>
int main() {
int x,i,n,b,fact=1,s=0;
printf("Please input number:");
scanf("%d",&n);
b=n;
while(n>0){
i=n%10;
fact=1;
for(x=1;x<=i;x++){
fact=fact*x;
}
s=s+fact;
n=n/10;
}
if(s==b){
printf("It is a strong number.\n");
}
else {
printf("It is not a strong number.\n");
}
return 0;
}
OUTPUT:-

2. Print the following patterns: (Nested for loops)

A) *

**

***

****

SOURCE CODE:

#include <stdio.h>

int main() {

int a,b,rows=4,space;

for(a=1;a<=rows;a++){

for(space=1;space<=(rows-a);space++){

printf(" ");

for (b=1;b<=a;b++){

printf("* ");

printf("\n");

return 0;

}
OUTPUT:-

B) 12345

1234

123

12

SOURCE CODE:

#include <stdio.h>

int main() {

int a,b,row=5;

for (a=row;a>=1;a--){

for(b=1;b<=a;b++){

printf("%d",b);

printf("\n");

return 0;
}

OUTPUT:-

SOURCE CODE:

#include <stdio.h>

int main() {

int a,b,rows=5;

for(a=1;a<=rows;a++){

for(b=a;b>=1;b--){

printf("%d ",b*b);
}

printf("\n");

return 0;

OUTPUT:-

SOURCE CODE:
#include <stdio.h>
int main() {
int i,j,space,rows=5;
for (i=1;i<=rows;i++){
for(j=1;j<=i;j++){
printf("%d",j);
}
for (space=1;space<=11-(2*i);space++){
printf(" ");
}
for(j=i;j>=1;j--){
printf("%d",j);
}
printf("\n");
}
return 0;
}

OUTPUT:-
SOURCE CODE:
#include<stdio.h>
int main()
{

int i,j,n;
printf("Enter n:");
scanf("%d",&n);
for(i=1;i<=n;i++){

for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
for(i=1;i<=n;i++){
for(j=1;j<=(n-i);j++)
{
printf("%d",j);
}
printf("\n");
}
return 0;
}

OUTPUT:-
SOURCE CODE:
3. Write a program to do the following task :

a) Accept any 2 positive numbers, say n1 and n2. Assume n1 > n2

b) Print all even numbers that lie between n1 and n2.

c) Print the total number of even numbers between n1 and n2.

SOURCE CODE:
#include <stdio.h>

int main() {

int a,b,count=0;

printf("Please enter two numbers : \n");

scanf("%d",&a);

scanf("%d",&b);

if(a>b){

for(int i=b;b<=a;b++)

printf("Even numbers are:%d",b);

printf("\n");

if(b%2==0)

count++;

else if(b>a){

for(int i=a;a<=b;a++){

printf("Even numbers are:%d",a);


printf("\n");

if(a%2==0)

count++;

else{

printf("%d",a);

printf("Count of even numbers %d",count);

return 0;

OUTPUT:-
4. Write a program to calculate the sum of the square of each digit of the given number.

[E.G. 4534 -> 42 + 52 + 32 + 42 = 66]

SOURCE CODE:
#include <stdio.h>
int main() {
int n,s=0,d;
printf("Please enter a number:");
scanf("%d",&n);
while (n>0) {
d=n%10;
s=s+d*d;
n=n/10;
}
printf("sum of square of digits is = %d",s);
return 0;
}

OUTPUT:-

5. Accept 2 four-digit positive integers then calculate and display the sum of the product of each pair
of digits occupying the same position in the two numbers.

[E.G. if the first number is 3445 and the second number is 4534 then the output will be 64.]
(Example: 3 * 4 + 4 * 5 + 4 * 3 + 5 * 4 = 64)

SOURCE CODE:
#include <stdio.h>
int main() {
int n1,n2,d1,d2,s=0;
printf("please enter two digits: \n");
scanf("%d",&n1);
scanf("%d",&n2);
while(n1>0 && n2>0){
d1=n1%10;
d2=n2%10;
s=s+(d1*d2);
n1=n1/10;
n2=n2/10;
}
printf("sum : %d\n",s);
return 0;
}

OUTPUT:-

Competitive problem
6. Given two integers L and R where L ≤ R, the task is to find an integer K such that: L ≤ K ≤ R. All the
digits of K are distinct.

The value of the expression (L – K) * (K – R) is maximum.

If multiple answers exist then choose the larger value for K.

SOURCE CODE:
7. Count of triples (A, B, C) where A*C is greater than B*B Given three integers A, B and C.

The task is to count the number of triples (a, b, c) such that a * c > b2, where 0 < a <= A, 0 < b <= B
and 0 < c <= C.

SOURCE CODE:
#include <stdio.h>
int main()
{
int a,b,c,i,j,k,count=0;
printf("Enter values of a,b,c:");
scanf("%d %d %d",&a,&b,&c);
for(i=1;i<=a;i++)
{
for(j=1;j<=b;j++)
{
for(k=1;k<=c;k++)
{
if(i*k>j*j)
{
printf("%d,%d,%d \n",i,j,k);
count++;
}
}
}
}
printf("\n");
printf("%d",count);
return 0;
}

OUTPUT:-

You might also like