[go: up one dir, main page]

0% found this document useful (0 votes)
4 views6 pages

C Language Example

C language
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

C Language Example

C language
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

C Language Puzzles

Basic Example
Q. 1. Program to Display "Hello, World!"
#include<stdio.h>
#include<conio.h>
void main()
{
// printf() displays the string inside quotation
printf("Hello, World!\n");
getch();
}
Q. 2. Write a Program Addition of two digits.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,add;
a=4;
b=5;
add=a+b;
printf("Addition of two Number is : %d",add);
getch();
}
Q. 3. Write a Program Addition of two digits.
#include<stdio.h>
#include<conio.h>
void main()
{
int hindi=34,english=55,math=65,sciene=28,computer=65,total;
int rollno=10265;
char name[30]=Ramesh;
printf(“Student Roll Number is : %d”,rollno);
printf(“Student Name is : %s”,name);
total=hindi+english+math+science+computer;
printf("Total Marks is : %d",total);
getch();
}

Q. 4. C program to get input from a user using scanf.


#include<stdio.h>
#include<conio.h>
void main()
{
int x;
printf("Input an integer\n");
scanf("%d", &x); // %d is used for an integer
printf("The integer is: %d\n", x);
getch();
}
Q. 5. WAP Addition of two numbers input by user in C
#include<stdio.h>
#include<conio.h>
int main()
{
int a, b, c;
printf("Enter two numbers to add\n");
scanf("%d%d",&a,&b);
c = a + b;
printf("Sum of the numbers = %d\n", c);
return 0;
}
Operator Example
Example of Assignment operator
Q. 1. Write a program to explain the assignment operator.
Solution: - #include<stdio.h>
#include<conio.h>
void main()
{
int a=10, b;
b=a;
printf(“Value is =%d”, b);
getch();
}
Example of Relational operator
Q. 2. Write a program to enter the two different values and check the entire relational operator.
Solution: - #include<stdio.h>
#include<conio.h>
void main()
{
int a, b;
printf(“Enter the two different value=”);
scanf(“%d%d”, &a, &b);
if (a<b)
printf(“%d is less than %d”, a, b);
if (a>b)
printf(“\n%d is greater than %d”, a, b);
if (a==b)
printf(“\n%d is equal to %d”, a, b);
if (a!=b)
printf(“\n%d is not equal to %d”, a, b);
if (a<=b)
printf(“\n%d is less than equal to %d”, a, b);
if (a>=b)
printf(“\n%d is greater than equal to %d”, a, b);
getch ();
}
Example of Conditional operator
Q. 3. Write a program to print the larger of two numbers using conditional operator.
Solution: - #include<stdio.h>
#include<conio.h>
void main()
{
int a,b,max;
printf(“Enter the two different value = ”);
Scanf(“%d%d”,&a,&b);
max=a<b?a:b;
printf(“The larger number is = %d”,max);
getch();
}
Example of if statement
Write a program to print a message if negative number is entered?
Solution: - #include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf(“enter the number=”);
scanf(“%d”&a);
if(a<0)
{
printf(“the number is negative”);
}
getch();
}
Example of if else statement
Write a program to accept any number and find whether it is even or odd by using if-else statement.
Solution: -
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf(“Enter any number=“);
scanf(“%d”,&a);
if(a%2==0)
printf(“Number is even”);
else
printf(“Number is odd”);
getch();
}

Example of switch
Write a program to understand the switch break statement.
Solution: - #include<stdio.h>
#include<conio.h>
void main()
{
int ch;
printf(“enteryour choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“first”);
break;
case 2:
printf(“secound”);
break;
case 3:
printf(“third”);
break;
default:
printf(“wrong choice”);
}
getch();
}
Example of goto statement
Write a program to print the number is even or odd?
Solution: - #include<stdio.h>
#include<conio.h>
void main()
{
int ch;
printf(“enter your choice”);
scanf(“%d”,&ch);
if(a%2==0)
goto even
else
goto odd
even:
printf(“the number is even);
goto end;
odd:
printf(“the number is odd);
goto end;
end:
getch();
}

Example of loop
For Loop
Q. 1. Write a program to display natural numbers from 1 to 10.
Solution: - #include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=1;<=10;i++)
{
printf(“%d”,i);
printf(“\n”);
}
getch();
}
Q. 2. Write a program to display table of any number (Entered user value)
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
printf(“Enter any number\n”);
scanf(“%d”,&n);
printf(“Table of %d\n”,n);
for(i=1;i<=10;i++)
{
printf(“%d*%d = %d”,n,i,n*i);
}
getch();
}
While Loop
Q. 1. Write a program to display a message (WELCOME) on the screen 10 times by using while statement.
Solution: - #include<stdio.h>
#include<conio.h>
void main()
{
int i;
while(i<=10)
{
printf(“\nWelcome”);
i++;
}
getch();
}
Q. 2. Write a program sum of digit input by user.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
printf(“Enter any number\n”);
scanf(“%d”,&n);
i=1;
while(i<=n)
{
sum=sum+i;
i++;
}
printf(“Sum of digits is : %d”,sum);
getch();
}
Do-While
Write a program to display a message (Ram) on the screen 10 times by using do-while statement.
Solution: - #include<stdio.h>
#include<conio.h>
void main()
{
int i;
i=1;
do
{
printf(“\nRam”);
i++;
}
while(i<=10);
getch();
}
Example of 1D Array
Write a program to input values into an array and display them.
Solution: - #include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
for(i=0;i>=5,i++)
{
printf(“enter the different value”);
scanf(‘%d”,&a[i]);
}
printf(“different values=”);
for(i=0;i<=5;i++)
printf(“%d”,a[i]);
getch();
}
Example of 2D Array
Write a program to print the assign value in matrix form for integer.
Solution: - #Include<stdio.h>
#include<conio.h>
void main()
{
int i,a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}},j;
printf(“matrix of order3*4\n”);
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf(“\t%d”,a[i][j]);
}
printf(“\n”);
}
getch();
}

You might also like