Write A Program To Print Diamond Inscribed Inside Rectangle: Solution
Write A Program To Print Diamond Inscribed Inside Rectangle: Solution
Write A Program To Print Diamond Inscribed Inside Rectangle: Solution
INSCRIBED
INSIDE RECTANGLE
SOLUTION :
#include <stdio.h>
int main()
{
int i, j, n;
scanf("%d", &n);
// upper half of the pattern
for(i = 0; i < n; i++)
{
for(j = 0; j < (2 * n); j++)
{
if(i + j <= n - 1) // upper left
triangle
printf("*");
else
printf(" ");
if((i + n) <= j) // upper right triangle
printf("*");
else
printf(" ");
}
printf("\n");
}
// bottom half of the pattern
for(i = 0; i < n; i++)
{
for(j = 0; j < (2 * n); j++)
{
if(i >= j) //bottom left triangle
printf("*");
else
printf(" ");
if(i >= (2 * n - 1) - j) // bottom right
triangle
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
RESULT
Input: 5
Output:
**********
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
**********
WRITE A PROGRAM TO PRINT PASCAL’S
TRIANGLE
SOLUTION :
#include<stdio.h>
int main()
{
int rows, coef=1, space, i, j;
printf("Enter number of rows: ");
scanf("%d", &rows);
for (i=0; i<rows; i++) {
for (space=1; space <= rows-i; space++)
printf(" ");
for (j=0; j<=i; j++) {
if (j==0 || i==0)
coef = 1;
else
coef=coef*(i-j+1)/j;
printf("%4d", coef);
}
printf("\n");
}
return 0;
}
INPUT : 6
OUTPUT :
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
WRITE A PROGRAM TO PRINT DIAMOND
PATTERN
SOLUTION :
#include <stdio.h>
int main()
{
int n, c, k, space = 1;
space = n - 1;
space--;
printf("\n");
}
space = 1;
for (k = 1; k <= n - 1; k++)
{
for (c = 1; c <= space; c++)
printf(" ");
space++;
printf("\n");
} return 0;
}
INPUT : 5
OUTPUT :
*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*
WRITE A PROGRAM TO CHECK PRIME NUMBER
SOLUTION :
#include <stdio.h>
main() {
int n, i, c = 0;
printf("Enter any number n:");
scanf("%d", &n);
if (c == 2) {
printf("Prime number");
}
else {
printf("Not a Prime number");
}
return 0;
}
OUTPUT :
Enter any number n: 7
Prime number
WRITE A PROGRAM TO CHECK LARGEST OF
THREE
NUMBER
SOLUTION :
#include <stdio.h>
int main() {
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
return 0;
}
OUTPUT :
Enter three numbers: - 4.5
3.9
5.6
5.60 is the largest
number.
WRITE A PROGRAM TO FIND THE GRADE
>=80 A+
>=75 A
>=60 B
>=45 C
>=35 D
<35 Fail
SOLUTION :
#include<stdio.h>
#include<conio.h>
void main()
{
int s1,s2,s3,s4,s5,t,p;
clrscr();
p=t/5;
else if(p>=75)
printf("\n\n Your Grade : A");
else if(p>=60)
printf("\n\n Your Grade : B");
else if(p>=45)
printf("\n\n Your Grade : C");
else if(p>=35)
printf("\n\n Your grade : D");
else
printf("\n\n You Are Fail");
getch();
}
OUTPUT :
Enter marks of 5 subjects each out of 100
Maths = 95
English = 98
Computer = 100
Physics = 92
Chemistry = 91
Total marks = 476/500
Percentage = 95.2
Your Grade = A+
switch(operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",n1, n2,
n1+n2);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf",n1, n2,
n1-n2);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf",n1, n2,
n1*n2);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf",n1, n2,
n1/n2);
break;
return 0;
}
OUPUT : Enter an operator (+, -, *,): -
Enter two operands: 32.5
12.4
int main()
{
sum += number;
}
jump:
average=sum/(i-1);
printf("Sum = %.2f\n", sum);
printf("Average = %.2f", average);
return 0;
}
Sum = 16.60
#include < stdio.h >
int main()
{
int num, count = 1;
printf("Enter a number\n");
scanf("%d", &num);
printf("\nMultiplication table for %d is:\n\n", num);
while(count <= 10)
{
printf("%d x %d = %d\n", num, count, (num*count));
count++;
}
return 0;
}
OUTPUT :
Enter a number
9
9x1=9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
9 x 10 = 90
printf("Sum = %.2lf",sum);
return 0;
}
OUTPUT :
void main(){
int num,r,sum=0,temp;
for(temp=num;num!=0;num=num/10){
r=num % 10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong
number.\n",temp);
else
printf("%d is not an Armstrong
number.\n",temp);
OUTPUT :
Input a number: 153
153 is an Armstrong number
PROGRAM TO REVERSE THE ELEMENT OF THE
ARRAY
SOLUTION :
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int arr[MAX_SIZE];
int size, i;
printf("Enter size of the array: ");
scanf("%d", &size);
return 0;
}
OUTPUT :
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}
OUTPUT:
int main()
{
long fno, sno, sum;
printf("-----------------------------------------\n")
;
OUTPUT :
Pointer : Add two numbers using call by reference:
-----------------------------------------------------
Input the first number : 5
Input the second number : 6
The sum of 5 and 6 is 11
PROGRAM TO SWAP TWO NUMBER USING CALL
BY VALUE
#include <stdio.h>
void swapnum( int var1, int var2 )
{
int tempnum ;
/*Copying var1 value into temporary variable */
tempnum = var1 ;
}
int main( )
{
int num1 = 35, num2 = 45 ;
printf("Before swapping: %d, %d", num1, num2);
OUTPUT :
Before swapping: 35, 45
After swapping: 35, 45
PROGRAM TO COUNT VOWELS CONSONAT DIGIT
SPACE
SOLUTION :
#include <stdio.h>
int main() {
char line[150];
int vowels, consonant, digit, space;
SOLUTION :
Enter a line of string: adfslkj34 34lkj343 34lk
Vowels: 1
Consonants: 11
Digits: 9
White spaces: 2