Program for simple arithematic operations
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a, b, add, sub, mul, div;
printf("\nEnter 1st number: ");
scanf("%d",&a);
printf("\nEnter 2nd number: ");
scanf("%d",&b);
printf("\nAddition is: %d", a+b);
printf("\nSubtraction is: %d", a-b);
printf("\nMultiplication is: %d", a*b);
printf("\nDivision is: %d\n", a/b);
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 1.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter 1st number: 12
Enter 2nd number: 3
Addition is: 15
Subtraction is: 9
Multiplication is: 36
Division is: 4
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for swapping using third variable
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a=5, b=10, temp=0;
temp=a;
a=b;
b=temp;
printf("\n a=%d", a);
printf("\n b=%d\n", b);
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 2.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
a=10
b=5
*/
-------------------------------------------------------------------------------------------------------------------------------
Program for swapping without third variable
-------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a=5, b=10;
a=a+b;
//15=5+10;
b=a-b;
//5=15-10;
a=a-b;
//10=15-5
printf("\n a=%d", a);
printf("\n b=%d\n", b);
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 3.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
a=10
b=5
*/
---------------------------------------------------------------------------------------------------------------------------------
Program for printing natural numbers upto 10
---------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i;
for(i=1;i<=10;i++)
{
printf("\n%d\n",i);
}
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 4.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
1
2
3
4
5
6
7
8
9
10
*/
----------------------------------------------------------------------------------------------------------------------------------
Program for printing ASCII value of a number
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a;
printf("\nEnter a number: ");
scanf("%d",&a);
printf("%c\n", a);
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 5.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter a number: 66
B
*/
-----------------------------------------------------------------------------------------------------------------------------
Program for finding profit
-----------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int cp, sp, pro;
printf("\nEnter selling price: ");
scanf("%d", &sp);
printf("\nEnter cost price: ");
scanf("%d", &cp);
pro=sp-cp;
printf("\nProfit: %d\n", pro);
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 6.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter selling price: 250
Enter cost price: 200
Profit: 50
*/
---------------------------------------------------------------------------------------------------------------------------------
Program for finding greater number
---------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a, b;
printf("\nEnter 1st number: ");
scanf("%d",&a);
printf("\nEnter 2nd number: ");
scanf("%d",&b);
if(a>b)
{
printf("\n%d is greater", a);
}
else
{
printf("\n%d is greater", b);
printf("\n");
}
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 7.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter 1st number: 32
Enter 2nd number: 48
48 is greater
*/
----------------------------------------------------------------------------------------------------------------------------------
Program for finding even or odd
----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a;
printf("\nEnter a number: ");
scanf("%d",&a);
if(a%2==0)
{
printf("\nNumber is even\n");
}
else
{
printf("\nNumber is odd\n");
}
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 8.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter a number: 45
Number is odd
*/
----------------------------------------------------------------------------------------------------------------------------------
/*Program for finding if the triangle is valid
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a, b, c;
printf("\nEnter 1st side: ");
scanf("%d",&a);
printf("\nEnter 2nd side: ");
scanf("%d",&b);
printf("\nEnter 3rd side: ");
scanf("%d",&c);
if((a+b+c)==180)
{
printf("\nTriangle is valid\n");
}
else
{
printf("\nTriangle is invalid\n");
}
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 9.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter 1st side: 60
Enter 2nd side: 50
Enter 3rd side: 10
Triangle is invalid
*/
----------------------------------------------------------------------------------------------------------------------------------
Program for printing result sheet
----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a;
printf("\nEnter marks: ");
scanf("%d",&a);
if(a>=75)
{
printf("\nDistinction\n");
}
else if(a>=61 && a<=75)
{
printf("\nFirst class\n");
}
else if(a>=51 && a<=60)
{
printf("\nHigher Second class\n");
}
else if(a>=41&& a<=50)
{
printf("\nSecond class\n");
}
else if(a>=35 && a<=40)
{
printf("\nPass class\n");
}
else
{
printf("\nFail\n");
}
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 10.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter marks: 80
Distinction
*/
----------------------------------------------------------------------------------------------------------------------------------
Program for arithematic operations using switch case
----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a, b, ch;
printf("\nEnter 1st number: ");
scanf("%d",&a);
printf("\nEnter 2nd number: ");
scanf("%d",&b);
printf("\n1. Addition\n2. Substraction\n3. Multiplication\n4. Division\n5. Mod\n6. Exit\n");
printf("\nSelect one of above: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nAddition is: %d\n", a+b);
break;
case 2:
printf("\nSubstraction is: %d\n", a-b);
break;
case 3:
printf("\nMultiplicaton is: %d\n", a*b);
break;
case 4:
printf("\nDivision is: %d\n", a/b);
break;
case 5:
printf("\nMod is: %d\n", a%b);
break;
case 6:
break;
default:
printf("\nInvalid choice\n");
}
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 11.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter 1st number: 18
Enter 2nd number: 9
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Mod
6. Exit
Select one of above: 5
Mod is: 0
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for finding if the given year is leap
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a;
printf("\nEnter a year: ");
scanf("%d", &a);
if(a%4==0)
{
printf("\n%d is a leap year\n", a);
}
else
{
printf("\n%d is not a leap year\n", a);
}
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 12.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter a year: 2016
2016 is a leap year
*/
----------------------------------------------------------------------------------------------------------------------------------
Program for finding if the first number is divisible by second
----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a, b;
printf("\nEnter first number: ");
scanf("%d", &a);
printf("\nEnter second number: ");
scanf("%d", &b);
if(a%b==0)
{
printf("\nFirst number is divisible by second\n");
}
else
{
printf("\nFirst number is not divisible by second\n");
}
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 13.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter first number: 20
Enter second number: 10
First number is divisible by second
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for finding ratio of a+b to c-d
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a, b, c, d;
int ratio;
printf("\nEnter first number: ");
scanf("%d", &a);
printf("\nEnter second number: ");
scanf("%d", &b);
printf("\nEnter third number: ");
scanf("%d", &c);
printf("\nEnter fourth number: ");
scanf("%d", &d);
ratio= (float)(a+b)/(c-d);
printf("\nRatio is: %d\n", ratio);
}
/*
OUTPUT:
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for printing salary sheet
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int year;
char gen;
char qua;
printf("\nEnter Gender(m/f): ");
scanf("%c", &gen);
printf("\nEnter Years: ");
scanf("%d", &year);
printf("\nEnter quaifications(g/p): ");
scanf("%s", &qua);
if(gen=='m' && year>=10 && qua=='p')
{
printf("\nSalary: 15,000\n");
}
if(gen=='m' && year>=10 && qua=='g')
{
printf("\nSalary: 10,000\n");
}
if(gen=='m' && year<10 && qua=='p')
{
printf("\nSalary: 10,000\n");
}
if(gen=='m' && year<10 && qua=='g')
{
printf("\nSalary: 15,000\n");
}
if(gen=='f' && year>=10 && qua=='p')
{
printf("\nSalary: 12,000\n");
}
if(gen=='f' && year>=10 && qua=='g')
{
printf("\nSalary: 9,000\n");
}
if(gen=='f' && year<10 && qua=='p')
{
printf("\nSalary: 10,000\n");
}
if(gen=='f' && year<10 && qua=='g')
{
printf("\nSalary: 6,000\n");
}
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 15.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter Gender(m/f): m
Enter Years: 10
Enter quaifications(g/p): g
Salary: 15,000
*/
----------------------------------------------------------------------------------------------------------------------------------
Program for finding if the entered character is vowel or digit or consonant
----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
char chr;
printf("\nEnter a character: ");
scanf("%c", &chr);
if(chr=='a' || chr=='e' || chr=='i' || chr=='o' || chr=='u' || chr=='A' || chr=='E' || chr=='I' || chr=='O' || chr=='U')
{
printf("\n%c is a vowel\n", chr);
}
else if(chr=='0' || chr=='1' || chr=='2' || chr=='3' || chr=='4' || chr=='5' || chr=='6' || chr=='7' || chr=='8' || chr=='9')
{
printf("\n%c is a digit\n", chr);
}
else
{
printf("\n%c is consonant\n", chr);
}
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 16.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter a character: A
A is a vowel
*/
----------------------------------------------------------------------------------------------------------------------------------
Program for finding if the given number is prime
----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int count=0, i, a;
printf("\nEnter a number: ");
scanf("%d", &a);
for(i=1;i<=a;i++)
{
if(a%i==0)
{
count++;
}
}
if(count==2 && a!=2)
{
printf("\nNumber is prime\n");
}
else
{
printf("\nNumber is composite\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/6Dec2016$ gcc prime.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/6Dec2016$ ./a.out
Enter a number: 5
Number is prime
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/6Dec2016$ ./a.out
Enter a number: 6
Number is composite
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for printing factorial of a number
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i, a, fac=1;
printf("\nEnter a number: ");
scanf("%d", &a);
for(i=a;i>=1;i--)
{
fac=fac*i;
}
printf("\n%d\n",fac);
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/6Dec2016$ gcc fac.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/6Dec2016$ ./a.out
Enter a number: 5
120
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for switch case - check for palindrome, find square, find cube
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int ch, temp, num=0, num1, res=0, rem=0;
printf("\nEnter a number: ");
scanf("%d", &num);
do
{
printf("\n1. Check for Palindrome\n2. Find square\n3. Find cube\n4.Exit");
printf("\nSelect one of above: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
temp=num;
while(num!=0)
{
rem=num%10;
res=(res*10)+rem;
num=num/10;
}
if(temp==res)
{
printf("\n%d is palindrome\n",temp);
}
else
{
printf("\n%d is not a palindrome\n", temp);
}
num=temp;
break;
case 2:
num1=num*num;
printf("\nSquare of %d is %d\n", num, num1);
break;
case 3:
num1=num*num*num;
printf("\nCube of %d is %d\n", num, num1);
break;
}
}while(ch!=4);
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/6Dec2016$ gcc 3.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/6Dec2016$ ./a.out
Enter a number: 121
1. Check for Palindrome
2. Find square
3. Find cube
4.Exit
Select one of above: 1
121 is palindrome
1. Check for Palindrome
2. Find square
3. Find cube
4.Exit
Select one of above: 2
Square of 121 is 14641
1. Check for Palindrome
2. Find square
3. Find cube
4.Exit
Select one of above: 3
Cube of 121 is 1771561
1. Check for Palindrome
2. Find square
3. Find cube
4.Exit
Select one of above: 4
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program to accept month in numerical form and print number of days in it
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int ch, year;
printf("Enter month in numerical form: ");
scanf("%d", &ch);
switch(ch)
{
case 4:
case 6:
case 9:
case 11:
printf("\nMonth has 30 days\n");
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
printf("\nMonth has 31 days\n");
break;
case 2:
printf("Enter year: ");
scanf("%d", &year);
if(year%4==0)
{
printf("\nMonth has 29 days\n");
}
else
{
printf("\nMonth has 28 days\n");
}
break;
default:
printf("\nInvalid month");
break;
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/6Dec2016$ gcc 5.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/6Dec2016$ ./a.out
Enter month in numerical form: 2
Enter year: 2016
Month has 29 days
*/
------------------------------------------------------------------------------------------------------------------------------------
Print first 10 natural numbers(use of while loop)
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int n=0;
while(n<10)
{
n=n+1;
printf("\n%d\n", n);
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 1.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
1
2
3
4
5
6
7
8
9
10
*/
------------------------------------------------------------------------------------------------------------------------------------
Print addition of first 10 natural numbers
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int n=0, m;
while(n<=10)
{
m=m+n;
n++;
}
printf("\nAddition of first 10 natural numbers is: %d\n", m);
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 2.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
Addition of first 10 natural numbers is: 55
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program to print sum and average of numbers divisible by 3
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int sum=0, avg, n=0;
int count=1;
while(n<=50)
{
if(n%3==0)
{
sum=sum+n;
count=count+1;
}
n++;
}
printf("\nSum is: %d ", sum);
avg=sum/count;
printf("\nCount is : %d", count);
printf("\nAverage is : %d\n", avg);
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 3.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
Sum is: 408
Count is : 18
Average is : 22
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
* * *
* * *
* * *
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(" * ");
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 5.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
* * *
* * *
* * *
*/
------------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
1 2 3
1 2 3
1 2 3
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i,j;
for(i=0;i<3;i++)
{
for(j=1;j<=3;j++)
{
printf(" %d ", j);
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 6.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
1 2 3
1 2 3
1 2 3
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
1 1 1
2 2 2
3 3 3
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ", i);
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
1 1 1
2 2 2
3 3 3
*/
------------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
1 2 3
4 5 6
7 8 9
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a, b, add, sub, mul, div;
printf("\nEnter 1st number: ");
scanf("%d",&a);
printf("\nEnter 2nd number: ");
scanf("%d",&b);
printf("\nAddition is: %d", a+b);
printf("\nSubtraction is: %d", a-b);
printf("\nMultiplication is: %d", a*b);
printf("\nDivision is: %d\n", a/b);
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 1.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter 1st number: 12
Enter 2nd number: 3
Addition is: 15
Subtraction is: 9
Multiplication is: 36
Division is: 4
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for swapping using third variable
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a=5, b=10, temp=0;
temp=a;
a=b;
b=temp;
printf("\n a=%d", a);
printf("\n b=%d\n", b);
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 2.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
a=10
b=5
*/
-------------------------------------------------------------------------------------------------------------------------------
Program for swapping without third variable
-------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a=5, b=10;
a=a+b;
//15=5+10;
b=a-b;
//5=15-10;
a=a-b;
//10=15-5
printf("\n a=%d", a);
printf("\n b=%d\n", b);
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 3.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
a=10
b=5
*/
---------------------------------------------------------------------------------------------------------------------------------
Program for printing natural numbers upto 10
---------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i;
for(i=1;i<=10;i++)
{
printf("\n%d\n",i);
}
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 4.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
1
2
3
4
5
6
7
8
9
10
*/
----------------------------------------------------------------------------------------------------------------------------------
Program for printing ASCII value of a number
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a;
printf("\nEnter a number: ");
scanf("%d",&a);
printf("%c\n", a);
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 5.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter a number: 66
B
*/
-----------------------------------------------------------------------------------------------------------------------------
Program for finding profit
-----------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int cp, sp, pro;
printf("\nEnter selling price: ");
scanf("%d", &sp);
printf("\nEnter cost price: ");
scanf("%d", &cp);
pro=sp-cp;
printf("\nProfit: %d\n", pro);
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 6.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter selling price: 250
Enter cost price: 200
Profit: 50
*/
---------------------------------------------------------------------------------------------------------------------------------
Program for finding greater number
---------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a, b;
printf("\nEnter 1st number: ");
scanf("%d",&a);
printf("\nEnter 2nd number: ");
scanf("%d",&b);
if(a>b)
{
printf("\n%d is greater", a);
}
else
{
printf("\n%d is greater", b);
printf("\n");
}
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 7.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter 1st number: 32
Enter 2nd number: 48
48 is greater
*/
----------------------------------------------------------------------------------------------------------------------------------
Program for finding even or odd
----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a;
printf("\nEnter a number: ");
scanf("%d",&a);
if(a%2==0)
{
printf("\nNumber is even\n");
}
else
{
printf("\nNumber is odd\n");
}
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 8.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter a number: 45
Number is odd
*/
----------------------------------------------------------------------------------------------------------------------------------
/*Program for finding if the triangle is valid
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a, b, c;
printf("\nEnter 1st side: ");
scanf("%d",&a);
printf("\nEnter 2nd side: ");
scanf("%d",&b);
printf("\nEnter 3rd side: ");
scanf("%d",&c);
if((a+b+c)==180)
{
printf("\nTriangle is valid\n");
}
else
{
printf("\nTriangle is invalid\n");
}
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 9.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter 1st side: 60
Enter 2nd side: 50
Enter 3rd side: 10
Triangle is invalid
*/
----------------------------------------------------------------------------------------------------------------------------------
Program for printing result sheet
----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a;
printf("\nEnter marks: ");
scanf("%d",&a);
if(a>=75)
{
printf("\nDistinction\n");
}
else if(a>=61 && a<=75)
{
printf("\nFirst class\n");
}
else if(a>=51 && a<=60)
{
printf("\nHigher Second class\n");
}
else if(a>=41&& a<=50)
{
printf("\nSecond class\n");
}
else if(a>=35 && a<=40)
{
printf("\nPass class\n");
}
else
{
printf("\nFail\n");
}
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 10.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter marks: 80
Distinction
*/
----------------------------------------------------------------------------------------------------------------------------------
Program for arithematic operations using switch case
----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a, b, ch;
printf("\nEnter 1st number: ");
scanf("%d",&a);
printf("\nEnter 2nd number: ");
scanf("%d",&b);
printf("\n1. Addition\n2. Substraction\n3. Multiplication\n4. Division\n5. Mod\n6. Exit\n");
printf("\nSelect one of above: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nAddition is: %d\n", a+b);
break;
case 2:
printf("\nSubstraction is: %d\n", a-b);
break;
case 3:
printf("\nMultiplicaton is: %d\n", a*b);
break;
case 4:
printf("\nDivision is: %d\n", a/b);
break;
case 5:
printf("\nMod is: %d\n", a%b);
break;
case 6:
break;
default:
printf("\nInvalid choice\n");
}
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 11.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter 1st number: 18
Enter 2nd number: 9
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Mod
6. Exit
Select one of above: 5
Mod is: 0
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for finding if the given year is leap
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a;
printf("\nEnter a year: ");
scanf("%d", &a);
if(a%4==0)
{
printf("\n%d is a leap year\n", a);
}
else
{
printf("\n%d is not a leap year\n", a);
}
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 12.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter a year: 2016
2016 is a leap year
*/
----------------------------------------------------------------------------------------------------------------------------------
Program for finding if the first number is divisible by second
----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a, b;
printf("\nEnter first number: ");
scanf("%d", &a);
printf("\nEnter second number: ");
scanf("%d", &b);
if(a%b==0)
{
printf("\nFirst number is divisible by second\n");
}
else
{
printf("\nFirst number is not divisible by second\n");
}
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 13.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter first number: 20
Enter second number: 10
First number is divisible by second
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for finding ratio of a+b to c-d
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a, b, c, d;
int ratio;
printf("\nEnter first number: ");
scanf("%d", &a);
printf("\nEnter second number: ");
scanf("%d", &b);
printf("\nEnter third number: ");
scanf("%d", &c);
printf("\nEnter fourth number: ");
scanf("%d", &d);
ratio= (float)(a+b)/(c-d);
printf("\nRatio is: %d\n", ratio);
}
/*
OUTPUT:
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for printing salary sheet
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int year;
char gen;
char qua;
printf("\nEnter Gender(m/f): ");
scanf("%c", &gen);
printf("\nEnter Years: ");
scanf("%d", &year);
printf("\nEnter quaifications(g/p): ");
scanf("%s", &qua);
if(gen=='m' && year>=10 && qua=='p')
{
printf("\nSalary: 15,000\n");
}
if(gen=='m' && year>=10 && qua=='g')
{
printf("\nSalary: 10,000\n");
}
if(gen=='m' && year<10 && qua=='p')
{
printf("\nSalary: 10,000\n");
}
if(gen=='m' && year<10 && qua=='g')
{
printf("\nSalary: 15,000\n");
}
if(gen=='f' && year>=10 && qua=='p')
{
printf("\nSalary: 12,000\n");
}
if(gen=='f' && year>=10 && qua=='g')
{
printf("\nSalary: 9,000\n");
}
if(gen=='f' && year<10 && qua=='p')
{
printf("\nSalary: 10,000\n");
}
if(gen=='f' && year<10 && qua=='g')
{
printf("\nSalary: 6,000\n");
}
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 15.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter Gender(m/f): m
Enter Years: 10
Enter quaifications(g/p): g
Salary: 15,000
*/
----------------------------------------------------------------------------------------------------------------------------------
Program for finding if the entered character is vowel or digit or consonant
----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
char chr;
printf("\nEnter a character: ");
scanf("%c", &chr);
if(chr=='a' || chr=='e' || chr=='i' || chr=='o' || chr=='u' || chr=='A' || chr=='E' || chr=='I' || chr=='O' || chr=='U')
{
printf("\n%c is a vowel\n", chr);
}
else if(chr=='0' || chr=='1' || chr=='2' || chr=='3' || chr=='4' || chr=='5' || chr=='6' || chr=='7' || chr=='8' || chr=='9')
{
printf("\n%c is a digit\n", chr);
}
else
{
printf("\n%c is consonant\n", chr);
}
}
/*
OUTPUT:
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ gcc 16.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/5Dec2016$ ./a.out
Enter a character: A
A is a vowel
*/
----------------------------------------------------------------------------------------------------------------------------------
Program for finding if the given number is prime
----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int count=0, i, a;
printf("\nEnter a number: ");
scanf("%d", &a);
for(i=1;i<=a;i++)
{
if(a%i==0)
{
count++;
}
}
if(count==2 && a!=2)
{
printf("\nNumber is prime\n");
}
else
{
printf("\nNumber is composite\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/6Dec2016$ gcc prime.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/6Dec2016$ ./a.out
Enter a number: 5
Number is prime
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/6Dec2016$ ./a.out
Enter a number: 6
Number is composite
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for printing factorial of a number
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i, a, fac=1;
printf("\nEnter a number: ");
scanf("%d", &a);
for(i=a;i>=1;i--)
{
fac=fac*i;
}
printf("\n%d\n",fac);
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/6Dec2016$ gcc fac.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/6Dec2016$ ./a.out
Enter a number: 5
120
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for switch case - check for palindrome, find square, find cube
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int ch, temp, num=0, num1, res=0, rem=0;
printf("\nEnter a number: ");
scanf("%d", &num);
do
{
printf("\n1. Check for Palindrome\n2. Find square\n3. Find cube\n4.Exit");
printf("\nSelect one of above: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
temp=num;
while(num!=0)
{
rem=num%10;
res=(res*10)+rem;
num=num/10;
}
if(temp==res)
{
printf("\n%d is palindrome\n",temp);
}
else
{
printf("\n%d is not a palindrome\n", temp);
}
num=temp;
break;
case 2:
num1=num*num;
printf("\nSquare of %d is %d\n", num, num1);
break;
case 3:
num1=num*num*num;
printf("\nCube of %d is %d\n", num, num1);
break;
}
}while(ch!=4);
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/6Dec2016$ gcc 3.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/6Dec2016$ ./a.out
Enter a number: 121
1. Check for Palindrome
2. Find square
3. Find cube
4.Exit
Select one of above: 1
121 is palindrome
1. Check for Palindrome
2. Find square
3. Find cube
4.Exit
Select one of above: 2
Square of 121 is 14641
1. Check for Palindrome
2. Find square
3. Find cube
4.Exit
Select one of above: 3
Cube of 121 is 1771561
1. Check for Palindrome
2. Find square
3. Find cube
4.Exit
Select one of above: 4
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program to accept month in numerical form and print number of days in it
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int ch, year;
printf("Enter month in numerical form: ");
scanf("%d", &ch);
switch(ch)
{
case 4:
case 6:
case 9:
case 11:
printf("\nMonth has 30 days\n");
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
printf("\nMonth has 31 days\n");
break;
case 2:
printf("Enter year: ");
scanf("%d", &year);
if(year%4==0)
{
printf("\nMonth has 29 days\n");
}
else
{
printf("\nMonth has 28 days\n");
}
break;
default:
printf("\nInvalid month");
break;
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/6Dec2016$ gcc 5.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/6Dec2016$ ./a.out
Enter month in numerical form: 2
Enter year: 2016
Month has 29 days
*/
------------------------------------------------------------------------------------------------------------------------------------
Print first 10 natural numbers(use of while loop)
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int n=0;
while(n<10)
{
n=n+1;
printf("\n%d\n", n);
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 1.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
1
2
3
4
5
6
7
8
9
10
*/
------------------------------------------------------------------------------------------------------------------------------------
Print addition of first 10 natural numbers
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int n=0, m;
while(n<=10)
{
m=m+n;
n++;
}
printf("\nAddition of first 10 natural numbers is: %d\n", m);
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 2.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
Addition of first 10 natural numbers is: 55
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program to print sum and average of numbers divisible by 3
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int sum=0, avg, n=0;
int count=1;
while(n<=50)
{
if(n%3==0)
{
sum=sum+n;
count=count+1;
}
n++;
}
printf("\nSum is: %d ", sum);
avg=sum/count;
printf("\nCount is : %d", count);
printf("\nAverage is : %d\n", avg);
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 3.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
Sum is: 408
Count is : 18
Average is : 22
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
* * *
* * *
* * *
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(" * ");
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 5.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
* * *
* * *
* * *
*/
------------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
1 2 3
1 2 3
1 2 3
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i,j;
for(i=0;i<3;i++)
{
for(j=1;j<=3;j++)
{
printf(" %d ", j);
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 6.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
1 2 3
1 2 3
1 2 3
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
1 1 1
2 2 2
3 3 3
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=3;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ", i);
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
1 1 1
2 2 2
3 3 3
*/
------------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
1 2 3
4 5 6
7 8 9
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i,j;
int num=1;
for(i=0;i<3;i++)
{
for(j=1;j<=3;j++)
{
printf(" %d ", num);
num++;
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
1 2 3
4 5 6
7 8 9
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
1 0 0
0 1 0
0 0 1
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
{
printf(" 1 ");
}
else
{
printf(" 0 ");
}
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
1 0 0
0 1 0
0 0 1
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
0 1 1
1 0 1
1 1 0
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
{
printf(" 0 ");
}
else
{
printf(" 1 ");
}
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
0 1 1
1 0 1
1 1 0
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
A A A
B B B
C C C
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
char y,z;
for(y='A';y<='C';y++)
{
for(z='A';z<='C';z++)
{
printf(" %c ", y);
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
A A A
B B B
C C C
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
A B C
A B C
A B C
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
char y,z;
for(y='A';y<='C';y++)
{
for(z='A';z<='C';z++)
{
printf(" %c ", z);
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
A B C
A B C
A B C
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
A B C
D E F
G H I
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
char y,z;
char alp='A';
for(y='A';y<='C';y++)
{
for(z='A';z<='C';z++)
{
printf(" %c ", alp);
alp++;
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
A B C
D E F
G H I
*/
------------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
*
* *
* * *
* * * *
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i,j;
for(i=0;i<=3;i++)
{
for(j=0;j<=i;j++)
{
printf(" * ");
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
*
* *
* * *
* * * *
*/
------------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
* * * *
* * *
* *
*
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i,j;
for(i=0;i<=3;i++)
{
for(j=3;j>=i;j--)
{
printf(" * ");
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
* * * *
* * *
* *
*
*/
------------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
1
2 3
4 5 6
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i,j;
int num=1;
for(i=0;i<=3;i++)
{
for(j=1;j<=i;j++)
{
printf(" %d ", num);
num++;
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
1
2 3
4 5 6
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
A
B B
C C C
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
char y, z;
for(y='A';y<='C';y++)
{
for(z='A';z<=y;z++)
{
printf(" %c ", y);
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
A
B B
C C C
*/
------------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
A
B C
D E F
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
char y, z;
char alp='A';
for(y='A';y<='C';y++)
{
for(z='A';z<=y;z++)
{
printf(" %c ", alp);
alp++;
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
A
B C
D E F
*/
------------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
*
* *
* * *
* * * *
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i, j, k;
for(i=0;i<=3;i++)
{
for(k=3;k>i;k--)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
printf(" * ");
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
*
* *
* * *
* * * *
*/
------------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
* * * *
* * *
* *
*
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i, j, k;
for(i=3;i>=0;i--)
{
for(k=i;k<3;k++)
{
printf(" ");
}
for(j=i;j>=0;j--)
{
printf(" * ");
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
* * * *
* * *
* *
*
*/
------------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
0
1 1
2 2 2
3 3 3 3
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i, j, k;
for(i=0;i<=3;i++)
{
for(k=3;k>i;k--)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
printf(" %d ", i);
}
printf("\n");
}
}
/*OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 9.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
0
1 1
2 2 2
3 3 3 3
*/
------------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
A
A B
A B C
A B C D
A B C D E
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
char y,z;
char alp='A';
for(y='A';y<='E';y++)
{
for(z='A';z<=y;z++)
{
printf(" %c ", z);
alp++;
}
printf("\n");
}
}
/*OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 10.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
A
A B
A B C
A B C D
A B C D E
*/
------------------------------------------------------------------------------------------------------------------------------------
Print series - 1 2 3 4 5
- 1 4 9 16 25
- 1 8 27 64 125
- 0 7 26 63 124
- 2 9 28 65 126
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int n=0, sq=0, cu=0;
while(n<=5)
{
n=n+1;
printf(" %d ", n);
}
printf("\n");
n=1;
while(n<=5)
{
sq=n*n;
printf(" %d ", sq);
n=n+1;
}
printf("\n");
n=1;
while(n<=5)
{
cu=n*n*n;
printf(" %d ", cu);
n=n+1;
}
printf("\n");
n=1;
cu=1;
while(n<=5)
{
cu=n*n*n;
printf(" %d ", cu-1);
n=n+1;
}
printf("\n");
n=1;
cu=1;
while(n<=5)
{
cu=n*n*n;
printf(" %d ", cu+1);
n=n+1;
}
printf("\n");
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 12.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
1 2 3 4 5 6
1 4 9 16 25
1 8 27 64 125
0 7 26 63 124
2 9 28 65 126
*/
------------------------------------------------------------------------------------------------------------------------------------
Print square & cube of natural numbers upto 10
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int n=1, sq=0, cu=0;
while(n<=10)
{
sq=n*n;
printf(" %d ", sq);
n=n+1;
}
printf("\n\n");
n=1;
while(n<=10)
{
cu=n*n*n;
printf(" %d ", cu);
n=n+1;
}
printf("\n\n");
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 13.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
1 4 9 16 25 36 49 64 81 100
1 8 27 64 125 216 343 512 729 1000
*/
------------------------------------------------------------------------------------------------------------------------------------
Average of numbers between 25 & 30
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int n=25, avg=0, count=0, sum=0;
while(n>=25 && n<=30)
{
if(n%2==0)
{
sum=sum+n;
count=count+1;
}
n++;
}
avg=sum/count;
printf("\nAverage: %d", avg);
printf("\n\n");
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 15.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
Average: 28
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to print table in the form(aX1=a)
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int mul=1, res=0, num;
printf("\nEnter a number: ");
scanf("%d", &num);
while(mul<=10)
{
res=num*mul;
printf("%d X %d = %d\n", num, mul, res);
mul++;
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 17.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
Enter a number: 19
19 X 1 = 19
19 X 2 = 38
19 X 3 = 57
19 X 4 = 76
19 X 5 = 95
19 X 6 = 114
19 X 7 = 133
19 X 8 = 152
19 X 9 = 171
19 X 10 = 190
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to print sum of digits(ex. 3+2+4=9)
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int num=0, res=0, rem=0, fr=0;
printf("\nEnter a number: ");
scanf("%d", &num);
while(num!=0)
{
rem=num%10;
res=(res*10)+rem;
num=num/10;
fr=fr+rem;
}
printf("\nSum of digits: %d\n", fr);
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 18.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
Enter a number: 324
Sum of digits: 9
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to print simple array
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i;
int a[]={1,2,3,4,5,6};
for(i=0;i<5;i++)
{
printf("%d\n", a[i]);
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 1.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
1
2
3
4
5
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to accept & display array elements
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a[10], i, n;
printf("\nEnter the count of array: ");
scanf("%d", &n);
printf("\nEnter array elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("\nArray elements are: ");
for(i=0;i<n;i++)
{
printf("%d\n", a[i]);
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 2.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of array: 5
Enter array elements: 12
65
98
56
34
Array elements are: 12
65
98
56
34
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to print sum & average of array elements
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a[10], i, n=0;
int sum=0, avg=0;
printf("\nEnter the count of array: ");
scanf("%d", &n);
printf("\nEnter array elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
sum=sum+a[i];
}
printf("\nSum of array elements: %d\n", sum);
avg=sum/n;
printf("\nAverage of array elements: %d\n", avg);
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 3.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of array: 4
Enter array elements: 12
3
33
45
Sum of array elements: 93
Average of array elements: 23
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to print alternate array elements
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a[10], i, n;
printf("\nEnter the count of array: ");
scanf("%d", &n);
printf("\nEnter array elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("\nArray elements are: ");
for(i=0;i<n;i++)
{
printf("%d\n", a[i]);
i++;
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 4.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of array: 7
Enter array elements: 1
2
3
4
5
6
7
Array elements are: 1
3
5
7
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to add elements of two arrays and print sum of them in third array
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a[10], b[10], c[10], i, n;
printf("\nEnter the count of array: ");
scanf("%d", &n);
printf("\nEnter elements of first array: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("\nEnter elements of second array: ");
for(i=0;i<n;i++)
{
scanf("%d", &b[i]);
}
for(i=0;i<n;i++)
{
c[i]=a[i]+b[i];
printf("\nSum of array element of a[%d] and b[%d] : %d\n", a[i], b[i], c[i]);
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 5.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of array: 4
Enter elements of first array: 1
2
3
4
Enter elements of second array: 1
2
3
4
Sum of array element of a[1] and b[1] : 2
Sum of array element of a[2] and b[2] : 4
Sum of array element of a[3] and b[3] : 6
Sum of array element of a[4] and b[4] : 8
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to swap the elements of two arrays
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a[10], b[10], temp[10], i, n;
printf("\nEnter the count of array: ");
scanf("%d", &n);
printf("\nEnter elements of first array: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("\nEnter elements of second array: ");
for(i=0;i<n;i++)
{
scanf("%d", &b[i]);
}
for(i=0;i<n;i++)
{
temp[i]=a[i];
a[i]=b[i];
b[i]=temp[i];
}
printf("\nElements of first array: ");
for(i=0;i<n;i++)
{
printf("%d\n", a[i]);
}
printf("\nElements of second array: ");
for(i=0;i<n;i++)
{
printf("%d\n", b[i]);
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 6.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of array: 3
Enter elements of first array: 1
2
3
Enter elements of second array: 4
5
6
Elements of first array: 4
5
6
Elements of second array: 1
2
3
*/
-------------------------------------------------------------------------------------------------------------------------------------
ProAccept and Print a matrix
#include<stdio.h>
void main()
{
int r, c, a[4][4], i,j;
printf("\nEnter the count of rows: ");
scanf("%d", &r);
printf("\nEnter the count of coloumns: ");
scanf("%d", &c);
printf("\nEnter row & columns: ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("a[%d][%d]: ",i,j);
scanf("%d", &a[i][j]);
}
}
printf("\nMatrix elements are: ");
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(" %d ", a[i][j]);
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 7.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of rows: 2
Enter the count of coloumns: 2
Enter row & columns: a[0][0]: 1
a[0][1]: 1
a[1][0]: 1
a[1][1]: 1
Matrix elements are:
1 1
1 1
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to print addition of two matrices
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int r, c, a[4][4], b[4][4], sum[4][4], i,j;
printf("\nEnter the count of rows: ");
scanf("%d", &r);
printf("\nEnter the count of coloumns: ");
scanf("%d", &c);
printf("\nEnter elements of first matrix: ");
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
printf("a[%d][%d]",i,j);
scanf("%d", &a[i][j]);
}
}
printf("\nEnter elements of second matrix: ");
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
printf("b[%d] [%d]",i,j);
scanf("%d", &b[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
sum[i][j]=a[i][j]+b[i][j];
printf("\nSum of Matrix elements are: a[%d][%d] = %d and b[%d][%d] = %d: sum[%d][%d] = %d ",i,j,a[i][j],i,j,b[i][j],i,j,sum[i][j]);
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 8.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of rows: 2
Enter the count of columns: 2
Enter elements of first matrix: a[0][0]1
a[0][1]1
a[1][0]1
a[1][1]1
Enter elements of second matrix: b[0] [0]1
b[0] [1]1
b[1] [0]1
b[1] [1]1
Sum of Matrix elements are: a[0][0] = 1 and b[0][0] = 1: sum[0][0] = 2
Sum of Matrix elements are: a[0][1] = 1 and b[0][1] = 1: sum[0][1] = 2
Sum of Matrix elements are: a[1][0] = 1 and b[1][0] = 1: sum[1][0] = 2
Sum of Matrix elements are: a[1][1] = 1 and b[1][1] = 1: sum[1][1] = 2
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to print multiplication of 3X3 matrices
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int k, a[4][4], b[4][4], mul[4][4], i,j;
printf("\nEnter elements of first matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("a[%d][%d]",i,j);
scanf("%d", &a[i][j]);
}
}
printf("\nEnter elements of second matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("b[%d][%d]",i,j);
scanf("%d", &b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
mul[i][j]=0;
for(k=0;k<3;k++)
{
mul[i][j]=mul[i][j]+(a[i][k]*b[k][j]);
}
}
}
printf("\nMultiplication of matrices: \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ",mul[i][j]);
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter elements of first matrix: a[0][0]1
a[0][1]1
a[0][2]1
a[1][0]1
a[1][1]1
a[1][2]1
a[2][0]1
a[2][1]1
a[2][2]1
Enter elements of second matrix: b[0][0]1
b[0][1]1
b[0][2]1
b[1][0]1
b[1][1]1
b[1][2]1
b[2][0]1
b[2][1]1
b[2][2]1
Multiplication of matrices:
3 3 3
3 3 3
3 3 3
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to print common elements from two different arrays
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a[10], b[10], temp[10], i, n, j;
printf("\nEnter the count of array: ");
scanf("%d", &n);
printf("\nEnter elements of first array: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("\nEnter elements of second array: ");
for(i=0;i<n;i++)
{
scanf("%d", &b[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i]==b[j])
{
printf("\n%d is the same element in both the arrays\n", a[i]);
}
}
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 12.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of array: 5
Enter elements of first array: 10
23
11
45
33
Enter elements of second array: 33
12
55
11
09
11 is the same element in both the arrays
33 is the same element in both the arrays
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to add 5 to each array elements
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a[10], i, n;
printf("\nEnter the count of array: ");
scanf("%d", &n);
printf("\nEnter array elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("\nArray elements on adding 5 in each: ");
for(i=0;i<n;i++)
{
a[i]=a[i]+5;
printf("%d\n", a[i]);
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 13.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of array: 4
Enter array elements: 1
2
3
4
Array elements on adding 5 in each: 6
7
8
9
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to search a particular element in an array and print its location
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a[10], i, n, k=0;
printf("\nEnter the count of array: ");
scanf("%d", &n);
printf("\nEnter array elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("\nEnter the element to be searched: ");
scanf("%d", &k);
for(i=0;i<n;i++)
{
if(k==a[i])
{
printf("\nElement is found at location a[%d]\n", i);
}
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 14.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of array: 4
Enter array elements: 1
2
3
4
Enter the element to be searched: 3
Element is found at location a[2]
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to sort array elements in ascending and descending order
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a[10], i ,j, n, temp;
printf("\nEnter the count of array: ");
scanf("%d", &n);
printf("\nEnter array elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nArray elements after sorting in ascending order: ");
for(i=0;i<n;i++)
{
printf("%d\n", a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nArray elements after sorting in descding order: ");
for(i=0;i<n;i++)
{
printf("%d\n", a[i]);
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 15.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of array: 5
Enter array elements: 5
2
7
4
1
Array elements after sorting in ascending order: 1
2
4
5
7
Array elements after sorting in descding order: 7
5
4
2
1
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to declare a float array and and find its max and min
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
float a[]={1.1, 2.2, 5.4, 3.3, 4.2}, max, min;
int i;
for(i=0;i<5;i++)
{
if(i==0)
{
max=a[i];
min=a[i];
}
else if(a[i]>max)
{
max=a[i];
}
else if(a[i]<min)
{
min=a[i];
}
}
printf("\nMax: %f\n",max);
printf("\nMin: %f\n",min);
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 17.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Max: 5.400000
Min: 1.100000
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to find greater number using function
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a, b;
void check();
printf("\nEnter first number: ");
scanf("%d", &a);
printf("\nEnter second numer: ");
scanf("%d", &b);
check(a, b);
}
void check(a, b)
{
if(a>b)
{
printf("\nFirst is greater\n");
}
else if(a<b)
{
printf("\nSecond is greater\n");
}
else
{
printf("\nBoth are equal\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 18.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter first number: 44
Enter second numer: 55
Second is greater
*/
#include<stdio.h>
void main()
{
int i,j;
int num=1;
for(i=0;i<3;i++)
{
for(j=1;j<=3;j++)
{
printf(" %d ", num);
num++;
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
1 2 3
4 5 6
7 8 9
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
1 0 0
0 1 0
0 0 1
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
{
printf(" 1 ");
}
else
{
printf(" 0 ");
}
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
1 0 0
0 1 0
0 0 1
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
0 1 1
1 0 1
1 1 0
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
{
printf(" 0 ");
}
else
{
printf(" 1 ");
}
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
0 1 1
1 0 1
1 1 0
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
A A A
B B B
C C C
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
char y,z;
for(y='A';y<='C';y++)
{
for(z='A';z<='C';z++)
{
printf(" %c ", y);
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
A A A
B B B
C C C
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
A B C
A B C
A B C
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
char y,z;
for(y='A';y<='C';y++)
{
for(z='A';z<='C';z++)
{
printf(" %c ", z);
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
A B C
A B C
A B C
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
A B C
D E F
G H I
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
char y,z;
char alp='A';
for(y='A';y<='C';y++)
{
for(z='A';z<='C';z++)
{
printf(" %c ", alp);
alp++;
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
A B C
D E F
G H I
*/
------------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
*
* *
* * *
* * * *
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i,j;
for(i=0;i<=3;i++)
{
for(j=0;j<=i;j++)
{
printf(" * ");
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
*
* *
* * *
* * * *
*/
------------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
* * * *
* * *
* *
*
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i,j;
for(i=0;i<=3;i++)
{
for(j=3;j>=i;j--)
{
printf(" * ");
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
* * * *
* * *
* *
*
*/
------------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
1
2 3
4 5 6
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i,j;
int num=1;
for(i=0;i<=3;i++)
{
for(j=1;j<=i;j++)
{
printf(" %d ", num);
num++;
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
1
2 3
4 5 6
*/
-----------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
A
B B
C C C
-----------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
char y, z;
for(y='A';y<='C';y++)
{
for(z='A';z<=y;z++)
{
printf(" %c ", y);
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
A
B B
C C C
*/
------------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
A
B C
D E F
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
char y, z;
char alp='A';
for(y='A';y<='C';y++)
{
for(z='A';z<=y;z++)
{
printf(" %c ", alp);
alp++;
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
A
B C
D E F
*/
------------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
*
* *
* * *
* * * *
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i, j, k;
for(i=0;i<=3;i++)
{
for(k=3;k>i;k--)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
printf(" * ");
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
*
* *
* * *
* * * *
*/
------------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
* * * *
* * *
* *
*
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i, j, k;
for(i=3;i>=0;i--)
{
for(k=i;k<3;k++)
{
printf(" ");
}
for(j=i;j>=0;j--)
{
printf(" * ");
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
* * * *
* * *
* *
*
*/
------------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
0
1 1
2 2 2
3 3 3 3
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i, j, k;
for(i=0;i<=3;i++)
{
for(k=3;k>i;k--)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
printf(" %d ", i);
}
printf("\n");
}
}
/*OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 9.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
0
1 1
2 2 2
3 3 3 3
*/
------------------------------------------------------------------------------------------------------------------------------------
Program for printing pattern
A
A B
A B C
A B C D
A B C D E
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
char y,z;
char alp='A';
for(y='A';y<='E';y++)
{
for(z='A';z<=y;z++)
{
printf(" %c ", z);
alp++;
}
printf("\n");
}
}
/*OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 10.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
A
A B
A B C
A B C D
A B C D E
*/
------------------------------------------------------------------------------------------------------------------------------------
Print series - 1 2 3 4 5
- 1 4 9 16 25
- 1 8 27 64 125
- 0 7 26 63 124
- 2 9 28 65 126
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int n=0, sq=0, cu=0;
while(n<=5)
{
n=n+1;
printf(" %d ", n);
}
printf("\n");
n=1;
while(n<=5)
{
sq=n*n;
printf(" %d ", sq);
n=n+1;
}
printf("\n");
n=1;
while(n<=5)
{
cu=n*n*n;
printf(" %d ", cu);
n=n+1;
}
printf("\n");
n=1;
cu=1;
while(n<=5)
{
cu=n*n*n;
printf(" %d ", cu-1);
n=n+1;
}
printf("\n");
n=1;
cu=1;
while(n<=5)
{
cu=n*n*n;
printf(" %d ", cu+1);
n=n+1;
}
printf("\n");
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 12.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
1 2 3 4 5 6
1 4 9 16 25
1 8 27 64 125
0 7 26 63 124
2 9 28 65 126
*/
------------------------------------------------------------------------------------------------------------------------------------
Print square & cube of natural numbers upto 10
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int n=1, sq=0, cu=0;
while(n<=10)
{
sq=n*n;
printf(" %d ", sq);
n=n+1;
}
printf("\n\n");
n=1;
while(n<=10)
{
cu=n*n*n;
printf(" %d ", cu);
n=n+1;
}
printf("\n\n");
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 13.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
1 4 9 16 25 36 49 64 81 100
1 8 27 64 125 216 343 512 729 1000
*/
------------------------------------------------------------------------------------------------------------------------------------
Average of numbers between 25 & 30
------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int n=25, avg=0, count=0, sum=0;
while(n>=25 && n<=30)
{
if(n%2==0)
{
sum=sum+n;
count=count+1;
}
n++;
}
avg=sum/count;
printf("\nAverage: %d", avg);
printf("\n\n");
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 15.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
Average: 28
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to print table in the form(aX1=a)
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int mul=1, res=0, num;
printf("\nEnter a number: ");
scanf("%d", &num);
while(mul<=10)
{
res=num*mul;
printf("%d X %d = %d\n", num, mul, res);
mul++;
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 17.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
Enter a number: 19
19 X 1 = 19
19 X 2 = 38
19 X 3 = 57
19 X 4 = 76
19 X 5 = 95
19 X 6 = 114
19 X 7 = 133
19 X 8 = 152
19 X 9 = 171
19 X 10 = 190
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to print sum of digits(ex. 3+2+4=9)
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int num=0, res=0, rem=0, fr=0;
printf("\nEnter a number: ");
scanf("%d", &num);
while(num!=0)
{
rem=num%10;
res=(res*10)+rem;
num=num/10;
fr=fr+rem;
}
printf("\nSum of digits: %d\n", fr);
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ gcc 18.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/8Dec2016$ ./a.out
Enter a number: 324
Sum of digits: 9
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to print simple array
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int i;
int a[]={1,2,3,4,5,6};
for(i=0;i<5;i++)
{
printf("%d\n", a[i]);
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 1.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
1
2
3
4
5
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to accept & display array elements
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a[10], i, n;
printf("\nEnter the count of array: ");
scanf("%d", &n);
printf("\nEnter array elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("\nArray elements are: ");
for(i=0;i<n;i++)
{
printf("%d\n", a[i]);
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 2.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of array: 5
Enter array elements: 12
65
98
56
34
Array elements are: 12
65
98
56
34
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to print sum & average of array elements
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a[10], i, n=0;
int sum=0, avg=0;
printf("\nEnter the count of array: ");
scanf("%d", &n);
printf("\nEnter array elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
sum=sum+a[i];
}
printf("\nSum of array elements: %d\n", sum);
avg=sum/n;
printf("\nAverage of array elements: %d\n", avg);
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 3.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of array: 4
Enter array elements: 12
3
33
45
Sum of array elements: 93
Average of array elements: 23
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to print alternate array elements
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a[10], i, n;
printf("\nEnter the count of array: ");
scanf("%d", &n);
printf("\nEnter array elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("\nArray elements are: ");
for(i=0;i<n;i++)
{
printf("%d\n", a[i]);
i++;
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 4.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of array: 7
Enter array elements: 1
2
3
4
5
6
7
Array elements are: 1
3
5
7
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to add elements of two arrays and print sum of them in third array
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a[10], b[10], c[10], i, n;
printf("\nEnter the count of array: ");
scanf("%d", &n);
printf("\nEnter elements of first array: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("\nEnter elements of second array: ");
for(i=0;i<n;i++)
{
scanf("%d", &b[i]);
}
for(i=0;i<n;i++)
{
c[i]=a[i]+b[i];
printf("\nSum of array element of a[%d] and b[%d] : %d\n", a[i], b[i], c[i]);
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 5.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of array: 4
Enter elements of first array: 1
2
3
4
Enter elements of second array: 1
2
3
4
Sum of array element of a[1] and b[1] : 2
Sum of array element of a[2] and b[2] : 4
Sum of array element of a[3] and b[3] : 6
Sum of array element of a[4] and b[4] : 8
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to swap the elements of two arrays
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a[10], b[10], temp[10], i, n;
printf("\nEnter the count of array: ");
scanf("%d", &n);
printf("\nEnter elements of first array: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("\nEnter elements of second array: ");
for(i=0;i<n;i++)
{
scanf("%d", &b[i]);
}
for(i=0;i<n;i++)
{
temp[i]=a[i];
a[i]=b[i];
b[i]=temp[i];
}
printf("\nElements of first array: ");
for(i=0;i<n;i++)
{
printf("%d\n", a[i]);
}
printf("\nElements of second array: ");
for(i=0;i<n;i++)
{
printf("%d\n", b[i]);
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 6.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of array: 3
Enter elements of first array: 1
2
3
Enter elements of second array: 4
5
6
Elements of first array: 4
5
6
Elements of second array: 1
2
3
*/
-------------------------------------------------------------------------------------------------------------------------------------
ProAccept and Print a matrix
#include<stdio.h>
void main()
{
int r, c, a[4][4], i,j;
printf("\nEnter the count of rows: ");
scanf("%d", &r);
printf("\nEnter the count of coloumns: ");
scanf("%d", &c);
printf("\nEnter row & columns: ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("a[%d][%d]: ",i,j);
scanf("%d", &a[i][j]);
}
}
printf("\nMatrix elements are: ");
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(" %d ", a[i][j]);
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 7.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of rows: 2
Enter the count of coloumns: 2
Enter row & columns: a[0][0]: 1
a[0][1]: 1
a[1][0]: 1
a[1][1]: 1
Matrix elements are:
1 1
1 1
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to print addition of two matrices
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int r, c, a[4][4], b[4][4], sum[4][4], i,j;
printf("\nEnter the count of rows: ");
scanf("%d", &r);
printf("\nEnter the count of coloumns: ");
scanf("%d", &c);
printf("\nEnter elements of first matrix: ");
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
printf("a[%d][%d]",i,j);
scanf("%d", &a[i][j]);
}
}
printf("\nEnter elements of second matrix: ");
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
printf("b[%d] [%d]",i,j);
scanf("%d", &b[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
sum[i][j]=a[i][j]+b[i][j];
printf("\nSum of Matrix elements are: a[%d][%d] = %d and b[%d][%d] = %d: sum[%d][%d] = %d ",i,j,a[i][j],i,j,b[i][j],i,j,sum[i][j]);
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 8.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of rows: 2
Enter the count of columns: 2
Enter elements of first matrix: a[0][0]1
a[0][1]1
a[1][0]1
a[1][1]1
Enter elements of second matrix: b[0] [0]1
b[0] [1]1
b[1] [0]1
b[1] [1]1
Sum of Matrix elements are: a[0][0] = 1 and b[0][0] = 1: sum[0][0] = 2
Sum of Matrix elements are: a[0][1] = 1 and b[0][1] = 1: sum[0][1] = 2
Sum of Matrix elements are: a[1][0] = 1 and b[1][0] = 1: sum[1][0] = 2
Sum of Matrix elements are: a[1][1] = 1 and b[1][1] = 1: sum[1][1] = 2
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to print multiplication of 3X3 matrices
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int k, a[4][4], b[4][4], mul[4][4], i,j;
printf("\nEnter elements of first matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("a[%d][%d]",i,j);
scanf("%d", &a[i][j]);
}
}
printf("\nEnter elements of second matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("b[%d][%d]",i,j);
scanf("%d", &b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
mul[i][j]=0;
for(k=0;k<3;k++)
{
mul[i][j]=mul[i][j]+(a[i][k]*b[k][j]);
}
}
}
printf("\nMultiplication of matrices: \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ",mul[i][j]);
}
printf("\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 20.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter elements of first matrix: a[0][0]1
a[0][1]1
a[0][2]1
a[1][0]1
a[1][1]1
a[1][2]1
a[2][0]1
a[2][1]1
a[2][2]1
Enter elements of second matrix: b[0][0]1
b[0][1]1
b[0][2]1
b[1][0]1
b[1][1]1
b[1][2]1
b[2][0]1
b[2][1]1
b[2][2]1
Multiplication of matrices:
3 3 3
3 3 3
3 3 3
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to print common elements from two different arrays
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a[10], b[10], temp[10], i, n, j;
printf("\nEnter the count of array: ");
scanf("%d", &n);
printf("\nEnter elements of first array: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("\nEnter elements of second array: ");
for(i=0;i<n;i++)
{
scanf("%d", &b[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i]==b[j])
{
printf("\n%d is the same element in both the arrays\n", a[i]);
}
}
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 12.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of array: 5
Enter elements of first array: 10
23
11
45
33
Enter elements of second array: 33
12
55
11
09
11 is the same element in both the arrays
33 is the same element in both the arrays
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to add 5 to each array elements
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a[10], i, n;
printf("\nEnter the count of array: ");
scanf("%d", &n);
printf("\nEnter array elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("\nArray elements on adding 5 in each: ");
for(i=0;i<n;i++)
{
a[i]=a[i]+5;
printf("%d\n", a[i]);
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 13.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of array: 4
Enter array elements: 1
2
3
4
Array elements on adding 5 in each: 6
7
8
9
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to search a particular element in an array and print its location
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a[10], i, n, k=0;
printf("\nEnter the count of array: ");
scanf("%d", &n);
printf("\nEnter array elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf("\nEnter the element to be searched: ");
scanf("%d", &k);
for(i=0;i<n;i++)
{
if(k==a[i])
{
printf("\nElement is found at location a[%d]\n", i);
}
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 14.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of array: 4
Enter array elements: 1
2
3
4
Enter the element to be searched: 3
Element is found at location a[2]
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to sort array elements in ascending and descending order
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a[10], i ,j, n, temp;
printf("\nEnter the count of array: ");
scanf("%d", &n);
printf("\nEnter array elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nArray elements after sorting in ascending order: ");
for(i=0;i<n;i++)
{
printf("%d\n", a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nArray elements after sorting in descding order: ");
for(i=0;i<n;i++)
{
printf("%d\n", a[i]);
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 15.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter the count of array: 5
Enter array elements: 5
2
7
4
1
Array elements after sorting in ascending order: 1
2
4
5
7
Array elements after sorting in descding order: 7
5
4
2
1
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to declare a float array and and find its max and min
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
float a[]={1.1, 2.2, 5.4, 3.3, 4.2}, max, min;
int i;
for(i=0;i<5;i++)
{
if(i==0)
{
max=a[i];
min=a[i];
}
else if(a[i]>max)
{
max=a[i];
}
else if(a[i]<min)
{
min=a[i];
}
}
printf("\nMax: %f\n",max);
printf("\nMin: %f\n",min);
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 17.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Max: 5.400000
Min: 1.100000
*/
-------------------------------------------------------------------------------------------------------------------------------------
Program to find greater number using function
-------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
void main()
{
int a, b;
void check();
printf("\nEnter first number: ");
scanf("%d", &a);
printf("\nEnter second numer: ");
scanf("%d", &b);
check(a, b);
}
void check(a, b)
{
if(a>b)
{
printf("\nFirst is greater\n");
}
else if(a<b)
{
printf("\nSecond is greater\n");
}
else
{
printf("\nBoth are equal\n");
}
}
/*
OUTPUT :
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ gcc 18.c
sd@sd-SVE15135CNW:~/Desktop/PLCrashCourse/9Dec2016$ ./a.out
Enter first number: 44
Enter second numer: 55
Second is greater
*/
No comments:
Post a Comment