Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 100

Lab Manual

Computer Fundamentals and Programming

Lab Task -01


Write a program that ask users to input marks of a subject and print the letter grade (See grading
criteria below). For details see the following sample execution of the program

Grading Criteria
Marks 51 – 60 D
Marks 61 – 70 C
Marks 71 – 80 B
Marks 81 – 100 A

Sample Output-1
Enter the marks: 80
Grade: A
CODE:
#include <stdio.h>
int main()
{
float marks;
printf ("Enter the marks: ");
scanf ("%f",&marks);
if (marks>=81&&marks<=100)
printf("Grade:A");

else if(marks>=71&&marks<=80)
printf("Grade:B");

else if (marks>=61&&marks<=70)
printf("Grade:C");

else if (marks>=51&&marks<=60)
printf("Grade:D");
else
printf("Invalid input");

getchar();getchar();
return 0;
}

Here is shown the screenshot of the above program and its executable file.

Figure 1Screenshot of above program

1|Page
Here is shown the executable file of the above program.

Figure 2.exe file

2|Page
Lab Task -02
Write a program that allow user to enter the day of the week as integer value e.g. for Monday the
user can enter 1, for Sunday user can enter 7.

The program should print the name of the day by using this integer value

Code:
#include <stdio.h>

int main()

int day;

printf ("Enter the day of the week as integer value e.g., 1 for Monday:
");

scanf ("%d",&day);

switch (day)

case (1):

printf ("Monday");

break;

case (2):

printf ("Tuesday");

break;

case (3):

printf ("Wednesday");

break;

case (4):

printf ("Thursday");

break;

3|Page
case (5):

printf ("Friday");

break;

case (6):

printf ("Satday");

break;

case (7):

printf ("Sunday");

break;

default:

printf ("Invalid Input");

getchar();getchar();

return 0;

4|Page
Here is shown the screenshot of the above program and its executable file.

Figure 3Screenshot of above program

5|Page
Here is shown the executable file of the above program.

6|Page
Home Task -1
Write a program that lets user enter an integer value between 1 and 10, the program
validates the input, if the value entered is between 1 and 10 the program prints the
message “Valid Number” and value entered otherwise the program should print message
“Invalid Number” and value. For better understanding of the problem following are two
sample outputs of the program

Sample Output-1
Enter a number between 1 and 10 -->56
Invalid Number 56

Sample Output-2
Enter a number between 1 and 10 -->6
Valid Number 6

1. CODE:
#include <stdio.h>
int main()
{
int number;
printf ("Enter a number between 1 and 10-->");
scanf("%d",&number);

if(number>0&&number<11)
printf ("Valid Number %d",number);

else
printf ("Invalid Number %d",number);
getchar();getchar();
return 0;
}

Here is shown the screenshot of the above program and its executable file.

7|Page
Figure 4Screenshot of above program

8|Page
Here is shown the executable file of the above program.

Figure 5.exe file

9|Page
Home Task -02
Write a program that gets a three digit integer input and prints the sum of its digits. For example
if user enters 123 the program should calculate 1+2+3 = 6 as answer. To get an idea about the
solution see the program below

#include<stdio.h>

#include<conio.h>

void main(){

int num=12;

int digit1,digit2;

digit1=num%10;

digit2=num/10;

printf(“First digit is = %d ”,digit1);

printf(“\nSecond digit is =%d’,digit2);

Sample Output
Enter a number: 457
Sum of digits is = 16
Code:
#include <stdio.h>

int main()

int num,digit1,digit2,digit3;

printf ("Please enter three digit integer: ");

scanf ("%d",&num);

digit1=num%10;

digit3=num/100;

digit2=(num-digit3*100)/10;

10 | P a g e
printf ("The sum of digits is: %d",digit1+digit2+digit3);

getchar();getchar();

return 0;

11 | P a g e
Here is shown the screenshot of the above program and its executable file.

Figure 6Screenshot of above program

12 | P a g e
Here is shown the executable file of the above program.

Figure 7.exe file

13 | P a g e
Home Task -03
Write a C program to read the age of a candidate and determine whether it is eligible for casting
his/her own vote. Sample Output is given below:

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

if(age<18)
printf ("You are not eligible for voting");

else
printf ("You are eligible for voting");
getchar();getchar();
return 0;
}
Here is shown the screenshot of the above program and its executable file.

14 | P a g e
Figure 8Screenshot of above program

15 | P a g e
Here is shown the executable file of the above program.

Figure 9.exe file

16 | P a g e
Home Task -04
Armstrong Numbers: An Armstrong number is an n-digit number that is equal to the sum of
the nth powers of its digits. For example, 153 is an Armstrong number as it is a three digit
number and if each digit is raised to the 3rd power and summed the result will be 153.

13+53+33=153

Write a program that gets a three digit number from the user and checks whether the
number is an

Armstrong number or not. Output of your program should be as under.

Hint: To check that a number is an Armstrong or not you first need to separate its digits as
done in last activity

Sample Output
Enter a number 342
Not an Armstrong number

Enter a number 371


The number is an Armstrong
Code:
#include <stdio.h>

#include <math.h>

int main()

int num,digit1,digit2,digit3,sum;

printf ("Please enter three digit integer: ");

scanf ("%d",&num);

digit1=num%10;

digit3=num/100;

digit2=(num-digit3*100)/10;

sum=pow(digit1,3)+pow(digit2,3)+pow(digit3,3);

if (sum==num)

17 | P a g e
printf ("The number is an Armstrong");

else

printf ("Not an Armstrong number");

getchar();getchar();

return 0;

18 | P a g e
Here is shown the screenshot of the above program and its executable file.

Figure 10Screenshot of above program

19 | P a g e
Here is shown the executable file of the above program.

Figure 11.exe file

20 | P a g e
Home Task -05
Armstrong Numbers: An Armstrong number is an n-digit number that is equal to the sum of
the nth powers of its digits. For example, 153 is an Armstrong number as it is a three digit
number and if each digit is raised to the 3rd power and summed the result will be 153.

13+53+33=153

Write a program that gets a three digit number from the user and checks whether the
number is an

Armstrong number or not. Output of your program should be as under.

Hint: To check that a number is an Armstrong or not you first need to separate its digits as
done in last activity

Sample Output
Enter a number 342
Not an Armstrong number

Enter a number 371


The number is an Armstrong

1.Code:
#include <stdio.h>

#include <math.h>

int main()

int number,o_number,last_digit,i,j,l_digit_with_power=1,sum=0;

printf ("Enter a 3 digit number ");

scanf ("%d",&number);

//digits=log10(number); declare this variable for any # of digits

o_number=number;

for (i=0;i<=2;i++) //replace 2 with digits

last_digit=number%10;

number/=10;

21 | P a g e
for (j=0;j<=2;j++) //replace 2 with digits

l_digit_with_power*=last_digit;

sum+=l_digit_with_power;

l_digit_with_power=1;

if (sum==o_number)

printf ("The number is an Armstrong");

else

printf ("Not an Armstrong number %d");

getchar();

getchar();

return 0;

22 | P a g e
Here is shown the screenshot of the above program and its executable file.

Figure 12Screenshot of above program

23 | P a g e
Here is shown the executable file of the above program.

Figure 13.exe file

24 | P a g e
Home Task -06
Write a program t to check whether the triangle is valid or not if sides are given. Implement this
code using ternary operator.Let’s say that a, b, c is the sides of the triangle. So it must satisfy the
below criteria:

1. a + b > c
2. a + c > b
3. b + c > a

Sample Output is given below:

1.Code:
#include <stdio.h>

#include <math.h>

int main()

int side1,side2,side3;

printf ("Enter 1 side of triangle: = ");

scanf ("%d",&side1);

printf ("\nEnter 1 side of triangle: = ");

scanf ("%d",&side2);

printf ("\nEnter 1 side of triangle: = ");

scanf ("%d",&side3);

if (side3+side2>side1 && side1+side3>side2 && side1+side2>side3)

printf("\nValid triangle.");

25 | P a g e
else

printf("\nInvalid triangle.");

getchar();

getchar();

return 0;

Here is shown the screenshot of the above program and its executable file.

Figure 14Screenshot of above program

26 | P a g e
Here is shown the executable file of the above program.

Figure 15.exe file

27 | P a g e
Home Task -07
Write a program that allow user to enter two numbers A and B , the program should print all the
odd numbers between these two numbers, to make the program simple it is supposed that A<B

Code (for loop):


#include <stdio.h>

int main()

int A,B,count=0;

printf("Enter two numbers A and B (A<B):");

scanf("%d%d",&A,&B);

printf("\nThe odd numbers between %d and %d are\n\n\t",A,B);

if (A%2==0)

A++;

for (A=A+2;A<B;A+=2)

printf("%d\t",A);

count++;

printf("\n\nNo. of times loop executed= %d",count);

getchar();

getchar();

return 0;

28 | P a g e
Here is shown the screenshot of the above program and its executable file.

Figure 16Screenshot of above program

29 | P a g e
Here is shown the executable file of the above program.

Figure 17.exe file

30 | P a g e
Code (while loop):
#include <stdio.h>

int main()

int A,B,count=0;

printf("Enter two numbers A and B (A<B):");

scanf("%d%d",&A,&B);

printf("\nThe odd numbers between %d and %d are\n\n\t",A,B);

A++;

if (A%2==0)

A++;

while (A<B)

printf("%d\t",A);

A+=2;

count++;

printf("\n\nNo. of times loop executed= %d",count);

getchar();

getchar();

return 0;

31 | P a g e
Here is shown the screenshot of the above program and its executable file.

Figure 18Screenshot of above program

32 | P a g e
Here is shown the executable file of the above program.

Figure 19.exe file

33 | P a g e
Code (do while loop):
#include <stdio.h>

int main()

int A,B,count=0;

printf("Enter two numbers A and B (A<B):");

scanf("%d%d",&A,&B);

printf("\nThe odd numbers between %d and %d are\n\n\t",A,B);

A++;

do

if (A>=B)

break;

if (A%2==0)

A++;

printf("%d\t",A);

A+=2;

count++;

while (A<B);

printf("\n\nNo. of times loop executed= %d",count);

getchar();

getchar();

return 0;

34 | P a g e
Here is shown the screenshot of the above program and its executable file.

Figure 20Screenshot of above program

35 | P a g e
Here is shown the executable file of the above program.

Figure 21.exe file

Sr. Element For While Loop Do While Loop


No Loop
.
1 Number of comparison 18 18 49
2 Number of variables 3 3 3
3 Number of statement 14 14 16
4 Difficulty in developing program, (scale 1‐10) 1 5 6
5 Clarity of program (scale 1 to 10) 7 7 6
6 Number of lines of program 19 21 24
7 Number of times body of loop executed 16 16 16
8 Loop control (scale 1 to 10) 3 6 7

36 | P a g e
Home Task -08
Just for exercise convert the following statements into conditional statements for A, B, C and D,
you do not need to write program for this, either do it verbally or write down on your notebook.
If you feel you understand the concepts and do not need this exercise you may proceed to next
part. To better understand the task see the examples.

Given that A, B, C, D are all integer variables having some initial value (which is not known)
Example (Single condition)

Statement Conditional Statement


A is a positive number A>0
B is not zero B!=0

Example (Multiple Conditions)

Statement Conditional Statement


A and B are positive numbers A>0 && B>0
B and C are non zero numbers B!=0 && C!=0

Single Condition

Statement Conditional Statement


A and B are equal A==B
A is an even number A%2==0
5 C is a factor of C (5*C)%C=0
B divides C and there is no remainder B%C==0
C is greater than D C>D
B is an odd number B%2!=0
Sum of A and B is less than C (A+B)<C
C is greater than 100 and less than 200 C>100&&C<200

Multiple Condition

Statement Conditional Statement


C and D are negative numbers C<0&&D<0
A is greater than B and C is greater than D A>B&&C>D
A is even or B is odd number A%2==0||B%2==1
A or B or C is a positive number A>0||B>0||C>0
A and B are greater than 40 or C and D are (A>40&&B>40)||(C<10&&D<10)
less than 10
A and B are odd numbers A%2==1&&B%2==1
A and C is negative and D is positive A<0&&C<0&&D>0
A or B is odd and C is even (A%2==1||B%2==1)&&C%2==0

37 | P a g e
Home Task -09
Write a program in C to display the number given by user in reverse order. Break the program if
the given number contains more than 5 digits otherwise display it in reverse order.

Code:
#include <stdio.h>

#include <math.h>

int main()

int num;

printf ("Please enter a number: ");

scanf ("%d",&num);

while (num>0)

if ((num/100000)>=1)

break;

printf ("%d",num%10);

num/=10;

getchar();getchar();

return 0;

38 | P a g e
Here is shown the screenshot of the above program and its executable file.

Figure 22Screenshot of above program

39 | P a g e
Here is shown the executable file of the above program.

Figure 23.exe file

40 | P a g e
Home Task -10
Write a C program to find sum of all prime numbers between 1 to n using for loop. C program to
generate sum of all primes between a given ranges. Logic to find sum of prime numbers in a
given range.
Sample Output:

Input
Input upper limit: 10

Output
Sum of prime numbers between 1-10: 17

Code:
#include <stdio.h>

#include <math.h>

int main()

int u_limit,number,sum=0,j,prime;

printf("Input upper limit: ");

scanf ("%d",&u_limit);

for (number=2;number<=u_limit;number++)

prime=1;

for (j=2;j<=number/2;j++)

if (number%j==0)

prime=0;

break;

if (prime==1)

41 | P a g e
{

sum+=number;

printf ("Sum of prime numbers between 1-%d: %d",u_limit,sum);

getchar();

getchar();

return 0;

42 | P a g e
Here is shown the screenshot of the above program and its executable file.

Figure 24Screenshot of above program

43 | P a g e
Here is shown the executable file of the above program.

Figure 25.exe file

44 | P a g e
Home Task -11
Develop a simple program using a for and while loop that divides a number N by a given
number M such that M<N, the program should keep dividing the number N till N reduces to
0, the result of each division should be displayed on the console, a sample of the execution
of such a program is given below.

Enter value for M: 3


Enter value for N: 65

M=3, N=65, N/M=21


M=3, N=21, N/M=7

Code:
#include <stdio.h>
#include <math.h>
int main()
{

int M,N,i=1;
printf("Enter value for M: ");
scanf ("%d",&M);
printf("Enter value for N: ");
scanf ("%d",&N);
while (N>0)
{

printf ("M=%d, N=%d, N/M=%d\n",M,N,N/M);


N/=M;

}
getchar();getchar();
return 0;

45 | P a g e
Here is shown the screenshot of the above program and its executable file.

Figure 26Screenshot of above program

46 | P a g e
Here is shown the executable file of the above program.

Figure 27.exe file

47 | P a g e
Home Task -12
Develop a simple C program that can print the first n terms of the following series, the
program should use a single while loop.
ai = ai‐1x 2+1
a) The program should get the value of value of n and a from the user, following is a
sample execution of such a program for a=2 and n=5, whereas a0=a,

Enter value of a: 2
Enter value of n: 5
2, 5, 11, 23, 47
b) Develop a second version of the above program that prints the series till a term
exceeds a given value N, for example if N=40 the last term printed will be 47, the
program should print the term number and the last term for example in case of
N=40, the program should print

Term number = 5, last term = 47

a.Code:
#include <stdio.h>

int main()

int a,n,rep=1;

printf ("Enter value of a: ");

scanf ("%d",&a);

printf ("Enter value of n: ");

scanf ("%d",&n);

printf("%d",a);

while (rep<n)

a=a*2+1;

printf(", %d",a);

48 | P a g e
rep++;

getchar();getchar();

return 0;

Here is shown the screenshot of the above program and its executable file.

Figure 28Screenshot of above program

49 | P a g e
Here is shown the executable file of the above program.

Figure 29 .exe file

50 | P a g e
b.Code:
#include <stdio.h>

int main()

int a,N,rep=1;

printf ("Enter value of a: ");

scanf ("%d",&a);

printf ("Enter value of last term: ");

scanf ("%d",&N);

while (1)

if (a>N)

break;

a=a*2+1;

rep++;

printf("Term number =%d, last term =%d",rep,a);

getchar();getchar();

return 0;

51 | P a g e
Here is shown the screenshot of the above program and its executable file.

Figure 30Screenshot of above program

52 | P a g e
Here is shown the executable file of the above program.

Figure 31 .exe file

53 | P a g e
Home Task -13
Write a program that gets two number from the user A and n, the program should print the
nth power of the number without using power function. For example if user enters 2 and 3
the program should calculate 23 i.e. 8

Code:
#include <stdio.h>

int main()

int number,power,i,result=1;

printf ("Enter number : ");

scanf ("%d",&number);

printf ("Input power: ");

scanf ("%d",&power);

for (i=1;i<=power;i++)

result*=number;

printf ("The result of %d ^ %d = %d",number,power,result);

getchar();getchar();

return 0;

54 | P a g e
}

Here is shown the screenshot of the above program and its executable file.

Figure 32Screenshot of above program

55 | P a g e
Here is shown the executable file of the above program.

Figure 33.exe file

56 | P a g e
Home Task -14
LCM is a smallest positive integer that exactly divides two or more numbers.
Write a C program to input two numbers from user and find LCM (Lowest Common
Multiple) using loop.

Sample Output
Input number1: 12
Input number2: 30
LCM = 60

Code:
#include <stdio.h>

int main()

int number1,number2,min,hcf;

printf ("Input number1: ");

scanf ("%d",&number1);

printf ("Input number2: ");

scanf ("%d",&number2);

min=number1<number2?number1:number2;

for (hcf=min;hcf>0;hcf--)

if (number1%hcf==0&&number2%hcf==0)

break;

printf ("\nLCM = %d",number1*number2/hcf);

getchar();getchar();

return 0;

57 | P a g e
Here is shown the screenshot of the above program and its executable file.

Figure 34Screenshot of above program

58 | P a g e
Here is shown the executable file of the above program.

Figure 35.exe

59 | P a g e
Home Task -15

Write a C program to input a number from user and print it into words using for loop.

Code:
#include <stdio.h>

#include <math.h>

int main()

int number,digits,i,case1;

printf ("Enter any number: ");

scanf ("%d",&number);

digits=log10(number);

for (i=0;i<=digits;)

case1=number/pow(10,digits);

switch(case1)

case 0:

printf("Zero ");

break;

case 1:

printf("One ");

break;

60 | P a g e
case 2:

printf("Two ");

break;

case 3:

printf("Three ");

break;

case 4:

printf("Four ");

break;

case 5:

printf("Five ");

break;

case 6:

printf("Six ");

break;

case 7:

printf("Seven ");

break;

case 8:

printf("Eight ");

break;

case 9:

printf("Nine ");

break;

number=number-case1*pow(10,digits);

digits--;

61 | P a g e
getchar();getchar();

return 0;

62 | P a g e
Here is shown the screenshot of the above program and its executable file.

63 | P a g e
Figure 36Screenshot of above program

Here is shown the executable file of the above program.

Figure 37.exe

64 | P a g e
Home Task -16

Strong number is a special number whose sum of factorial of digits is equal to the original
number. For example: 145 is strong number. Since, 1! + 4! + 5! = 145.
Write a C program to print Strong numbers between 1 to n. C program to print all strong
numbers between a given range. Logic to print strong numbers in a given range in C
program.
Sample Output
Input
Input upper limit: 1000
Output
Strong numbers between 1-1000:
1, 2, 145
Code:
#include <stdio.h>

#include <math.h>

int main()

int number,number1,factorial=1,i,sum=0,o_number,last_digit;

printf ("Input upper limit: ");

scanf ("%d",&number);

printf ("Strong number between 1-%d:\n",number);

o_number=number;

for (number1=1;number1<=o_number;number1++)

number=number1;

while(number>0)

last_digit=number%10;

for (i=1;i<=last_digit;i++)

factorial*=i;

sum+=factorial;

65 | P a g e
factorial=1;

number=number/10;

if (number1==sum)

printf ("%d, ",number1);

sum=0;

getchar();getchar();

return 0;

66 | P a g e
Here is shown the screenshot of the above program and its executable file.

Figure 38Screenshot of above program

67 | P a g e
Here is shown the executable file of the above program.

Figure 39.exe

68 | P a g e
Home Task -17
Write a C program to input a number from user and print multiplication table of the given
number using for, while and do-while loops.

Code (for loop):


#include <stdio.h>

#include <math.h>

int main()

int number,i;

printf("Enter number to print table of: ");

scanf ("%d",&number);

for (i=1;i<11;i++)

printf ("%d * %d = %d\n",number,i,number*i);

69 | P a g e
getchar();getchar();

return 0;

Here is shown the screenshot of the above program and its executable file.

Figure 40Screenshot of above program

70 | P a g e
Here is shown the executable file of the above program.

Figure 41.exe

71 | P a g e
Code (while loop):
#include <stdio.h>

#include <math.h>

int main()

int number,i=1;

printf("Enter number to print table of: ");

scanf ("%d",&number);

while (i<11)

printf ("%d * %d = %d\n",number,i,number*i);

i++;

getchar();getchar();

return 0;

72 | P a g e
Here is shown the screenshot of the above program and its executable file.

Figure 42Screenshot of above program

73 | P a g e
Here is shown the executable file of the above program.

Figure 43.exe

74 | P a g e
Code (do while):
#include <stdio.h>

#include <math.h>

int main()

int number,i=1;

printf("Enter number to print table of: ");

scanf ("%d",&number);

do

printf ("%d * %d = %d\n",number,i,number*i);

i++;

while (i<11);

getchar();getchar();

return 0;

75 | P a g e
Here is shown the screenshot of the above program and its executable file.

Figure 44Screenshot of above program

76 | P a g e
Here is shown the executable file of the above program.

Figure 45.exe

77 | P a g e
Home Task -18
Write a C program to input a number from user and find first and last digit of number using
loop.

Code:
#include <stdio.h>

int main()

int number,last_digit,first_digit;

printf ("Enter any number: ");

scanf ("%d",&number);

last_digit=number%10;

first_digit=number;

while (first_digit>9)

first_digit/=10;

printf("First digit = %d\n",first_digit);

printf("Last digit = %d",last_digit);

getchar();getchar();

return 0;

78 | P a g e
}

Here is shown the screenshot of the above program and its executable file.

Figure 46Screenshot of above program

79 | P a g e
Here is shown the executable file of the above program.

Figure 47.exe

80 | P a g e
Home Task -19
Write a C program to input a number from user and swap first and last digit of the given
number.

Code:
#include <stdio.h>

int main()

int number,last_digit,first_digit,digits=1;

printf ("Enter any number: ");

scanf ("%d",&number);

last_digit=number%10;

first_digit=number;

while (first_digit>9)

first_digit/=10;

digits*=10;

printf("Original number = %d\n",number);

printf("Number after swapping first and last digit: %d",number-


first_digit*digits-last_digit+last_digit*digits+first_digit);

getchar();getchar();

return 0;

81 | P a g e
Here is shown the screenshot of the above program and its executable file.

Figure 48Screenshot of above program

Here is shown the executable file of the above program.

Figure 49.exe

82 | P a g e
Home Task -20
Palindrome number is such number which when reversed is equal to the original number.
For example: 121, 12321, 1001 etc.
Write a C program to input number from user and check number is palindrome or not
using loop. How to check whether a number is palindrome or not using loop in C
programming.

Code:
#include <stdio.h>

int main()

int number,reversed_number,original_number,last_digit;

printf ("Enter any number: ");

scanf ("%d",&number);

original_number=number;

while (number>0)

last_digit=number%10;

reversed_number=reversed_number*10+last_digit;

number/=10;

original_number==reversed_number?printf ("%d is
palindrome",original_number):printf ("%d is not palindrome",original_number);

getchar();getchar();

83 | P a g e
return 0;

Here is shown the screenshot of the above program and its executable file.

Figure 50Screenshot of above program

Here is shown the executable file of the above program.

Figure 51.exe

84 | P a g e
Home Task -21
Write a C program to input a number from user and calculate product of its digits. How to
find product of digits of a number using loop in C programming.

Code:
#include <stdio.h>

int main()

int number,last_digit,product=1;

printf ("Enter any number to calculate product of digit: ");

scanf ("%d",&number);

if (number<1)

product=0;

printf ("Product of digits = ");

while (number>0)

last_digit=number%10;

product*=last_digit;

number/=10;

printf ("%d",product);

getchar();getchar();

return 0;

85 | P a g e
Here is shown the screenshot of the above program and its executable file.

Figure 52Screenshot of above program

86 | P a g e
Here is shown the executable file of the above program.

Figure 53.exe

87 | P a g e
Home Task -22
Factor of any number is a whole number which exactly divides the number into a whole
number without leaving any remainder. For example: 2 is a factor of 6 because 2 divides 6
exactly leaving no remainder.
Write a C program to input a number from user and find all factors of the given number
using for and while loop.

1.Code(for loop):
#include <stdio.h>

#include <math.h>

int main()

int number,i;

printf("Enter any number to find its factors: ");

scanf ("%d",&number);

printf("\n\nAll factors of %d are:\n",number);

for (i=1;i<=number/2;i++)

if(number%i==0)

printf ("%d,\t",i);

printf ("%d,",number);

getchar();getchar();

return 0;

88 | P a g e
Here is shown the screenshot of the above program and its executable file.

Figure 54Screenshot of above program

89 | P a g e
Here is shown the executable file of the above program.

Figure 55.exe

90 | P a g e
2.Code(while):
#include <stdio.h>

#include <math.h>

int main()

int number,i=2;

printf("Enter any number to find its factors: ");

scanf ("%d",&number);

printf("\n\nAll factors of %d are:\n",number);

while (i<=number/2)

if(number%i==0)

printf ("%d,\t",i);

i++;

printf ("%d,",number);

getchar();getchar();

return 0;

91 | P a g e
Here is shown the screenshot of the above program and its executable file.

Figure 56Screenshot of above program

92 | P a g e
Here is shown the executable file of the above program.

Figure 57.exe

93 | P a g e
Home Task -23
Write a program that Print the square roots of the first 25 odd positive integers using while
loop.

Code:
#include <stdio.h>

#include <math.h>

int main()

int number=1,count=1;

while (count<=25)

printf ("Square root of %d = %f\n",number,sqrt(number));

number+=2;

count++;

getchar();getchar();

return 0;

94 | P a g e
Here is shown the screenshot of the above program and its executable file.

Figure 58Screenshot of above program

95 | P a g e
Here is shown the executable file of the above program.

Figure 59.exe

96 | P a g e
Home Task -24
Write a program in C to calculate the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5)
+ ... + (n*n)
Sample Output:
Input the value for nth term: 5
1*1 = 1
2*2 = 4
3*3 = 9
4*4 = 16
5*5 = 25
The sum of the above series is: 55

Code:
#include <stdio.h>

int main()

int num,sum,i;

printf ("Input the value for nth term: ");

scanf ("%d",&num);

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

sum+=i*i;

printf ("The sum of the series is: %d",sum);

getchar();getchar();

return 0;

97 | P a g e
Here is shown the screenshot of the above program and its executable file.

Figure 60Screenshot of above program

98 | P a g e
Here is shown the executable file of the above program.

Figure 61.exe

99 | P a g e

You might also like