DSL810
Special Topics in Design
Programming in C
Step 1- The first task was to find an online compiler to help me with writing the codes in the correct syntax. I used "https://www.onlinegdb.com/online_c_compiler" as advised by our professor.
Step 2- I went to howstuffworks.com and learned a few basic commands and their syntax.
Step3- I tried to run a basic code that said that it was the output of my first program,
#include <stdio.h>
int main()
{
printf("This is output from my first program!\n");
return 0;
}
​
Step 4- When we enter this program, position #include so that the pound sign is in column 1 (the far left side). Otherwise, the spacing and indentation can be any way I like it.
Step 5- I should be able see the output "This is output from my first program!" when I run the program.
Programming in Python
Python was a much more simple language, easier to learn. Python has a very simple and elegant syntax. I am using "Sublime text 3" to compile and verify my codes.
Step 1- I tried the most famous beginner's exercise to print "hello world!" on my screen using the simple code-
print("Hello world!")
Step 2- A comprehension of Sanju's notes- Keywords are the reserved words in Python. We cannot use a keyword as a variable name, function name or any other identifier. They are used to define the syntax and structure of the Python language. In Python, keywords are case sensitive. There are 33 keywords in Python 3.7.
Step 3- Learning the rules
-
Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore _. Names like myClass, var_1 and print_this_to_screen, all are valid example.
-
An identifier cannot start with a digit. 1variable is invalid, but variable1 is perfectly fine.
-
Keywords cannot be used as identifiers.
-
We cannot use special symbols like !, @, #, $, % etc. in our identifier.
-
Identifier can be of any length.
​
Step 4- Things to Remember-Python is a case-sensitive language. This means, Variable and variable are not the same. Always name identifiers that make sense.
While, c = 10 is valid. Writing count = 10 would make more sense and it would be easier to figure out what it does even when you look at your code after a long gap.
Multiple words can be separated using an underscore, this_is_a_long_variable.
Write a program to take two integers "a" and "b" as inputs from the user and print their sum.
For C-
/* program to get two numbers from the user and display their sum
*/
#include <stdio.h> //header file for standard input output operations
//main program starts
int main()
{
int a,b; //define the variables for getting data from user
printf("Enter the first number:"); //%d represents integer data type as input
scanf("%d", &a);
printf("\nEnter the second number:");
scanf("%d", &b); //& is for referencing the memory of the variables
printf("\nThe sum of %d and %d is %d",a,b,a+b);
return 0;
}
For Python-
a=int(input("First number: "))
b=int(input("Second number: "))
print("Sum of the two numbers: ", a+b)
Write a program to take an integer "n" as input and print if it is "odd" or "even".
For C-
/* Program to get a number form the user and to check and display if the number is odd/even
​
*/
​
#include <stdio.h> //header file for standard input output operations
//main program starts
​
int main()
{
int n;
printf("Enter the number you want to check as odd or even:");
scanf("%d", &n); //getting data from user
if(n%2==0)
printf("\nThe number is even"); //checking the 'modulus' with 2
else
printf("\nThe number is odd"); //if it isn't even, its odd
return 0;
}
For Python-
a=0
a=int(input("Enter the number to find if it's odd or even: "))
if a%2==0:
print("The entered number is even")
else if a%2!=0:
print("The entered number is odd")
Write a program to take an integer "n" as input from the user and print its factorial.
For C-
/* Program to find the factorial of a numbers
*/
​
#include <stdio.h> //header file for standard input output operations
//main program starts
​
int main()
{
int n,i; //n for the number, i for the index in for loop
int fact=1; // by default factorial values are multiplied, hence value is defined as 1
printf("\t***PROGRAM TO FIND FACTORIAL***\n");
printf("Enter the number you want to find out factorial for: ");
scanf("%d",&n);
//for loop begins
for(i=1;i<=n;i++) {
fact*=i;
}
printf("The factorial of %d is %d",n,fact);
return 0;
}
For Python-
f=1
num=int(input("Enter the number for factorial :"))
while (num>=1):
f*=num
num-=1
print("The factorial is : ", f)