Computer Programming
Computer Programming
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
Output; should have one or more well defined ouputs and they should match desired outputs
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
They have less errors and they are easy to find and debug.
QUESTION 4
#include<stdio.h>
Int main()
{int i,number,count;
for(number=1;number<=100;number++)
{count=0
For(i=2;i<=number/2;i++)
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];
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)
for (i=1;I<=n;i++)
{sum +=(1/i);}
return(sum);}
main()
{ int a;
scanf(“%d”,&a);
printf(“sn=%f”,series(a));
return 0;
QUESTION 9
#include<stdio.h>
#include<math.h>
{ if((pow(n,n)==1)
return(s)
else{s+ =pow(n,n);
n--;
powerseries(n,s);
main()
{int n,s=1;
scanf(“%d”,&n);
return 0;
}
QUESTION 10;
#include<stdio.h>
#include<stdlib.h>
main()
{ FILE*fp;
char c,filename[30];
int character=0,words=0,lines=0;
gets(filename);
fp=fopen(filename,”r”);
if(fp==NULL)
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++;
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;
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]);
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);
for(i=0;i<number(i);i++)
printf(“%c”,number(i));
return 0;
QUESTION 13
#include<stdio.h>
main()
{ float n,p;
Scanf(“%f %f”,&n,&P);
Printf(“%f”,pow(n,p));
return 0;
QUESTION 14
#include<stdio.h>
main()
{ int trio[3];
Int a;
scanf(“%d %d %d”,&trio[1],&trio[2],&trio[3]);
for(a=0;a<3;a++)
printf(“%d”,trio[a]);
for(a=2;a>0;a--)
printf(“%d”,trio[a]);
return 0;
QUESTIO 15
#include<stdio.h>
main()
{ char end;
printf(“menu”);
printf(“\t1.Add\n\t2.Substraction\n\t3.Multiply\n\t4.Divide\n\t5.Modulus”)
scanf(“%d”,&choice);
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;