Section C: C
Program:
1: Write a code to
add, subtract, mulitiply and divide two numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int
num1, num2;
clrscr();
printf("Enter
First number : ");
scanf("%d",&num1);
printf("\nEnter
Second number : ");
scanf("%d",&num2);
printf("\n%d
+ %d = %d", num1, num2, (num1 + num2));
printf("\n%d
- %d = %d", num1, num2, (num1 - num2));
printf("\n%d
* %d = %d", num1, num2, (num1 * num2));
printf("\n%d
/ %d = %d", num1, num2, (num1 / num2));
getch();
}
Output:
2: Write a code to
find reminder of two numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int
num1, num2;
clrscr();
printf("Enter
First number : ");
scanf("%d",&num1);
printf("\nEnter
Second number : ");
scanf("%d",&num2);
printf("\nReminder
of %d / %d is %d", num1, num2, (num1 % num2));
getch();
}
Output:
3: Write a code to
print multiplication table of any integer entered by user.
#include<stdio.h>
#include<conio.h>
void main()
{
int
num, i;
clrscr();
printf("Enter
Number : ");
scanf("%d",&num);
printf("\nTable
of : %d\n\n", num);
for(si
= 1; i < 11; i++)
printf("%d
* %d : %d\n", num, i, (num * i));
getch();
}
Output:
4: write a code to
find maximum number out of two numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int
num1, num2;
clrscr();
printf("Enter
First number : ");
scanf("%d",&num1);
printf("\nEnter
Second number : ");
scanf("%d",&num2);
if(num1
> num2)
printf("\nMax
: %d", num1);
else
printf("\nMax
: %d", num2);
getch();
}
Output:
5: Write a code to
check weather given number is odd or even.
#include<stdio.h>
#include<conio.h>
void main()
{
int
num;
clrscr();
printf("Enter
Number : ");
scanf("%d",&num);
if(num
% 2 == 0)
printf("\n%d
is Even.");
else
printf("\n%d
is Odd.");
getch();
}
Output 1:
Output 2:
6: Write a code to
check weather entered number is prime or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int
num, i, flag = 1;
clrscr();
printf("Enter
Number : ");
scanf("%d",&num);
for(i
= 2; i < num / 2; i++)
if(num
% i == 0)
{
flag
= 0;
break;
}
if(num
== 1)
printf("\n%d
is neither prime nor composite number.", num);
else
if(flag == 1)
printf("\n%d
is Prime.", num);
else
printf("\n%d
is not Prime.", num);
getch();
}
Ouput 1:
Output
2:
Output
3:
7: Write a program
to print 1 to 10 numbers using while loop
#include<stdio.h>
#include<conio.h>
void main()
{
int
i = 1;
clrscr();
while(i
< 11)
{
printf("%d\n",
i);
i++;
}
getch();
}
Output:
8: Write a program
to print 1 to 10 numbers using for loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int
i;
clrscr();
for(i
= 1; i < 11; i++)
printf("%d\n",
i);
getch();
}
Output:
9: Write a program
to print all the elements of an array.
#include<stdio.h>
#include<conio.h>
void main()
{
int
i, a[10];
clrscr();
printf("Array
:\n\n");
for(i
= 0; i < sizeof(a) / sizeof(int); i++)
{
a[i]
= i + 1;
printf("a[%d]
: %d\n", i, a[i]);
}
getch();
}
Output:
10: Write a program
to print the following pattern using nested for loop.
*
* *
* * *
* * * *
#include<stdio.h>
#include<conio.h>
void main()
{
int
i, j, length = 4;
clrscr();
for(i
= length; i > 0; i--)
{
for(j
= i; j > 1;j--)
printf("
");
for(j
= 0 ; j <= length - i; j++)
printf("*
");
printf("\n");
}
getch();
}
Output:
11: Write a program
to print the following pattern using nested for loop.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include<stdio.h>
#include<conio.h>
void main()
{
int
i, j;
clrscr();
for(i
= 0; i < 5; i++)
{
for(j
= 0 ; j <= i; j++)
printf("%d
", j + 1);
printf("\n");
}
getch();
}
Output:
12: Write a program
to print the following pattern using nested for loop.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
#include<stdio.h>
#include<conio.h>
void main()
{
int
i, j;
clrscr();
for(i
= 0; i < 5; i++)
{
for(j = 0 ; j <= i; j++)
printf("%d
", i + 1);
printf("\n");
}
getch();
}
Output:
Comments
Post a Comment