0% found this document useful (0 votes)
5 views13 pages

XII C Programs Coding

The document contains various C programming examples demonstrating input/output functions, control structures (if, switch, nested if), loops (for, while), and pattern printing using nested loops. It includes code snippets for tasks such as calculating the area of a triangle, generating multiplication tables, swapping numbers, and printing patterns. Each code example is accompanied by a brief description of its functionality.

Uploaded by

seenamkamrankhan
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)
5 views13 pages

XII C Programs Coding

The document contains various C programming examples demonstrating input/output functions, control structures (if, switch, nested if), loops (for, while), and pattern printing using nested loops. It includes code snippets for tasks such as calculating the area of a triangle, generating multiplication tables, swapping numbers, and printing patterns. Each code example is accompanied by a brief description of its functionality.

Uploaded by

seenamkamrankhan
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

Scanf() , printf()

#include <stdio.h>

int main()

int testInteger;

printf("Enter an integer: ");

scanf("%d", &testInteger);

printf("Number = %d",testInteger);

return 0;

Getchar(), getche(), getch()

#include <stdio.h>

#include <conio.h>

int main()

char a;

printf(("Enter a value: " );

a = getch();

printf(("\n your value is: %c ", a);

Puts() , gets()

#include <stdio.h>

#include <conio.h>

int main()

Char country [50];


printf("Enter the name of your favourite country : " );

gets(country);

printf("\n Your favourite country name is: ");

puts(country);

If structure

#include<conio.h>

#include<stdio.h>

int main ()

float cost,tax,luxury,total;

luxury=0.0;

printf("Enter the cost of item");

scanf("%f", &cost);

tax = cost*0.06;

if(cost>40000.00)

luxury=cost*0.005;

total=cost+tax+luxury;

printf("The total cost is %.2f",total);

getch();

}
Nested if

#include<conio.h>

#include<stdio.h>

int main ()

int exp,rank,salary;

printf("Enter experience\n");

scanf("%d",&exp);

printf("Enter rank\n");

scanf("%d", &rank);

printf("Enter salary\n");

scanf("%d", &salary);

if(exp>=5)

if(rank>=17)

if(salary>=50000)

printf("Bonus tri star 5KG given to employee");

getch();

}
If-else structure

#include<conio.h>

#include<stdio.h>

int main ()

float cost,tax,luxury,total;

luxury=0.0;

printf("Enter the cost of item");

scanf("%f", &cost);

tax = cost*0.06;

if(cost>40000.00)

luxury=cost*0.005;

printf("The luxury tax is %.2f \n",luxury);

else

printf("There is no luxury tax for this item");

getch();

Switch

#include<conio.h>

#include<stdio.h>

int main ()
{
char op;
float num1,num2;
printf("Enter an operator(+,-,*,/):");
scanf("%f,&op");
printf("Enter an number:"<<"\n";
scanf("%f,&num1");
scanf("%f,&num2");

switch(op)
{
case'+':
printf(num1"+"num2"="num1+num2);
break;

case'-':
printf (num1"-"num2"="num1-num2);
break;

case'*':
printf(num1"*"num2"="num1*num2);
break;

case'/':
printf(num1"/"num2"="num1/num2);
break;
default:
printf("Error! The operator is not correct");

}
return 0;
}

#include<conio.h>

#include<stdio.h>

int main()

double f;

int num;

while(num!=0)

printf("\n Enter the number to find factorial");

scanf("%d", & num);

f = 1;

while (num>1)

f = f*num;

num--;
}

printf("\n the factorial is %lf",f);

Q. Write a program to print the following pattern by using nested loop.

*
**
***
****
*****

#include <stdio.h>

int main()
{

// first loop to print all rows


for (int i = 0; i <5; i++) {

// first inner loop to print the * in each row


for (int j = 0; j<=i; j++) {
printf("* ");
}
printf("\n");
}
}

*****
****
***
**
*
#include <stdio.h>

int main()
{
for(int i=1 ; i<=5 ; i++)
{
for(int j=5 ; j>=i ; j--)
{
printf("*");
}
printf("\n");
}
}

*
**
* * *
* * * *
* * ** *

#include <stdio.h>

int main()
{
for(int i=1 ; i<=5 ; i++)
{
for(int j=i ; j<5 ; j++)
{
printf(" ");
}
for(int k=1; k<=i; k++)
{
printf("*");
}
printf("\n");
}
}

* * ** *
* * * *
* * *
* *
*

#include <stdio.h>

int main()
{
for(int i=1 ; i<=5 ; i++)
{
for(int j=1 ; j<i ; j++)
{
printf(" ");
}
for(int k=i; k<=5; k++)
{
printf("*");
}
printf("\n");
}
}

Q: Area of Triangle by using formula (A=w*I)


1. #include <stdio.h>
2. int main()
3. {
4. int w,I,A;
5. Printf(“Enter the value of width”);
6. Scanf(“%d”,& w);
7. Printf(“Enter the value of I”);
8. Scanf(“%d”,& I);
9. A=w*I;
10. printf("Area of the rectangle=%d",A);
11. Return 0;
12. }

Q: write c program to show the table of an inputted number

#include <stdio.h>

int main()
{
int num, i;

// Ask the user to enter a number


printf("Enter a number to display its multiplication table: ");
scanf("%d", &num);
// Display the multiplication table up to 10
printf("Multiplication Table of %d:\n", num);
for (i = 1; i <= 10; i++)
{
printf("%d * %d = %d\n", num, i, num * i);
}

return 0;
}

Q: Write a program that swap two inputted numbers using C language

#include <stdio.h>

int main() {
int num1, num2, temp;

// Input the first number


printf("Enter the first number: ");
scanf("%d", &num1);

// Input the second number


printf("Enter the second number: ");
scanf("%d", &num2);

// Print the numbers before swapping


printf("\nNumbers before swapping:\n");
printf("First number: %d\n", num1);
printf("Second number: %d\n\n", num2);

// Swapping logic
temp = num1;
num1 = num2;
num2 = temp;

// Print the numbers after swapping


printf("Numbers after swapping:\n");
printf("First number: %d\n", num1);
printf("Second number: %d\n", num2);

return 0;
}
Q: Write the equivalent code of the following statement in while loop format using C language.

for(a=1; a<100; a++)


printf("%d\n", a*a);

Answer:
while (a < 100) {
printf("%d\n", a * a);
a++;
}
_________________________________________________________________________________

Q: What is the output of the given program.

#include <stdio.h>
int main() {
for (int x = 1; x <= 5; x++)
{
for (int y = 1; y <= x; y++)
{
printf("%d", y);
}
printf("\n");
}

return 0;
}

Answer:

1
22
233
2334
23345

Q: if x=1, what is the output of the following code?

printf("x=%d\n",x);
printf("x=%d\n",++x);
printf("x=%d\n",x);

Output:

X=1
X=2
X=2
Q: Write the following program in while() equivalent.

{
Int a=3;
Do(print(“%d\n”,a);
A=a+3
}

Answer:
while (a =3)
{
printf("%d\n", a);
A=a+3;
}

Q: write a program in C language by using nested loop to print the following output
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25

Answer:

#include <stdio.h>
int main() {
// Outer loop for rows
for (int i = 2; i <= 5; i++) {
// Inner loop for columns
for (int j = 1; j <= 5; j++) {
// Calculate and print the value based on row and column
printf("%d\t", i * j);
}
// Move to the next line after each row
printf("\n");
}
return 0;
}

Q. Write a program that prints the following using nested loops by using C language:
Number Square
11
24
39
4 16
5 25

#include <stdio.h>

int main() {
int i, j;

printf("Number Square\n");

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


{
for (j = 1; j <= 1; j++)
{
printf("%d %d\n", i, i * i);
}
}

return 0;
}

*
***
*****
*******
*********
*******
*****
***
*

#include <stdio.h>

int main() {
int i, j, space;
int n = 5; // diamond ki height ka half

// Upper part
for (i = 1; i <= n; i++) {
for (space = 1; space <= n - i; space++) {
printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++) {
printf("*");
}
printf("\n");
}

// Lower part
for (i = n - 1; i >= 1; i--) {
for (space = 1; space <= n - i; space++) {
printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++) {
printf("*");
}
printf("\n");
}

return 0;
}

You might also like