0% found this document useful (0 votes)
6 views30 pages

C Programming: Loops and Functions Examples

Uploaded by

nayanarafat009
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views30 pages

C Programming: Loops and Functions Examples

Uploaded by

nayanarafat009
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q-1 Reverse digits of an integer using while loop.

#include <stdio.h>
int main()
{
int n, r=0, d;
printf("Enter number: ");
scanf("%d",&n);

while(n!=0)
{
d = n % 10;
r = r*10 + d;
n = n / 10;
}

printf("Reversed = %d\n", r);


return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-2 Print factorial table from 1! to m!.

#include <stdio.h>
int main()
{
int m,i;
unsigned long long fact = 1;
printf("Enter m: ");
scanf("%d",&m);

for(i=1;i<=m;i++)
{
fact *= i;
printf("%2d! = %llu\n", i, fact);
}

return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-3 Print first n Fibonacci numbers using do…while loop.

#include <stdio.h>
int main()
{
int n, i=0;
unsigned long a=0,b=1,next;
printf("Number of terms: ");
scanf("%d",&n);

do
{
if(i==0)
next = 0;
else if(i==1)
next = 1;
else
{
next = a + b;
a = b;
b = next;
}
printf("%llu ", next);
i++;
}
while(i<n);

printf("\n");
return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-4 Print 2-D table of y = exp(-x) for x = 0.0 to 9.9.

#include <stdio.h>
float my_pow(float a, int n)
{
float r = 1.0f;
while (n--) r *= a;
return r;
}

float my_fact(int n)
{
float r = 1.0f;
for (; n > 1; n--) r *= n;
return r;
}

float my_exp_neg(float x)
{
float sum = 0.0f;
int k;
for (k = 0; k < 12; k++)
sum += my_pow(-x, k) / my_fact(k);
return sum;
}

int main()
{
int row, col;
printf("Table y = exp(-x)\n");

for (row = 0; row <= 9; row++)


{
for (col = 0; col <= 9; col++)
{
float x = row + col / 10.0f;
printf("%7.4f", my_exp_neg(x));
}
printf("\n");
}

return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Yasin Arafat, ID: 22-0-52-020-014
Q-6 Prepare table showing compound interest for P=1000–10000, r=0.10–
0.20, n=1–10.

#include <stdio.h>
int main()
{
int P;
float r;
int n, pr;
printf(" P r n Amount\n");
for(P = 1000; P <= 10000; P += 1000)
{
for(pr = 10; pr <= 20; pr++)
{
r = pr / 100.0f;
for(n = 1; n <= 10; n++)
{
float amount = P;
int i;
for(i = 0; i < n; i++)
amount = amount * (1.0f + r);
printf("%4d %4.2f %2d %10.2f\n", P, r, n, amount);
}
}
}
return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-7 Convert integer to binary using repeated division by 2.

#include <stdio.h>
int main()
{
unsigned int n;
int bits[32], i=0;
printf("Enter non-negative integer: ");
scanf("%u",&n);

if(n==0)
{
printf("Binary: 0\n");
}

while(n>0)
{
bits[i++] = n % 2;
n /= 2;
}

printf("Binary: ");
for(i=i-1;i>=0;i--)
printf("%d", bits[i]);
printf("\n");

return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-8 Print sequence -4, -2, 0, 2, 4 using for loop.

#include <stdio.h>
int main()
{
int i;
for(i=-4;i<=4;i+=2)
printf("%d ", i);
printf("\n");
return 0;
}

¨ Demonstrate break by reducing m by 10 until m < 10.

#include <stdio.h>
int main()
{
int m, n = 0;
printf("Enter m: ");
scanf("%d", &m);
while(n==0)
{
if(m < 10)
break;
m = m - 10;
}
printf("m = %d\n", m);
return 0;

Yasin Arafat, ID: 22-0-52-020-014


Q-9 Demonstrate continue inside do…while loop.

#include <stdio.h>
int main()
{
int m = 0;

do
{
if(m > 10)
{
m = m + 10;
continue;
}
m = m + 10;
}
while(m < 50);

printf("m = %d\n", m);


return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-10 Use an infinite for(;;) loop and break after a few iterations.

#include <stdio.h>
int main()
{
int i=0;

for(;;)
{
if(i>=5)
break;
printf("Tick %d\n", i+1);
i++;
}

return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-11 Print multiplication table using nested do…while loops.

#include <stdio.h>
int main()
{
int row=1, column, COLMAX=10, ROWMAX=12;

do
{
column = 1;
do
{
printf("%4d", row*column);
column++;
}
while(column<=COLMAX);
printf("\n");
row++;
}
while(row<=ROWMAX);

return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-12 Print pyramid of numbers using absolute values.

#include <stdio.h>
int main()
{
int num, y, i;
printf("Enter number: ");
scanf("%d", &num);

for(y=0;y<=num;y++)
{
for(i= -y;i<=y;i++)
printf("%2d", (i<0)? -i : i);
printf("\n");
}

return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-13 Convert a for loop into equivalent while loop.

#include <stdio.h>
int main()
{
int m;
printf("Enter m: ");
scanf("%d", &m);

while(m<10)
{
printf("%d ", m);
m = m+1;
}

return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-14 Compute factorial using for loop.

#include <stdio.h>
int main()
{
int m,i;
unsigned long long fact=1;
printf("Enter m: ");
scanf("%d",&m);

for(i=1;i<=m;i++)
{
fact *= i;
printf("%2d! = %llu\n", i, fact);
}

return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-15 Print Fibonacci numbers up to limit N.

#include <stdio.h>
int main()
{
int a=0,b=1,next;
int N;
printf("Enter N: ");
scanf("%d",&N);
printf("%d ", a);

if(N==0)
{
printf("\n");
return 0;
}
printf("%d ", b);

while(1)
{
next = a + b;
if(next > N)
break;
printf("%d ", next);
a = b;
b = next;
}

printf("\n");
return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-16 Create table of y = exp(-x) for x = 0.0 to 1.0 with step 0.1.

#include <stdio.h>
float my_pow(float a, int n)
{
float r = 1.0f;
while (n--)
r *= a;
return r;
}
float my_fact(int n)
{
float r = 1.0f;
for (; n > 1; n--)
r *= n;
return r;
}
float my_exp_neg(float x)
{
float sum = 0.0f;
int k;
for (k = 0; k < 12; k++)
sum += my_pow(-x, k) / my_fact(k);
return sum;
}

int main()
{
float x;
for (x = 0.0f; x <= 1.0f; x += 0.1f)
printf("%4.1f %7.5g\n", x, my_exp_neg(x));
return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-17 Display binary digits of a number.

#include <stdio.h>
int main()
{
unsigned int n;
int rem[32], i=0;
printf("Enter integer: ");
scanf("%u",&n);

if(n==0)
{
printf("0\n");
return 0;
}

while(n)
{
rem[i++] = n % 2;
n /= 2;
}

for(i=i-1;i>=0;i--)
printf("%d", rem[i]);
printf("\n");

return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-18 Use continue to skip multiples of 3 in a loop.

#include <stdio.h>
int main()
{
int n,i;
printf("Enter n: ");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
if(i%3==0)
continue;
printf("%d ", i);
}

return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-19 Exit nested loops when sum == 2 using break or goto.

#include <stdio.h>
int main()
{
int m,n,p;

for(m=0; m<3; m++)


{
for(n=0; n<3; n++)
{
for(p=0; p<3; p++)
{
if(m + n + p == 2)
goto print;
}
}
}

print:
printf("%d, %d, %d\n", m, n, p);
return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-20 Use do…while loop ensuring body executes at least once.

#include <stdio.h>
int main()
{
int m;
printf("Enter m: ");
scanf("%d", &m);

do
{
printf("Inside do once: m=%d\n", m);
m++;
}
while(m<0);

return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-21 C program to generate Fibonacci Sequence Up to a Certain
Number.

#include <stdio.h>
int main()
{
int t1=0, t2 = 1, nextTerm = 0, n;
printf("Enter a positive number: ");
scanf("%d", &n);

printf("Fibonacci Series: %d, %d, ", t1, t2);


nextTerm = t1 + t2;

while(nextTerm <= n)
{
printf("%d, ",nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}

return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-22 C program to Calculate sum of digits using while loop.

#include<stdio.h>
int main()
{
int a, s;
printf("Enter value of a: ");
scanf("%d",&a);

s = 0;
while(a > 0)
{
s = s + (a%10);
a = a / 10;
}

printf("Sum of digits: %d",s);


return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-23 C Program to calculate the sum of first n natural numbers.

#include <stdio.h>
int main()
{
int num, count, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &num);

for(count = 1; count <= num; ++count)


{
sum += count;
}

printf("Sum = %d", sum);


return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-24 C Program to find Factorial of a Number.

#include <stdio.h>
int main()
{
int n, i;
unsigned long long factorial = 1;

printf("Enter an integer: ");


scanf("%d",&n);

if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else
{
for(i=1; i<=n; ++i)
{
factorial *= i;
}
printf("Factorial of %d = %llu", n, factorial);
}

return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-25 Fibonacci series program in C language.

#include<stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;

printf("Enter the number of terms: ");


scanf("%d",&n);

printf("First %d terms of Fibonacci series are :-\n",n);


for(c=0;c<n;c++)
{
if (c <= 1)
next = c;

else
{
next = first + second;
first = second;
second = next;
}

printf("%d ",next);
}

return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-26 C program to calculate factorial value using do while.

#include<stdio.h>
int main()
{
long int i,n,fact=1;

printf("Enter the value of n: ");


scanf("%ld", &n);

i=1;
do
{
fact*=i;
i++;
}
while(i<=n);

printf("Factorial = %ld\n",fact);
return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-27 C Program to sum of even numbers up to n using continue
statement.

#include <stdio.h>
int main()
{
int N, counter, sum=0;

printf("Enter a positive number: ");


scanf("%d", &N);

for(counter=1; counter <= N; counter++)


{
if (counter%2== 1)
{
continue;
}
sum+= counter;
}

printf("Sum of all even numbers between 1 to %d = %d", N, sum);


return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-28 A program to evaluate the equation y=xn

#include <stdio.h>
int main()
{
int count, n;
float x, y;

printf("Enter the values of x and n : ");


scanf(“%f %d”, &x, &n);

y = 1.0;
count = 1;

while ( count <= n)


{
y = y*x;
count++;
}

printf("\nx = %0.2f; n = %d; x to the power n = %0.3f\n",x,n,y);


return 0;
}

Yasin Arafat, ID: 22-0-52-020-014


Q-29 Write a C program to print a multiplication table from 1 to 10 using
nested loops.

#include <stdio.h>
#define COLMAX 10
#define ROWMAX 10
int main()
{
int row,column, y;
row = 1;
printf(" MULTIPLICATION TABLE \n");
printf("– – – – – – – – – – – – – – – – – – – – – – – \n");
do
{
column = 1;
do
{
y = row * column;
printf("%4d", y);
column = column + 1;
}
while (column <= COLMAX);
printf("\n");
row = row + 1;
}
while (row <= ROWMAX);
printf("— — — — — — — — — — — — — — — — — — — — — — —\n");
return 0;

Yasin Arafat, ID: 22-0-52-020-014


Q-30 Given a number, write a program using while loop to reverse the
digits of the number.

#include <stdio.h>
int main()
{
int num, rev = 0, digit;

printf("Enter a number: ");


scanf("%d", &num);

while (num != 0)
{
digit = num % 10;
rev = rev * 10 + digit;
num = num / 10;
}

printf("Reversed number = %d", rev);


return 0;
}

Yasin Arafat, ID: 22-0-52-020-014

You might also like