Skip to content

Instantly share code, notes, and snippets.

@NiharG15
Last active August 29, 2015 14:08
Show Gist options
  • Save NiharG15/6219e0551799ab305d45 to your computer and use it in GitHub Desktop.
Save NiharG15/6219e0551799ab305d45 to your computer and use it in GitHub Desktop.
FPL C2 Assignments
/*
* matrix.c
*
* Created on: Nov 4, 2014
*/
#include<stdio.h>
int main() {
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of Rows and Columns in matrix: ");
_flushall();
scanf("%d%d", &m, &n);
printf("Enter the elemets of first matrix: ");
_flushall();
for (c = 0; c < m; c++) {
for (d = 0; d < n; d++) {
scanf("%d", &first[c][d]);
}
}
printf("Enter the elemets of second matrix: ");
_flushall();
for (c = 0; c < m; c++) {
for (d = 0; d < n; d++) {
scanf("%d", &second[c][d]);
}
}
for (c = 0; c < m; c++) {
for (d = 0; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
}
}
printf("\nSum of first and second matrices is \n");
_flushall();
for (c = 0; c < m; c++) {
for (d = 0; d < n; d++) {
printf("%d\t", sum[c][d]);
}
printf("\n");
}
return 0;
}
/*
Output:
Enter the number of Rows and Columns in matrix: 3
3
Enter the elemets of first matrix: 1
2
4
5
6
3
2
1
2
Enter the elemets of second matrix: 3
2
1
5
4
6
8
7
4
Sum of first and second matrices is
4 4 5
10 10 9
10 8 6
*/
/*
* sum.c
*
* Created on: Sep 5, 2014
*
*/
#include<stdio.h>
void main() {
int sum =0, i;
for(i=0; i<=100; i++) {
if(i%4==0) {
sum= sum + i;
}
}
printf("The sum is %d.", sum);
}
/*
Output:
The Sum is 1300.
*/
/*
* number.c
*
* Created on: Sep 19, 2014
*/
#include<stdio.h>
void main () {
int num, units, tens;
printf("Enter a number: ");
_flushall();
scanf("%d", &num);
if(num >= 100) {
printf("Invalid Number");
} else if(num > 10 && num < 20) {
switch(num) {
case 11: printf("Eleven"); break;
case 12: printf("Twelve"); break;
case 13: printf("Thirteen"); break;
case 14: printf("Fourteen"); break;
case 15: printf("Fifteen"); break;
case 16: printf("Sixteen"); break;
case 17: printf("Seventeen"); break;
case 18: printf("Eighteen"); break;
case 19: printf("Nineteen"); break;
}
} else {
tens = num/10;
units = num%10;
switch(tens) {
case 2: printf("Twenty "); break;
case 3: printf("Thirty "); break;
case 4: printf("Forty "); break;
case 5: printf("Fifty "); break;
case 6: printf("Sixty "); break;
case 7: printf("Seventy "); break;
case 8: printf("Eighty "); break;
case 9: printf("Ninety "); break;
}
switch(units) {
case 1: printf("One"); break;
case 2: printf("Two"); break;
case 3: printf("Three"); break;
case 4: printf("Four"); break;
case 5: printf("Five"); break;
case 6: printf("Six"); break;
case 7: printf("Seven"); break;
case 8: printf("Eight"); break;
case 9: printf("Nine"); break;
}
}
}
/*
Output:
Enter a number: 65
Sixty Five
*/
/*
* fibonacci.c
*
* Created on: Sep 5, 2014
*/
#include<stdio.h>
void main() {
int i, temp1 = 0, temp2 = 1, temp3;
printf("Fibonacci series till 20 is \n");
printf("%d \n%d \n", temp1, temp2);
for(i=0; i<20; i++) {
temp3 = temp2 + temp1;
printf("%d \n", temp3);
temp1 = temp2;
temp2 = temp3;
}
}
/*
Output:
Fibonacci series till 20 is
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
*/
/*
* prime.c
*
* Created on: Sep 19, 2014
*/
#include<stdio.h>
void main() {
int a, k, t, p;
printf("Enter the value of k: ");
_flushall();
scanf("%d", &k);
printf("Prime numbers upto %d are: \n",k );
for(a=1; a<=k; a++) {
p = 0;
for(t=1; t<=a; t++) {
if(a%t==0) { p++; }
}
if(p==2) {
printf("%d ", a);
}
}
}
/*
Output:
Enter the value of k: 50
Prime numbers upto 50 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
*/
/*
* factorial.c
*
* Created on: Sep 26, 2014
*/
#include<stdio.h>
int factorial(int); //Declaration of Function
void main() {
int n, fact;
printf("Enter a number to calculate factorial: ");
_flushall();
scanf("%d", &n);
fact = factorial(n); //Function Call
printf("Factorial of %d is %d", n, fact);
}
int factorial(int x) {
int f;
if(x==1) {
return 1;
}
else {
f = x*factorial(x-1);
return f;
}
}
/*
Output:
Enter a number to calculate factorial: 6
Factorial of 6 is 720
*/
/*
* bubblesort.c
*
* Created on: Sep 26, 2014
*/
#include<stdio.h>
void main() {
int temp, i, j, n;
int a[10];
printf("Enter number of elements of an array: ");
_flushall();
scanf("%d", &n);
printf("Enter the elements of array:");
_flushall();
for(i=0;i<n;i++) {
scanf("%d", &a[i]);
}
for(i=0;i<n;i++) {
for(j=0;j<n-i;j++) {
if(a[j] > a[j+1]) {
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Sorted array is:");
for(i=0;i<n;i++) {
printf(" %d ", a[i]);
_flushall();
}
}
/*
Output:
Enter number of elements of an array: 5
Enter the elements of array:1 3 4 2 8
Sorted array is: 1 2 3 4 8
*/
/*
* string_operations.c
*
* Created on: Nov 4, 2014
*/
#include<stdio.h>
#include<string.h>
void main() {
char a[30], b[30];
int l;
printf("Enter a string: ");
_flushall();
scanf("%s", &a);
l = strlen(a);
printf("\n Length of given string is %d.", l);
printf("\n Enter another string: ");
_flushall();
scanf("%s", &b);
strcat(a,b);
printf("\n Concatenated string is %s.", a);
}
/* Output:
Enter a string: Hello
Length of given string is 5.
Enter another string: World!
Concatenated string is HelloWorld!.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment