Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
0 views5 pages

FCP Assignment 2

The document contains code snippets for various programming assignments in C, including functions for checking numbers, calculating sums, and converting decimal to octal. It features user input handling, error checking, and mathematical operations such as finding the greatest common divisor (GCD) and least common multiple (LCM). Each section is focused on specific programming tasks, demonstrating fundamental concepts in C programming.

Uploaded by

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

FCP Assignment 2

The document contains code snippets for various programming assignments in C, including functions for checking numbers, calculating sums, and converting decimal to octal. It features user input handling, error checking, and mathematical operations such as finding the greatest common divisor (GCD) and least common multiple (LCM). Each section is focused on specific programming tasks, demonstrating fundamental concepts in C programming.

Uploaded by

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

FCP Assignment 2

Anurag Bhunia

Chapter 3

Q5.
#include <stdio.h>
int check(int arr[16],int n){
for(int i=0;i<16;i++){
if(arr[i]==n){
return 1;
}
}
return 0;
}
int main(){
int
arr[16]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16},a[16]={};
printf("Enter the numbers 1 through 16 in any order:\n");
int i=0;
while(i<16){
int n;
scanf("%d",&n);
if(check(arr,n)==0){
printf("The number you gave is not in the list.
Please try again.\n");
continue;
}
else if(check(a,n)==1){
printf("You have already entered that
number.Please try again.\n");
}
else{
a[i++]=n;
}
}
for(int i=0;i<4;i++){
for(int j=4*i;j<4*i+4;j++){
printf("%d ",a[j]);
}
printf("\n");
}
printf("The row sums are :");
for(int i=0;i<4;i++){
int s=0;
for(int j=4*i;j<4*(i+1);j++){
s+=a[j];
}
printf("%d ",s);
}
printf("\nThe column sums are:");
for(int i=0;i<4;i++){
int s1=0;
for(int j=0;j<4;j++){
s1+=a[i+4*j];
}
printf("%d ",s1);
}
printf("\nThe diagonal sums are:");
int s2=0,s3=0;
for(int i=0;i<4;i++){
s2+=a[5*i];
}
for(int i=1;i<=4;i++){
s3+=a[3*i];
}
printf("%d %d",s2,s3);
}

Q6.
#include <stdio.h>
int min(int a,int b){
if(a<b){
return a;
}
else{
return b;
}
}
int max(int a,int b){
if(a>b){
return a;
}
else{
return b;
}
}
int gcd(int a,int b){
int n=max(a,b),m=min(a,b),d=1;
for(int i=2;i<=m;i++){
if(m%i==0 && n%i==0){
d=i;
}
}
return d;
}
int lcm(int a,int b){
int n=max(a,b),m=min(a,b),j=0,k=1;
while(j<1){
if((n*k)%m==0){
j++;
}
else{
k++;
}
}
return n*k;
}
int main(){
printf("Enter the two fractions seperated by a plus
sign:");
int a,b,c,d;
scanf("%d/%d+%d/%d",&a,&b,&c,&d);
int num=(a*(lcm(b,d)/b))+(c*(lcm(b,d)/d)),den=lcm(b,d);
if(den/gcd(num,den)==1){
printf("The sum is %d",num/gcd(num,den));
}
else{
printf("The sum is
%d/%d",num/gcd(num,den),den/gcd(num,den));
}
}
Chapter 4

Q1.
#include <stdio.h>
int main(){
printf("Enter the number:");
int n,r=0;
scanf("%d",&n);
while(n>0){
r=(r*10)+n%10;
n/=10;
}
printf("The reversed number is %d",r);
}

Q4.
#include <stdio.h>
int ndig(int n){
int i=0;
while(n>0){
i++;
n/=10;
}
return i;
}
int exp(int n,int m){
if(m==0){
return 1;
}
else{
return n*exp(n,m-1);
}
}
int main(){
int n,i=0,d=0;
printf("Enter the number in decimal:");
scanf("%d",&n);
while(n>0){
d=d+(n%8)*exp(10,i);
n=n/8;
i++;
}
printf("The octal form of this number is:\n");
if(ndig(d)>=5){
printf("%d",d);
}
else{
for(int i=0;i<5-ndig(d);i++){
printf("0");
}
printf("%d",d);
}

You might also like