0% found this document useful (0 votes)
4 views

Lab Session - 3 [Loops]

The document contains a series of programming exercises in C, each demonstrating different concepts such as loops, conditionals, and basic arithmetic operations. Exercises include displaying natural numbers, multiplication tables, odd numbers, squares of even numbers, prime checking, finding the largest number, Fibonacci series, factorial calculation, digit counting, Armstrong numbers, and palindrome checking. Each exercise is accompanied by code snippets and explanations for user input and expected output.

Uploaded by

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

Lab Session - 3 [Loops]

The document contains a series of programming exercises in C, each demonstrating different concepts such as loops, conditionals, and basic arithmetic operations. Exercises include displaying natural numbers, multiplication tables, odd numbers, squares of even numbers, prime checking, finding the largest number, Fibonacci series, factorial calculation, digit counting, Armstrong numbers, and palindrome checking. Each exercise is accompanied by code snippets and explanations for user input and expected output.

Uploaded by

p20242202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab session - 3

--------------------------------------------------
----
1. WAP to display first n natural numbers and their sum using for loop.
Take n from the user.

#include <stdio.h>
#include <conio.h>
void main()
{
int i, n, sum = 0;
clrscr();
printf("Up to which natural no. do you need the sum? : ");
scanf("%d", &n);
printf("\nThe first %d natural numbers are:\n", n);
for (i = 1; i <= n; i++)
{
printf("%d\n", i);
sum += i;
}
printf("\nThe Sum of natural numbers upto %d terms : %d \n", n,
sum);
getch();
}

2. WAP to print multiplication table of a number given by user.

#include<stdio.h>
#include<conio.h>
void main ()
{
int n, i;
clrscr();
printf("Enter a number to get its multiplication table: ");
scanf("%d",&n);
printf("\nMultilication table of %d: \n",n);
for (i=1;i<=10;i++)
{
printf("%d x %d = %d\n", n, i, n*i);
//If there is only a single statement inside the for loop, you can
omit the {} braces.
}
getch();
}

3. WAP to print list of odd natural numbers up to a no. given by user,


using while loop.

#include<stdio.h>
#include<conio.h>
void main ()
{
int n, i=1;
system("cls");
printf("Enter a number up to which odd natural numbers are needed:
");
scanf("%d",&n);
printf("\nOdd natural numbers up to %d are:\n\n",n);
while (i<=n)
{
if (i%2!=0)
printf("%d\n",i);
i++;
}
getch();
}

4. WAP to print the square of only even nos. entered by user, and to
skip output for negative or odd nos.

#include <stdio.h>
#include <conio.h>
void main()
{
int i, n, a, sq;
clrscr();
printf("\nHow many numbers you want to enter: ");
scanf("%d", &n);
for (i=1;i<=n; i++)
{
printf("\nEnter number: ");
scanf("%d", &a);
if (a<0 || a%2!=0)
continue; //takes us back to beginning of for loop
sq = a * a;
printf("\nSquare = %d\n", sq);
}
getch();
}

5. WAP to check if the no. given by user is prime or not.


#include<stdio.h>
#include<conio.h>
void main ()
{
int n,i;
system("cls");
printf("Enter any no.: ");
scanf("%d", &n);
if (n==1)
printf("Not Prime");
for(i=2;i<=n-1;i++)
{
if(n%i==0)
{
printf("Not Prime");
break; //this breaks the for loop and program proceeds further
}
}
if(i==n)
printf("Prime");
getch();
}

6. WAP to take multiple numbers from user and to print the largest among
them using Do-While loop.

#include<stdio.h>
#include<conio.h>
void main ()
{
int num=-1, largest=0;
clrscr();
do
{
printf("Enter a number (enter 0 to stop): ");
scanf("%d",&num);
if(num>largest)
largest = num;
}
while (num!=0);//note that ; must be used with the while statement
here.
printf("Largest number is %d",largest);
getch();
}

7. WAP to print Fibonacci series up to no. of terms given by user.

/*Program to print Fibonacci Series 0,1,1,2,3,5,8,13,...*/


#include<stdio.h>
void main()
{
int n,n1=0,n2=1,n3,i=3;
clrscr();
printf("Enter no. of terms for fibonacci series: ");
scanf("%d",&n);
printf("\nFibonacci Series: ");
printf("\n%3d%3d",n1,n2); //here, %3d is same as %d, with wider
spacing.
do
{
n3 = n1+n2;
printf("%3d",n3);
n1 = n2;
n2 = n3;
i++;
} while(i<=n);
getch();
}

A better alternative:

#include<stdio.h>
#include<conio.h>
void main()
{
int x=0,y=1,z,n;
clrscr();
printf("Enter no. of terms for fibonacci series: ");
scanf("%d",&n);
n-=2; //same as writing n=n-2
if (n==-1)
printf("%d\t",x);
else if (n==0)
printf("%d\t%d\t",x,y);
else
{
printf("%d\t%d\t",x,y);
do
{
printf("%d\t",z=x+y);
x=y;
y=z;
--n;
}
while (n>0);
}
getch();
}

8. WAP to print the factorial of a number given by user.


#include<stdio.h>
#include<conio.h>
void main ()
{
int n, i;
long fact=1; //or long int
system("cls");
printf("Enter a number to get its factorial: ");
flag:
scanf("%d",&n);
if (n<0)
{
printf("\n\nInvalid input. Enter a positive integer: ");
goto flag;
}
for (i=2;i<=n;i++)
{
fact=fact*i;
}
printf("%d! = %ld",n,fact);
getch();
}

9. WAP to count the number of digits in an integer entered by user.


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
long int n, m, digits=1;
clrscr();
printf("Enter a positive integer: ");
scanf("%ld",&n);
m=n/10;
while (m!=0)
{
digits++;
m=m/10;
}
printf("%ld has %ld digits",n, digits);
getch();
}
10. WAP to find whether the entered no. is an Armstrong no. or not.

//Armstrong nos. are equal to the sum of their digits individually


raised to the power of no. of digits in the original no. e.g. 1, 153,
407, 1634 etc.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
long int n, m, p=0, digits=1;
clrscr();
printf("Enter a positive integer: ");
scanf("%ld",&n);
m=n/10;
while (m!=0) // A loop to count digits in entered no.
{
digits++;
m=m/10;
}
m=n; //reset value of m to n
do
{
p=p+(long)pow((m%10), digits); //typecasting to ensure long int is
intact after pow operation
m=m/10;
}
while (m!=0);
if (n==p)
printf("%ld is an Armstrong no.",n);
else
printf("%ld is NOT an Armstrong no.",n);
getch();
}

11. WAP to find whether the entered no. is a palindrome or not.

// A palindrome no. is one that is equal to itself when reversed.


e.g. 121, 346643 etc.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
long int n, m=0, temp;
clrscr();
printf("Enter a positive integer: ");
flag:
scanf("%ld",&n);
if (n<10)
{
printf("\nEnter a no. greater than 9: ");
goto flag;
}
temp=n;
while (temp!=0)
{
m=m*10 + temp%10;
temp=temp/10;
}
if (m==n)
printf("%ld IS a palindrome",n);
else
printf("%ld is NOT a palindrome",n);
getch();
}

----------------------------------X----------------------------------

You might also like