0% found this document useful (0 votes)
19 views

Computer Programming

The document contains 15 programming questions and their solutions in C language. Each question has the question statement and the C code to solve the problem. The questions cover topics like loops, functions, arrays, strings, user input, file handling and more.

Uploaded by

Elvis Kadagi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Computer Programming

The document contains 15 programming questions and their solutions in C language. Each question has the question statement and the C code to solve the problem. The questions cover topics like loops, functions, arrays, strings, user input, file handling and more.

Uploaded by

Elvis Kadagi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

ENM211-0145/2019 KADAGI ELVIS IHAJI

MECHANICAL ENGINEERING

CAT

QUESTION 1

#include<stdio.h>

Int main()

{ float fahrehneit,celcius;

Int lower,upper,step;

Lower=0;

Upper=200;

Step=20;

printf(“c\t f”);

celcius=lower;

while(celcius<=upper)

{fahnrenheit=(9.0/5.0)*celcius+32;

printf(“%f %f”,celcus,fahrenheit);

celcius=celcius+step;

Return 0;

QUESTION 2

Unambiguous; an algorithm should be clear and each of the steps should be clear leading to one
meaning

Input; it should have well defined input or have zero inputs

Output; should have one or more well defined ouputs and they should match desired outputs

Finiteness; it should have a finite number of steps

Feasibility; it should be feasible to the available resources

Independent; it should have step by step directions which should be independent of any programming
code
QUESTION 3

They are programmer friendly i.e they are easy to write,debug and maintain

Provide high level of abstraction from machine languages

They are machine independent i.e it can run on any computer.

They have less errors and they are easy to find and debug.

It results into better programming productivity

QUESTION 4

#include<stdio.h>

Int main()

{int i,number,count;

printf(“prime numbers between 1 and 100 are:”);

for(number=1;number<=100;number++)

{count=0

For(i=2;i<=number/2;i++)

{ (if number %i==0)

count ++;

break:}

If (count ==0&&number!=1)

{ printf (“%d”,number);}

} return 0;

QUESTION 5

Translator; is a translation program that converts high level statement into the corresponding machine
code
Compiler; a written computer program which translates a source code written in high level language into
the corresponding object code of the low level language

QUESTION 6

#include<stdio.h>

Main()

{char name[20];

printf(“what is your name?”);

Scanf(“%s”,&name);

Printf(“welcome %s”,name);

Return 0;

QUESTION 7

#include<stdio.h>

#include<stdlib.h>

int main()

{ float dollars;

printf(“enter dollars”);

Scanf(“%f”,&radius);

float riel=dollars*4100;

printf(“%f”,riel);

return 0;

QUESTION 8

#include<stdio.h>

double series(int n)

{double i,sum 0.0;

for (i=1;I<=n;i++)

{sum +=(1/i);}
return(sum);}

main()

{ int a;

printf(“enter series number”);

scanf(“%d”,&a);

printf(“sn=%f”,series(a));

return 0;

QUESTION 9

#include<stdio.h>

#include<math.h>

Int powerseries(int n,ints)

{ if((pow(n,n)==1)

return(s)

else{s+ =pow(n,n);

n--;

powerseries(n,s);

main()

{int n,s=1;

printf(“how many numbers are in the series”);

scanf(“%d”,&n);

printf(“sum of the series is %d”,powerseries(n,s));

return 0;

}
QUESTION 10;

#include<stdio.h>

#include<stdlib.h>

main()

{ FILE*fp;

char c,filename[30];

int character=0,words=0,lines=0;

printf(“enter file name”);

gets(filename);

fp=fopen(filename,”r”);

if(fp==NULL)

printf(“could not open”);

return 1;

while((c=fget(fp)!=EOF);

characters++;

rewind(fp);

while((c=fgetc(fp))!=EOF)

if (c==’\n’||c==’\t’||c==’\0’)

words++;

rewind(fp);

while((c=fgetc(fp)!=EOF)

if(c==’\n’||c==’\0’)

lines++;

printf(“file %s has %d characters,%dwords,%dlines”,filename,characters,words,lines);

fclose(fp);

return 0;

}
QUESTION 11

#include<stdio.h>

#include<stdlib.>

Int_1st_weeDay(int year);

{day=(((year-1)*365)+((year-1)/4)-((year-1)/100)+((year)/400)%7;

returnDay;}

main ()

{int year,month,day,daysInMonth,WeekDay=0,startingDay;

printf(“\n enter year:”);

scanf(“%d”,&year);

char*months[ ]
={“January”,”February”,”March”,”April”,”May”,”June”,”July”,”August”,”September”,”October”,”Novem
ber”,”December”};

int monthDay[]={31,28,31,30,31,30,31,31,30,31,30,31};

if((year%4==0&&year%100!=0||year%400=0)

MonthDay[1]=29;

startingDay=get_1st_weekDay(year);

for(month=0;month<12;month++)

{daysInMonth=monthDay[month];

printf(“\n--------------%s---------\n”,months[month]);

Printf(“\n Sun Mon Tue Wed Thurs Fri Sat”\n);

for(weekday=0;weekday<startingDay;weekday++);

printf(“ “);

for(day=1;day<=dayInMonth;day++)

{ printf(“%5d”,day);

If(++weekday>6)

{printf(“\n”);

weekday=0;}

}startingDay=weekday;
}return 0;

QUESTION 12

#include<stdio.h>

#include<string.h>

{int I;

char number[40];

printf(“enter a number”);

gets(number);

printf(“\n the number of digits %d”,strien(number));

printf(\nthe digits of the number”);

for(i=0;i<number(i);i++)

printf(“%c”,number(i));

return 0;

QUESTION 13

#include<stdio.h>

main()

{ float n,p;

printf(“please enter base value and its power value”);

Scanf(“%f %f”,&n,&P);

Printf(“%f”,pow(n,p));

return 0;

QUESTION 14

#include<stdio.h>

main()
{ int trio[3];

Int a;

printf(“please enter any three numbers”);

scanf(“%d %d %d”,&trio[1],&trio[2],&trio[3]);

printf(“\n your number forward”);

for(a=0;a<3;a++)

printf(“%d”,trio[a]);

printf(“your numbers reversed”);

for(a=2;a>0;a--)

printf(“%d”,trio[a]);

return 0;

QUESTIO 15

#include<stdio.h>

main()

{ char end;

Int choice n1,n2,result;

printf(“menu”);

printf(“\t1.Add\n\t2.Substraction\n\t3.Multiply\n\t4.Divide\n\t5.Modulus”)

{ printf(“enter your choice”);

scanf(“%d”,&choice);

printf(\n enter your two numbers”)

scanf(“%d %d”,&n1,&n2);

switch(choice);

case1:

result=n1 +n2;

break;

case 2:
result=n1-n2;

break;

case 3:

result=n1*n2;

break;

case 4:

result=n1/n2;

break;

case 5:

result=n1%n2;

break;

default;

printf(“\ERROR”);

printf(“\n result:%d”,result);

printf(“\n continue?”);

scanf(“%s”,&end);

while(end==’y’);

return 0;

You might also like