0% found this document useful (0 votes)
22 views10 pages

Write A Program To Generate Multiplication Table Using For Loop

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)
22 views10 pages

Write A Program To Generate Multiplication Table Using For Loop

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
You are on page 1/ 10

Problem Solving Using c course code: UGCA-1905

16. Write a program to generate multiplication table using for loop.

#include<stdio.h>
int main()
{
int num;
print(“enter a table to print its multiplication table till 10: “);
scanf(“%d”,&num);
for(int i=1;i<10;i++)
{printf(“\n%d*%d=%d”,num,i,num*i); }
return 0;
}

Output:

Enter a table to print its multiplication


table till 10: 5

5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50

16
Problem Solving Using c course code: UGCA-1905

17. Write a program to generate multiplication table using while loop.

#include<stdio.h>
int main()
{
int num, i=1;
printf(“enter a number to generate its multiplication table: “);
scanf(“%d”,&num);
printf(“multiplication table of %d:\n”,num);
while (i <=10)
{
printf(“%d*%d=%d\n”,num,i,num*i);
i++;
}
return 0;
}

Output:

Enter a number to generate its


multiplication table: 5

5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50

17
Problem Solving Using c course code: UGCA-1905

18. Write a Program to make a simple calculator using switch…case.

#include<stdio.h>
int main()
{
char operator;
double num1, num2, result;
printf(“enter an operator (+,-,*,/):”);
scanf(“%c”, &operator);
printf(“enter two operands: “);
scanf(“%lf %lf”, &num1, &num2);
switch(operator)
{
case ‘+’:
result = num1 + num2;
printf(“%.2lf + %.2lf = %.2lf\n”, num1,num2, result);
break;
case’-‘:
result = num1 – num2;
printf(“%.2lf-%.2lf=%.2lf\n”, num1, num2, result);
break;
case ‘*’:
result = num1*num2;
printf(“%.2lf * %.2lf= %.2lf\n”, num1, num2, result);
break;
case ‘/’ :
if (num2 !=0)
{
result = num1/ num2;
printf(“%.2lf / %.2lf= %.2lf\n”, num1, num2, result);
} else {
printf(“error! division by zero . \n”);
}
break;
default:
printf(“invalid operator !\n”);
}
return 0;
}
Output:

Enter an operator (+,-,*,/): +


enter two operands : 4.5 2.5
4.50 + 2.50 = 7.00 18
Problem Solving Using c course code: UGCA-1905

19. Write a program to find whether the given number is a prime number.

#include<stdio.h>
int main()
{
int n, i, flag = 0;
printf(“enter a positive integer: “);
scanf(“%d”, &n);
if (n <=1)
{
printf(“%d is not a prime number.\n”, n);
return 0;
}
for (i = 2; i <=n/2; i++) {
if (n % i== 0) {
flag =1;
break;
}
}
if (flag == 0)
printf(“% is a prime number.\n”, n);
else
printf(“%d is not a prime number .\n”, n);
return 0;
}

Output:

Enter a number: 7

7 is a prime number.

Enter a number: 10

10 is not a prime number.

19
Problem Solving Using c course code: UGCA-1905

20. Write a program using function to find the largest of three numbers.

#include<stdio.h>
int findlargest(int a, int b, int c)
{
if (a >=b && a>=c)
return a;
else if (b >= a && b>=c)
return b;
else
return c;
}
int main()
{
int num1, num2, num3, largest;
printf(“enter three numbers: “);
scanf(“%d %d %d”, &num1, &num2, &num3);
largest = find largest(num1, num2, num3);
printf(“the largest number is: %d\n”,largest);
return 0;
}

Output:

Enter three numbers:

First number: 10

Second number: 20

Third number: 15

The largest number among is: 20

20
Problem Solving Using c course code: UGCA-1905

21. Write a program using function to print first 20 numbers and its squares.

#include<stdio.h>
void print squares(int n)
{
for (int i=1; i<=n; i++) {
printf(“%d\t %d\n”, i, i*i);
}
}
int main()
int limit = 20;
printf(“number\T-square\n”);
print squares(limit);
return 0;
}

Output:

Number square
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
11 121
12 144
13 169
14 196
15 225
16 256
17 289
18 324
19 361
20 400

21
Problem Solving Using c course code: UGCA-1905

22. Write a program to find the factorial of a given number.

#include <stdio.h>

unsigned long long factorial(int n) {

if (n == 0 || n == 1) {

return 1;

} else {

return n * factorial(n - 1);

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num < 0) {

printf("Factorial is not defined for negative numbers.\n");

} else {

printf("Factorial of %d = %llu\n", num, factorial(num));

return 0;

}
Output:

Enter a number to find its factorial: 0

The factorial of 0 is: 1

23. Write a program to find the Fibonacci series till n number of elements.

22
Problem Solving Using c course code: UGCA-1905

#include <stdio.h>
int main ()
{
int n, i;
int t1 = 0, t2 =1;
int next term;
printf(“enter the number of terms: “);
scanf(“%d”, &n);
printf(“fibonacci series :”);

for (i = 1; i <= n; ++i) {


printf(“%d” , t1);
next term = t1 +t2;
t1 = t2;
t2 = nextterm;
}
return 0;
}

Output:

Enter the number of elements in the Fibonacci


series: 10

Fibonacci series up to 10 elements: [0, 1, 1, 2, 3, 5,


8, 13, 21, 34

24. Write a program using various unformatted input functions.

23
Problem Solving Using c course code: UGCA-1905

#include<stdio.h>
int main()
{
char name[50];
char single char;
int age;
float salary;
printf(“enter a single character: “);
single char = get char();
while (get char() != ‘\n’);
printf(“enter your name: “);
gets(name);

printf(“enter your age:”);


scanf(“%d”, &age);
printf(“enter your salary: “);
scanf(“%f”, &salary);

printf(“\n---output ---\n”);
printf(“single character: %c\n”, single char);
printf(“name: %\n”, name);
printf(“age: %d\n”, age);
printf(“salary: %.2f\n”, salary);

return 0;
}

Output:

Enter a single character: A


enter your name: KARTIK
enter your age: 18
enter your salary: 10000.00

24
Problem Solving Using c course code: UGCA-1905

25. Write a program to find area of rectangle and print the result using unformatted output functions.

#include<stdio.h>
int main()
{
int length, width, area;
printf(“enter the length of the rectangle: “);
scanf(“%d”, &length);
printf(“enter the width of the rectangle: “);
scanf(“%d”, &width);
area = length * width;
printf(“the area of the rectangle is: “)
int temp= area;
char result[10];
int i = 0;

while (temp > 0) {


result[i++] = (temp %10) + ‘0’;
temp /=10;
}
for (int j=i – 1; j >= 0; j--) {
putcher(result[j]);
}
putcher(‘\n’);
return 0;
}

Output:

Enter the length of the rectangle: 7.5

Enter the width of the rectangle: 2.0

The area of the rectangle is:

15.0

25

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy