Professional Documents
Culture Documents
Lab Report 2
Lab Report 2
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.
1|Page
Here is shown the executable file of the above program.
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:
getchar();getchar();
return 0;
4|Page
Here is shown the screenshot of the above program and its executable file.
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.
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;
Sample Output
Enter a number: 457
Sum of digits is = 16
Code:
#include <stdio.h>
int main()
int num,digit1,digit2,digit3;
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.
12 | P a g e
Here is shown the executable file of the above program.
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.
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
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
#include <math.h>
int main()
int num,digit1,digit2,digit3,sum;
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
getchar();getchar();
return 0;
18 | P a g e
Here is shown the screenshot of the above program and its executable file.
19 | P a g e
Here is shown the executable file of the above program.
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
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
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;
scanf ("%d",&number);
o_number=number;
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)
else
getchar();
getchar();
return 0;
22 | P a g e
Here is shown the screenshot of the above program and its executable file.
23 | P a g e
Here is shown the executable file of the above program.
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
1.Code:
#include <stdio.h>
#include <math.h>
int main()
int side1,side2,side3;
scanf ("%d",&side1);
scanf ("%d",&side2);
scanf ("%d",&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.
26 | P a g e
Here is shown the executable file of the above program.
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
int main()
int A,B,count=0;
scanf("%d%d",&A,&B);
if (A%2==0)
A++;
for (A=A+2;A<B;A+=2)
printf("%d\t",A);
count++;
getchar();
getchar();
return 0;
28 | P a g e
Here is shown the screenshot of the above program and its executable file.
29 | P a g e
Here is shown the executable file of the above program.
30 | P a g e
Code (while loop):
#include <stdio.h>
int main()
int A,B,count=0;
scanf("%d%d",&A,&B);
A++;
if (A%2==0)
A++;
while (A<B)
printf("%d\t",A);
A+=2;
count++;
getchar();
getchar();
return 0;
31 | P a g e
Here is shown the screenshot of the above program and its executable file.
32 | P a g e
Here is shown the executable file of the above program.
33 | P a g e
Code (do while loop):
#include <stdio.h>
int main()
int A,B,count=0;
scanf("%d%d",&A,&B);
A++;
do
if (A>=B)
break;
if (A%2==0)
A++;
printf("%d\t",A);
A+=2;
count++;
while (A<B);
getchar();
getchar();
return 0;
34 | P a g e
Here is shown the screenshot of the above program and its executable file.
35 | P a g e
Here is shown the executable file of the above program.
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)
Single Condition
Multiple Condition
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;
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.
39 | P a g e
Here is shown the executable file of the above program.
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;
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;
getchar();
getchar();
return 0;
42 | P a g e
Here is shown the screenshot of the above program and its executable file.
43 | P a g e
Here is shown the executable file of the above program.
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.
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)
{
}
getchar();getchar();
return 0;
45 | P a g e
Here is shown the screenshot of the above program and its executable file.
46 | P a g e
Here is shown the executable file of the above program.
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
a.Code:
#include <stdio.h>
int main()
int a,n,rep=1;
scanf ("%d",&a);
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.
49 | P a g e
Here is shown the executable file of the above program.
50 | P a g e
b.Code:
#include <stdio.h>
int main()
int a,N,rep=1;
scanf ("%d",&a);
scanf ("%d",&N);
while (1)
if (a>N)
break;
a=a*2+1;
rep++;
getchar();getchar();
return 0;
51 | P a g e
Here is shown the screenshot of the above program and its executable file.
52 | P a g e
Here is shown the executable file of the above program.
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;
scanf ("%d",&number);
scanf ("%d",&power);
for (i=1;i<=power;i++)
result*=number;
getchar();getchar();
return 0;
54 | P a g e
}
Here is shown the screenshot of the above program and its executable file.
55 | P a g e
Here is shown the executable file of the above program.
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;
scanf ("%d",&number1);
scanf ("%d",&number2);
min=number1<number2?number1:number2;
for (hcf=min;hcf>0;hcf--)
if (number1%hcf==0&&number2%hcf==0)
break;
getchar();getchar();
return 0;
57 | P a g e
Here is shown the screenshot of the above program and its executable file.
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;
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
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;
scanf ("%d",&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)
sum=0;
getchar();getchar();
return 0;
66 | P a g e
Here is shown the screenshot of the above program and its executable file.
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.
#include <math.h>
int main()
int number,i;
scanf ("%d",&number);
for (i=1;i<11;i++)
69 | P a g e
getchar();getchar();
return 0;
Here is shown the screenshot of the above program and its executable file.
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;
scanf ("%d",&number);
while (i<11)
i++;
getchar();getchar();
return 0;
72 | P a g e
Here is shown the screenshot of the above program and its executable file.
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;
scanf ("%d",&number);
do
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.
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;
scanf ("%d",&number);
last_digit=number%10;
first_digit=number;
while (first_digit>9)
first_digit/=10;
getchar();getchar();
return 0;
78 | P a g e
}
Here is shown the screenshot of the above program and its executable file.
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;
scanf ("%d",&number);
last_digit=number%10;
first_digit=number;
while (first_digit>9)
first_digit/=10;
digits*=10;
getchar();getchar();
return 0;
81 | P a g e
Here is shown the screenshot of the above program and its executable file.
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;
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 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;
scanf ("%d",&number);
if (number<1)
product=0;
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.
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;
scanf ("%d",&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.
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;
scanf ("%d",&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.
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)
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.
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;
scanf ("%d",&num);
for (i=1;i<=num;i++)
sum+=i*i;
getchar();getchar();
return 0;
97 | P a g e
Here is shown the screenshot of the above program and its executable file.
98 | P a g e
Here is shown the executable file of the above program.
Figure 61.exe
99 | P a g e