Write A Program To Generate Multiplication Table Using For Loop
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:
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
#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:
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
#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:
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
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:
First number: 10
Second number: 20
Third number: 15
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
#include <stdio.h>
if (n == 0 || n == 1) {
return 1;
} else {
int main() {
int num;
scanf("%d", &num);
if (num < 0) {
} else {
return 0;
}
Output:
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 :”);
Output:
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(“\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:
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;
Output:
15.0
25