Write A Program To Print Diamond Inscribed Inside Rectangle: Solution

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

WRITE A PROGRAM TO PRINT DIAMOND

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;

  printf("Enter number of rows\n");


  scanf("%d", &n);

  space = n - 1;

  for (k = 1; k <= n; k++)


  {
    for (c = 1; c <= space; c++)
      printf(" ");

    space--;

    for (c = 1; c <= 2*k-1; c++)


      printf("*");

    printf("\n");
  }

  space = 1;
for (k = 1; k <= n - 1; k++)
  {
    for (c = 1; c <= space; c++)
      printf(" ");

    space++;

    for (c = 1 ; c <= 2*(n-k)-1; c++)


      printf("*");

    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);

/*logic*/ for (i = 1; i <= n; i++) {


if (n % i == 0) {
c++;
}
}

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);

if (n1 >= n2) {


if (n1 >= n3)
printf("%.2lf is the largest
number.", n1);
else
printf("%.2lf is the largest
number.", n3);
} else {
if (n2 >= n3)
printf("%.2lf is the largest
number.", n2);
else
printf("%.2lf is the largest
number.", 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();

printf("\n Enter marks of 5 subjects each out of 100


");

printf("\n\n Maths = ");


scanf("%d",&s1);

printf("\n Science = ");


scanf("%d",&s2);

printf("\n English = ");


scanf("%d",&s3);

printf("\n History = ");


scanf("%d",&s4);

printf("\n Geography = ");


scanf("%d",&s5);
t=s1+s2+s3+s4+s5; //Total
printf("\n Total marks = %d/500",t);

p=t/5;

printf("\n\n Percentage = %d%",p);

//////////// Ladder If Statement ////////////


if(p>=80)
printf("\n\n Your Grade : A+");

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");

//////// Ladder If Statement /////////////

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+

WRITE A PROGRAM TO IMPLEMENT A


CALCULATOR
SOLUTION :
#include <stdio.h>
int main() {
char operator;
double n1, n2;

printf("Enter an operator (+, -, *, /): ");


scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf",&n1, &n2);

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;

// operator doesn't match any case constant


+, -, *, /
default:
printf("Error! operator is not correct");
}

return 0;
}
OUPUT : Enter an operator (+, -, *,): -
Enter two operands: 32.5
12.4

32.5 - 12.4 = 20.1

PROGRAM TO CALCULATE THE SUM AND


AVERAGE OF
POSITIVE NUMBER
SOLUTION :
# include <stdio.h>

int main()
{

const int maxInput = 5;


int i;
double number, average, sum=0.0;

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


{
printf("%d. Enter a number: ", i);
scanf("%lf",&number);

if(number < 0.0)


goto jump;

sum += number;
}

jump:
average=sum/(i-1);
printf("Sum = %.2f\n", sum);
printf("Average = %.2f", average);

return 0;
}

OUTPUT : 1. Enter a number: 3


2. Enter a number: 4.3
3. Enter a number: 9.3
4. Enter a number: -2.9

Sum = 16.60

PROGRAM TO PRINT THE TABLE OF ENTERED


NUMBER
SOLUTION :
   

#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

Multiplication table for 9 is:

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

PROGRAM TO ADD NUMBER UNITLL USER


ENTER ZERO
SOLUTION :
#include <stdio.h>
int main()
{
double number, sum = 0;

// the body of the loop is executed at least once


do
{
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.0);

printf("Sum = %.2lf",sum);

return 0;
}

OUTPUT :

Enter a number: 1.5


Enter a number: 2.4
Enter a number: -3.4
Enter a number: 4.2
Enter a number: 0
Sum = 4.70
PROGRAM TO CHECK ARMSTRONG NUMBER OR
NOT
SOLUTION :
#include <stdio.h>

void main(){
int num,r,sum=0,temp;

printf("Input a number: ");


scanf("%d",&num);

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);

printf("Enter elements in array: ");


for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

printf("\nArray in reverse order: ");


for(i = size-1; i>=0; i--)
{
printf("%d\t", arr[i]);
}

return 0;
}

OUTPUT :

Enter size of the array : 5  


Enter elements in array:
1 2 3 4 5
Array in reverse order:
5 4 3 2 1
PROGRAM TO MULTIPLY TWO 2D ARRAY
SOLUTION :
#include <stdio.h>
 
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
 
printf("Enter the number of rows and columns of first
matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
 
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);
 
printf("Enter the number of rows and columns of second
matrix\n");
scanf("%d%d", &p, &q);
 
if ( n != p )
printf("Matrices with entered orders can't be multiplied
with each other.\n");
else
{
printf("Enter the elements of second matrix\n");
 
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);
 
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}
 
multiply[c][d] = sum;
sum = 0;
}
}
 
printf("Product of entered matrices:-\n");
 
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )

printf("%d\t", multiply[c][d]);
 
printf("\n");
}
}
 
return 0;
}

OUTPUT:

Enter the number of rows and columns of first matrix


3 3

Enter the elements of first matrix


1 2 0
0 1 1
2 0 1
Enter the number of rows and columns of second matrix
3 3

Enter the elements of second matrix


1 1 2
2 1 1
1 2 1

Product of entered matrices:-


53 4
33 2
34 5
PROGRAM TO PRINT PRIME FACTORS
SOLUTION :
#include<stdio.h>
void PFactors( int num);
void IPFactors( int n);
 
int main( )
{
int num;
printf("Enter a number : ");
scanf("%d", &num);
printf("\nUsing Recursion :: \n");
PFactors(num);
printf("\n");
printf("\nUsing Iteration :: \n");
IPFactors(num);
printf("\n");
 
return 0;
 

void PFactors( int num)
{
int i = 2;
if( num == 1 )
return;
while( num%i != 0 )
i++;
printf("%d ", i);
PFactors(num/i);
}
 
void IPFactors( int num)
{
int i;
for( i = 2; num!=1; i++)
while( num%i == 0 )
{
printf("%d ", i);
num = num/i;
}
}
OUTPUT :

Enter a number : 100


 
Using Recursion ::
2255
 
Using Iteration ::
2255
PROGRAM TO ADD TWO NUMBER USING CALL
BY
REFERENCE
SOLUTION :
#include <stdio.h>
long addTwoNumbers(long *, long *);

int main()
{
long fno, sno, sum;

printf("\n\n Pointer : Add two numbers using call


by reference:\n");

printf("-----------------------------------------\n")
;

printf(" Input the first number : ");


scanf("%ld", &fno);
printf(" Input the second number : ");
scanf("%ld", &sno);
sum = addTwoNumbers(&fno, &sno);
printf(" The sum of %ld and %ld is %ld\n\n", fno,
sno, sum);
return 0;
}
long addTwoNumbers(long *n1, long *n2)
{
long sum;
sum = *n1 + *n2;
return sum;
}

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 ;

/* Copying var2 value into var1*/


var1 = var2 ;

/*Copying temporary variable value into var2 */


var2 = tempnum ;

}
int main( )
{
int num1 = 35, num2 = 45 ;
printf("Before swapping: %d, %d", num1, num2);

/*calling swap function*/


swapnum(num1, num2);
printf("\nAfter 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;

vowels = consonant = digit = space = 0;

printf("Enter a line of string: ");


fgets(line, sizeof(line), stdin);

for (int i = 0; line[i] != '\0'; ++i) {


if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i'
||
line[i] == 'o' || line[i] == 'u' || line[i] == 'A'
||
line[i] == 'E' || line[i] == 'I' || line[i] == 'O'
||
line[i] == 'U') {
++vowels;
}
else if ((line[i] >= 'a' && line[i] <= 'z') || (line[i]
>= 'A' &&
line[i] <= 'Z')) {
++consonant;
} else if (line[i] >= '0' && line[i] <= '9') {
++digit;
} else if (line[i] == ' ') {
++space;
}
}

printf("Vowels: %d", vowels);


printf("\nConsonants: %d", consonant);
printf("\nDigits: %d", digit);
printf("\nWhite spaces: %d", space);
return 0;
}

SOLUTION :
Enter a line of string: adfslkj34 34lkj343 34lk
Vowels: 1
Consonants: 11
Digits: 9
White spaces: 2

You might also like